Setting up the players data/stats safely


#1
function dataManager:GetData(player)
	local myData = playerDataStore:GetAsync(player.UserId) or {}
	
	myData.level = myData.level or 1
	myData.exp = myData.exp or 0
	myData.gold = myData.gold or 100
	myData.gems = myData.gems or 0
	
	myData.class = myData.class or 'Knight'
	
	local playerFolder = Instance.new('Folder')
	playerFolder.Name = 'PlayerFolder'
	
	local level = Instance.new('IntValue')
	level.Name = 'Level'
	level.Value = myData.level
	level.Parent = playerFolder
	
	local exp = Instance.new('IntValue')
	exp.Name = 'Exp'
	exp.Value = myData.exp
	exp.Parent = playerFolder
	
	local gold = Instance.new('IntValue')
	gold.Name = 'Gold'
	gold.Value = myData.gold
	gold.Parent = playerFolder
	
	local gems = Instance.new('IntValue')
	gems.Name = 'Gems'
	gems.Value = myData.gems
	gems.Parent = playerFolder
	
	local class = Instance.new('StringValue')
	class.Name = 'Class'
	class.Value = myData.class
	class.Parent = playerFolder
	
	playerFolder.Parent = player
				
	playerData[player.UserId] = myData
end

This is kinda how I’ve always set up my data for games, creating a data store for them, then created values inside the player (so UI can easily access their stats to display their level, cash, etc.)

Few things I’m not certain on. If this is the most efficient way to go about this. I have thought about just firing an event whenever one of the values is update, and have the UI look for, and just change whatever needs to be changed from there. Just keeping the data stores and removing the values seems like it would be safer too, as I know players can just edit values from within their player, and that is why all the values are set to the myData (the data store) values, as there is no way a player can edit these.

Any words of advice?? If it would be better to just fire and event off whenever the data store is changed???


#2

Just to clarify – the player cannot edit folders in their player on the server. They can do it on the client, but it won’t be replicated, and if they do edit it on their client, any issues that come with that are their fault because they’re exploiting.

You don’t need a special event to listen for data being updated unless you want a very broad signal saying ‘something in the player’s data changed but I don’t necessarily know what’. And on the note of broad signals, it’s a waste of performance if the client is looking for something that could have easily been given (i.e. what value changed) for no good reason.

Anyway, if you’re using an object like you are now, you can easily just get the player to listen for changes to specific values:

playerFolder.Level:GetPropertyChangedSignal("Value"):Connect(function()
    print("Level has been changed to", playerFolder.Level.Value)
end)

In the end it’s all up to you. There isn’t a wrong way. You can make your own event(s) or use features built into Roblox instances.