R
roblox-ts3w ago
kv_

flamework not identifying service constructor

tl;dr services cannot be in shared I am using flamework for the first time and I'm not sure whether this is a conceptual misunderstanding or there is something I'm not seeing I have the following service (shared):
export interface OnSessionLoaded { /* ... lifecycle event */ }
export interface OnSessionUpdate { /* ... lifecycle event */ }

@Service()
export class SessionProvider {
PlayerStore: ProfileStore.Store<typeof PROFILE_TEMPLATE, object> | Omit<ProfileStore.Store<typeof PROFILE_TEMPLATE, object>, "Mock">;
Sessions: { [key: number]: ProfileStore.Profile<typeof PROFILE_TEMPLATE, object> | undefined} = {};

constructor() {
this.PlayerStore = RunService.IsStudio()
? ProfileStore.New("PlayerStore", PROFILE_TEMPLATE).Mock
: ProfileStore.New("PlayerStore", PROFILE_TEMPLATE);

const OnSessionLoaded_listeners = new Set<OnSessionLoaded>();
const OnSessionUpdate_listeners = new Set<OnSessionUpdate>();

Modding.onListenerAdded<OnSessionLoaded>((object) => {OnSessionLoaded_listeners.add(object)});
Modding.onListenerAdded<OnSessionUpdate>((object) => {OnSessionUpdate_listeners.add(object)});

// ...
}

GetSession(player: Player) {
// ...
}
}
export interface OnSessionLoaded { /* ... lifecycle event */ }
export interface OnSessionUpdate { /* ... lifecycle event */ }

@Service()
export class SessionProvider {
PlayerStore: ProfileStore.Store<typeof PROFILE_TEMPLATE, object> | Omit<ProfileStore.Store<typeof PROFILE_TEMPLATE, object>, "Mock">;
Sessions: { [key: number]: ProfileStore.Profile<typeof PROFILE_TEMPLATE, object> | undefined} = {};

constructor() {
this.PlayerStore = RunService.IsStudio()
? ProfileStore.New("PlayerStore", PROFILE_TEMPLATE).Mock
: ProfileStore.New("PlayerStore", PROFILE_TEMPLATE);

const OnSessionLoaded_listeners = new Set<OnSessionLoaded>();
const OnSessionUpdate_listeners = new Set<OnSessionUpdate>();

Modding.onListenerAdded<OnSessionLoaded>((object) => {OnSessionLoaded_listeners.add(object)});
Modding.onListenerAdded<OnSessionUpdate>((object) => {OnSessionUpdate_listeners.add(object)});

// ...
}

GetSession(player: Player) {
// ...
}
}
And a dependent component (server):
import { SessionProvider } from "shared/session";

// ...

@Component()
}) export class Kiosk extends BaseComponent<{}, ComponentInstance> implements OnStart {
constructor(private SessionProvider: SessionProvider) { super(); }

onStart() { /* ... */ })
}
}
import { SessionProvider } from "shared/session";

// ...

@Component()
}) export class Kiosk extends BaseComponent<{}, ComponentInstance> implements OnStart {
constructor(private SessionProvider: SessionProvider) { super(); }

onStart() { /* ... */ })
}
}
I am getting the error, [...].modding:510: Could not find constructor for shared/session@SessionProvider while constructing Kiosk Additionally, I am a bit confused on how I am supposed to communicate between scripts. The SessionProvider creates a couple lifecycle events and fires them as needed, but I am not sure how I create a listener, the documentation has me lost, and I am thinking there is probably a better way to do that... Any help is appreciated :)
Solution:
Message Not Public
Sign In & Join Server To View
Jump to solution
8 Replies
Solution
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
kv_
kv_OP3w ago
Ook I see
kv_
kv_OP3w ago
https://flamework.fireboltofdeath.dev/docs/modding/guides/lifecycle-events This docs page details custom lifecycle events, can listeners not use them?
Lifecycle Events | Flamework
This guide is for creating a custom lifecycle event.
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
kv_
kv_OP3w ago
Ohh ok I see. My question is how could I listen to events like onSessionLoaded? Eg.
import { Service } from "@flamework/core";
import { OnSessionLoaded } from "server/services/session";

@Service()
export class Leaderstats implements OnSessionLoaded {
onSessionLoaded(player: Player, session: ProfileStore.Profile<{ coins: number; }, object>): void {
let folder = new Instance("Folder", player);
let coin_val = new Instance("IntValue", folder);
coin_val.Value = session.Data.coins
}
}
import { Service } from "@flamework/core";
import { OnSessionLoaded } from "server/services/session";

@Service()
export class Leaderstats implements OnSessionLoaded {
onSessionLoaded(player: Player, session: ProfileStore.Profile<{ coins: number; }, object>): void {
let folder = new Instance("Folder", player);
let coin_val = new Instance("IntValue", folder);
coin_val.Value = session.Data.coins
}
}
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
kv_
kv_OP3w ago
Ok, I think it's just that the metamethods I added are misbehaving. Both of those lifecycle events are fired by changes to the ProfileStore Data table:
// Add special metamethods to fire the session data changed life cycle events on table modification/index
setmetatable(profile.Data, {
__index: () => {
for (const listener of OnSessionUpdate_listeners) {
task.spawn(() => {listener.onSessionDataChanged(player, this.Sessions[player.UserId])})
print(listener)
print('asdf')
}
},
__newindex: () => {
for (const listener of OnSessionUpdate_listeners) {
task.spawn(() => {listener.onSessionDataChanged(player, this.Sessions[player.UserId])})
print(listener)
print('asdf')
}
}
})
// Add special metamethods to fire the session data changed life cycle events on table modification/index
setmetatable(profile.Data, {
__index: () => {
for (const listener of OnSessionUpdate_listeners) {
task.spawn(() => {listener.onSessionDataChanged(player, this.Sessions[player.UserId])})
print(listener)
print('asdf')
}
},
__newindex: () => {
for (const listener of OnSessionUpdate_listeners) {
task.spawn(() => {listener.onSessionDataChanged(player, this.Sessions[player.UserId])})
print(listener)
print('asdf')
}
}
})
But it appears they are not doing as they should :p
kv_
kv_OP3w ago
Thank you :)

Did you find this page helpful?