What's the best way to handle player data?

I’ve been struggling recently on how to handle player data for my game. I’ve seen lots of tutorials online that create a folder inside of the Player and adds values to that folder, but to me this seems like a bit of a bad way to do it. Also not sure if its secure. Is there a better way to handle the data?

Well, it really depends what data you are trying to manage. Could you tell us more?

It would mainly be money, level, experience, etc.

Then yes, I would use a folder under the player. Hope that helps! :+1:

Who has access to the game? No one can control the data besides developers. Adding values to a folder is a secure method on the serverside, but it seems a little unoptimized and unnecessary even though positive is that you can change it real-time.

I’d say make an empty modulescript reserved for storing data. It can discourage other developers from fiddling around with data if they are that irresponsible, and making it accessible through CLI forces other developers to be aware of the data they’re changing.


For how you store player data, I recommend you use DataStore2 (not ProfileService) for such simple things.

Thank you, i’ll try it out and see if it works for me.

Just be aware: storing as values in folders can serve as safe type-checking.

security-wise it is super secure if you have filtering enabled and do everything on the server, which is a requirement anyways because the changes client makes to the values will stay on the client. Also you should never trust the client like for example if your making a shop, have every items price on the server instead of the client telling you (its easy to manipulate when the client has that power.

the answer is Modulescript

module={}
playerdata={
coin=0, gems=0,
}

function module:updatePlayerdata()
end

function module:savetostore()

end

return module

say the module is named, module
on each player added clone the module and rename it to player.Name, so that you could access player’s data using their username like require([server storage folder][plr.Name])

game.Players.PlayerAdded:Connect(function(plr)
 local module=[module]:Clone()
    module.Name=plr.Name
    module.Parent=[a folder in serverstorage]
end)

when player leaves save the data to data store then destroy that module script
or
you can maintain each player data in a single module as a table and, when a player leaves the game, save the data to datastore then remove the player from that table.

@flvffywvffy

Instead of creating a module for each player you can use just one and store the players’ data in a table

module = {}
module.LoadedData = {}

function module:LoadData(player)
   local data = -- code to load data
   module.LoadedData[player] = data
end

and then simply remove it once the player leaves

hello

Summary

This text will be hidden