This is most likely because ProfileStore doesn’t yield, and when you call the function addItem it would obviously return nil as there is no index within the module.Profiles table following the Player object. Now as for fixing this, I recommend making a module that would return a signal indicating that the player’s data is loaded and THEN call the function or simply do a repeat until scenario in the module over returning which I don’t think that’s a good method.
Me personally? I have an OOP module called PlayerDataService which pretty much handles the player data loading part — passed the Player object as argument.
However in this case, I’m also using ReplicaService of which I return that instead.
Then, I would simply call the Load method and listen for the OnLoad custom signal which returns the Profile object that allows me to work my way through.
And alas, I store both Replica and Profile separately within my custom Player object implementation. I did this so I could easily access it anywhere at anytime on server scripts.
If you think that this approach is way too tedious, then it’s fine. Just know that I believe the reason why it returns nil is because it couldn’t find anything within the module.Profiles table of the Player object index. If you tried printing the table then it would return an empty table {} which I can guarantee you. So either you’re calling the addItem function way too early before the player’s data was even able to load in, or I’m just completely off the track. Anyway, hope this helps somehow.
Unfortunately I don’t think this can really work in this contexts. The specific “Additem” I’m firing right now to test… only fires when the player kills an mob in game by dealing enough damage to it (which a script fires additem to add a item to the player), so the data by then has most definitely loaded in.
Would you ever consider doing a similar data store module that isn’t session locked? There’s certain uses cases that I don’t want session locked by a player and instead use a global key that I want any player to access at anytime
That sort of system wouldn’t be scalable, or in other words, you’d be fine with writing to one key when your game is at less than 100 players, but when 1000+ players play your game accessing one DataStore key more and more will make the system halt as Roblox DataStores were not designed for this and have a limit on how many times you can write to a single key until you run into serious issues.
The module you speak of could not exist, at least it would not work like ProfileStore.
Modules can’t change the fact that DataStore keys have a limit on how often you can write to them from different servers at the same time which makes implementations where you’d have something changeable from all servers non-scalable.
Some limitations can be found here:
Data storage is not magic and there’s a limit to everything, so not everything is possible, but there’s usually a workaround for certain features you want to create.
Currently trying to implement a ‘ban wave’ system. Issue is that when a ban wave occurs, all the players caught in the ban wave aren’t necessarily online or may be in another server (their data is session-locked). Is it possible to load a player’s data who happens to be offline and force write to a certain key (in my case, a “banned” key) even though their data may be session-locked?
Just go through your code and replace all variables and functions to use the new APIs. You might also have to reorder some parts of it. Use the tutorial as a template.
This setting would mute all of the prints coming from ProfileStore. Warnings and errors will stay as they are important.
It could be implemented like this:
SAVE_ENABLED
This setting will control whether saving is enabled. When true, data may be loaded, but no changes are saved. I’ve needed to implement this myself in ProfileService, because I don’t want my code to ruin my data when I’m experimenting with something, or if I’m working on a new system.