Saving player data in a module script

Hey!
I was thinking about the best method to store user data safely on the server. I decided that I wanted to use a module script that holds all player data in its table. But how does it work? Do you need a key for every user id in the table or is there a table automatically generated for every user?

Let’s say this is my module script in ServerScriptService:

local playerdata = {math.random(1, 100)}
print('hello there!')
print(playerdata)
return playerdata

and this was my server script:

game.Players.PlayerAdded:Connect(function(plr)
local playerdata = require(game.ServerScriptService.ModuleScript)
end)

When I run it in Studio it says “hello there!” and gives me a table with the first item being a random number between 1 and 100. But how would this look like ingame with multiple players? Are there now multiple instances of the table with a random number or does is basically update the number every time a player joins in only one table?

Do I have to do this or not:

-- module script
local playerdata = {}
playerdata.new = function(userid)
playerdata[userid] = {math.random(1, 100)}
end
print("hello there!")
return playerdata

-- server script
game.Players.PlayerAdded:Connect(function(plr)
local playerdata = require(game.ServerScriptService.ModuleScript)
playerdata.new(plr.UserId)
end)

Thanks for every answer. If you have any questions let me know but hopefully you understand what I mean!

Yes, otherwise you are modifying the same table.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.