Is there any way to store/access a specific player's data globally without using datastores or value instances?

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


  1. Create a ModuleScript (You don’t have to change anything inside it)

  2. Inside the script that creates the player’s data, require the ModuleScript at the top of the script

  3. When you create the player’s data, set it to a local variable within the script.

  4. Add the player’s data to the required ModuleScript using the player’s UserId as a key and the player data as a value

    Example: required_module_script[user_id] = player_data



Now if a script requires that same ModuleScript, it should have access to the player’s data as well.


When you want to access the data from another script


  1. Require the same ModuleScript you set the player’s data to

  2. Index the ModuleScript with the Player’s UserID

    Example: local player_data = required_module_script[user_id]


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:

Example: required_module_script[user_id] = nil

2 Likes

You can cache that data in a table, usually through a module then the client or other server scripts can request that data from the cache.

Thanks a bunch man. Really appreciate it.

1 Like

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.

1 Like

The only time you need to set the data within the ModuleScript is when you load from the datastore.

Saving the data to a Datastore should be pulling directly from the ModuleScript, making setting the player’s data again to the ModuleScript pointless.

When setting the player’s data to the ModuleScript, it would be fine to do so from the module used for loading from the Datastore.

Just make sure no scripts try to pull the data from the ModuleScript before it’s set!

1 Like

Alright, once again thank you so much.