Datastore Problems

Hello there, I have made a leaderboard script and the datastore script. There’s a problem. The datastore works only on the Falls value, but not on the Cash value. Here are both scripts

Leaderboard script
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Falls = Instance.new("IntValue")
	Falls.Name = "Falls"
	Falls.Value = 0
	Falls.Parent = leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Value = 0
	Cash.Parent = leaderstats
	
	
end)
DataStore script
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Falls
	local save2 = plr.leaderstats.Cash
	
	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then 
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else 
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Falls.Value}, {plr.leaderstats.Cash.Value})
end)

Help would be appriciated, there are no errors. I’m really confused.

2 Likes

SetAsync only uses 1 table, so it’d be

ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Falls.Value, plr.leaderstats.Cash.Value})
1 Like

When changed the datastore still doesn’t work. This time both stats

1 Like

Also, the value adds to the leaderboard once ex. let’s say it was 0, i touched the part and now it’s 15, the second time it doesnt add a value

1 Like

Just like @12904 said, you cannot store more than one dataset in a given Datastore. Your code should look like this:

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Falls.Value, plr.leaderstats.Cash.Value})
end)

Another point to keep in mind, with my experience of using PlayerRemoving to save data after the player leaves the game, it doesn’t always work. SetAsync is a yielding function and does not save data immediately, so as the player leaves the game, it is not guaranteed that the data will always save.

To combat this, I found Datastore2 a much better primary choice for saving user data.