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

What are the benefits of saving data to a table and how can it be done?

I've seen many pages of people asking how to save datastores to a table and people have also recommended me to do it but i don't understand what are the benefits of doing so. I wanted to give it a try so this is what i've attempted to do but i'm not sure if i'm doing it right.


--//Service
local dataService = game:GetService('DataStoreService')

--//DataStores
local dataStores = {
	goldData = dataService:GetDataStore('GoldData'),
	silverData = dataService:GetDataStore('SilverData')
}

--//Save Function
function saveData(dataName, playerId, value)
	dataName:SetAsync(playerId, value)
end

--//PlayerAdded
game.Players.PlayerAdded:Connect(function(players)
	
	local ID = players.UserId
	
	local folder = Instance.new('Folder')
	folder.Name = 'leaderstats'
	folder.Parent = players
	
	local gold = Instance.new('IntValue')
	gold.Name = 'Gold'
	gold.Parent = folder
	
	local silver = Instance.new('IntValue')
	silver.Name = 'Silver'
	silver.Parent = folder
	
	local loadGoldData = nil
	local loadSilverData = nil
	
	pcall(function()
		loadGoldData = dataStores.goldData:GetAsync(ID)
		loadSilverData = dataStores.silverData:GetAsync(ID)
	end)
	
	--//If player has data then load it
	if loadGoldData and loadSilverData ~= nil then
		gold.Value = loadGoldData
		silver.Value = loadSilverData
		print('Data Loaded')
	else
	--//Else player is new 
		gold.Value = 10
		silver.Value = 100
		print('New Player')
	end
end)

--//PlayerRemoving
game.Players.PlayerRemoving:Connect(function(players)
	local ID = players.UserId
	
	saveData(dataStores.goldData, ID, players:WaitForChild('leaderstats'):WaitForChild('Gold').Value)

	saveData(dataStores.silverData, ID ,players:WaitForChild('leaderstats'):WaitForChild('Silver').Value)
end)

--//BindToClose
game:BindToClose(function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v then
			v:Kick('The Server Has Shut Down')
		end
	end
	wait(3)
end)




1 answer

✓ Accepted answer

Datastores have limits on how often you can save and write to them. Often times you'll want to save multiple bits of information that are related in some way, such as Level, Experience, Cash, etc. By grouping them into a table of {Level = n, Experience = n, Cash = n} and saving that table you can cut down the amount of save/writes you need to do when a player joins and you try to load their data.

What you're doing is making a table of datastores you've loaded, what you want to do instead is use one data store and save your Gold/Silver data together.

Saving a table is the same as saving any other value

DS:SetAsync("Cash", {Gold = gold.Value, Silver = silver.Value})
loadData = DS:GetAsync("Cash")
player.Leaderstats.Gold.Value = loadData.Gold
player.Leaderstats.Silver.Value = loadData.Silver

1 comment

0

Thanks, now i understand!

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