Where should i store the data of players?

  1. What do you want to achieve? I want a way to store player data temporarily somewhere in the server while the player is in the game, and then save that to the the datastores when they leave. Is there a better way of doing this?

  2. What is the issue? I dont know whats the optimal way of doing this and i dont want to mess it up

  3. What solutions have you tried so far? I’ve tried saving the values to the datastores as they changed but that was stupid since there’s a limit to how often you can do that, So now i’m thinking of creating a folder with NumValues representing their data for each player.

Create a Table in the Script, everytime the Player joins, get their data and store it into this table using their UserId. The UserId is unique to every single player, and never changes, making it a perfect option for acessing every Players Data.

, when the Player leaves, ensure the data saves and then remove the data from the table.
You can keep the Data for when the Server Closes to ensure data saves if you like if it may be safer to just remove it once you are done with it.

I usually do this to keep Player data organized. Its easy to acess without having to send a request for the data again, and again, or to gather all the data I need for the save, whenever there is a change, you just tell it to change a specifc value in the Players Data and you’re good to go.

2 Likes

If that table exists in the script, then how would i be able to acess that data with other unrelated scripts?

1 Like

You can have a BindableEvent Connected to those Scripts to access the Data, ScriptA (The Core Script) would be the Script that would fire the Event, while ScriptB (Other Scripts in the same Environment) would be the one recieving the Data, if you need to send Data to the Client, or vice versa, You use a RemoteEvent however with both Instances I recommend validating all Data coming from, or to them.

1 Like

Alternatively, the script getting the data could put it in a ModuleScript so any script that needs to access the player data can require the module and do whatever it pleases.

Unfortunately that isnt how that works.

requiring a ModuleScript just pulls the table that its in the ModuleScript into the Script that its in, if another Script requires it, it will just grab the data was already there to begin with.

All scripts that require the ModuleScript get the exact same table (same memory address), so any changes made to the table in one script will show up in all the others.

-- Module:
local module = {}
return module

-- Script 1:
local m = require(script.Parent.ModuleScript)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	m[player.UserId] = player
end)

-- Script 2:
local m = require(script.Parent.ModuleScript)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	task.delay(1, function()
		print(m[player.UserId]) -- Will print the player's name since 'm' is a reference to the table returned by the ModuleScript
	end)
end)

I have tried this but that doesn’t work unfortunatly. I recommend abstaining from using modulescripts as data holders.