Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I save 20 values with datastore efficiently?

Im trying to make an inventory system where people can get different colors and faces, and this will save. To save it, im trying to add values in player that save, but it will start getting messy and might break when I get to a lot of values. If anyone can give me a datastore they use for their inventories, that would help a lot! Thank you for any help! Here is my saving leaderboard script:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("KillsaveSystem")--change all "kills" to what value you want and keep doing it 
local ds2 = datastore:GetDataStore("CashSaveSystem")--change all "cash" to what value you want  and keep doing it

game.Players.PlayerAdded:Connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local Kills = Instance.new("IntValue", folder)
 Kills.Name = "Kills"
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Cash"
 
 Kills.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, Kills.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
 
end)
game.Players.PlayerRemoving:Connect(function(plr)
	
		ds1:SetAsync(plr.UserId, plr.leaderstats.Kills.Value)
	ds2:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
end)

1 answer

✓ Accepted answer

If you want to save multiple values, it's best to do so with a table, this way you only need a single DataStore, rather than several of them for every stat.

In general, you also want to wrap the DataStore inside a pcall function to make sure no errors are thrown and that the script remains intact.

Finally, when you set parents on new instances, you can use the given parameter, but it is less taxing on your game's memory (this may not be the correct wording for it) than if you were to add a new line of code to set the parent.

I generally also write out the base values under the new instances for the DataStore since if it returns nil values, these will still override the "or" part of your GetAsync. However, since both value defaults given by you will be 0, you don't need to set the values since they will be defaulted to zero regardless.

Example Server Script based on above in Question

local DataStore = game:GetService("DataStoreService")
local Storage = DataStore:GetDataStore("SaveSystem")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr

	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = folder
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = folder

	local success, result = pcall(function()
		Storage:GetAsync(plr.UserId)
	end)
	
	if not success then
		warn(result)
	else
		local LoadTable = Storage:GetAsync(plr.UserId)
		if LoadTable then
			Kills.Value = LoadTable[1]
			cash.Value = LoadTable[2]
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local SaveTable = {plr.leaderstats.Kills.Value, plr.leaderstats.Cash.Value}
	Storage:SetAsync(plr.UserId, SaveTable)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.