Is this an efficient way to data store multiple values?

Hi DevForum,

The title is pretty self-explanatory, but I was wondering with the code I made, if it was efficient in saving multiple values. If not, can you help me correct any mistakes?

Thanks!

Code:

local NamesModule = require(script.AvailabilitiesModule)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local playerData = Instance.new("Folder", player)
	playerData.Name = "playerData"
	
	local family = Instance.new("StringValue", playerData)
	family.Name = "Family"
	
	local jenny = Instance.new("IntValue", playerData)
	jenny.Name = "Jenny"
	jenny.Value = 0
	
	family.Value = NamesModule[math.random(1,#NamesModule)]
	
	local fullname = (player.Name.. " ".. family.Value)
	
	
	local UserId = "Player_".. player.UserId
	
	--// Load Data
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(UserId)
	end)
	
	if success then
		family.Value = data.family
		jenny.Value = data.jenny
		
		--// Set data = the values
		
		
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local UserId = "Player_".. player.UserId
	
	local data = {family = player.playerData.Family.Value, jenny = player.playerData.Jenny.Value}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(UserId, data)
	end)
	
	if success then
		print("Data Successfully Saved!")
	else
		warn("There was an error saving your data. ErrorMessage: ".. errormessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		local UserId = "Player_".. player.UserId
		
		local data = {family = player.playerData.Family.Value, jenny = player.playerData.Jenny.Value}
		
		myDataStore:SetAsync(UserId, data)
	end
end)

I recommend you to use [“family”] instead of just “family”, that’s a better way to save multible values with any letter, for example: [“Best Family In Town”]. The script looks solid but I recommend you to save as many times as posible the data of the players, for example you can make a script that does a loop saving a player progress, its very important to add this because sometimes roblox goes down, errors while saving in “PlayerLeaving” function, etc, also add a Kick function if the data of the player doesn’t load, it’s very important to make the data of the player load becuase if his data was “family = rich”, and when he joins the game the data doesn’t load it will be “family = ???”, when he notice that, he will rejoin to try to solve that problem but when that happend the script will save the data as "family = ???’. This is a really big problem if you don’t have this on consideration.

  • You are doing a great job, keep the good work!
1 Like

I would avoid saving dictionaries and tables, yes there is now the compatibility but it still causes it to be a little less reliable than string.

I think either converting to string or using table.concate on an array would be better, you can easily split it back up and its better to save strings.

The other thing is you are saving the values as a dictionary then setting them to other values, wouldnt it be more benificial to just keep them in the original loaded format at to use that, otherwise whats the point in making a dictionary if you arent intending to use a dictionary to read your data.

1 Like