I’m working on a game which uses pretty solidly large dictionaries to store a given player’s data, and as of right now, the only way to access that data (from any script that wasn’t the one creating the data itself) is to load the player’s data using datastoreservice, which obviously is not feasible if you want to have this data available any time the player is doing or taking damage because you need their damage or armor values (as a rough example).
The first solution I thought of is to use Value objects to store every single value of the player, and change them any time their data is modified, but before doing that I’d appreciate if anybody could offer up any alternatives.
Any help would be greatly appreciated, and I just wanted to check my options before committing into something that already sounds inefficient and tedious in my head.
A ModuleScript would be perfect for what you are trying to do. They allow multiple scripts access to the same memory address.
If you don’t know how they work, then I would suggest you look up short tutorials or documentation on them.
I’ll also include a very short and quickly made series of steps to follow to make a very simple storage for player’s data that other scripts can retrieve using ModuleScripts.
ModuleScript player data storage
Create a ModuleScript (You don’t have to change anything inside it)
Inside the script that creates the player’s data, require the ModuleScript at the top of the script
When you create the player’s data, set it to a local variable within the script.
Add the player’s data to the required ModuleScript using the player’s UserId as a key and the player data as a value
Any changes you make the player’s data should be seen by the other scripts that require the ModuleScript since they both would target the same memory address.
Just make sure to set the player’s data to the ModuleScript before trying to retrieve it, otherwise it will return nil.
IMPORTANT: You MUST delete player’s data from the module script when they leave the game!!!
Otherwise, you will cause what is called a memory leak. Overtime the server can slow down (lag) and eventually shut down causing player’s experience to suffer.
To protect against this, when a player leaves, require the ModuleScript and using the player’s UserId as index, set the value to nil:
quick question before I test it out. Is it fine to write the “ModuleName[Player.UserId] = data” inside the module I use for saving and loading data? As in writing the data into the module every time I save and load that is.