I have been trying make the data from my simulator game to save. I am relatively new to coding and writing script so i have been trying to follow tutorials, but none of them seem to work. Whenever i try the game to see if my data saves nothing happens. Now i have found one that I thought would work but it did not. It worked in the beginning but now i cannot earn any more of the things i want to earn, it just goes back to what i had from the first time i played. I mean that if I had 5 Skill and then earned 10 it would just go back down to 5 if I left and joined again. Here is the code I have written:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)

wait()

local plrkey = "Id_"..plr.userId

local save1 = plr.leaderstats.Skill

local save2 = plr.leaderstats.Cash

local save3 = plr.leaderstats.Rebirths

local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
	save1.Value = GetSaved[1]
	save2.Value = GetSaved[2]
	save3.Value = GetSaved[3]
else
	local NumberForSaving = {save1.Value, save2.Value, save3.Value}
	DS:GetAsync(plrkey, NumberForSaving)
end

end)

game.Players.PlayerRemoving:Connect(function(plr)

DS:SetAsync("Id_"..plr.userId, {plr.leaderstats.Skill.Value, plr.leaderstats.Cash.Value, plr.leaderstats.Rebirths.Value})

end)

I really hope someone can help since i cannot make a simulator without saving the leaderstats!

3 comments

0

It's "UserId", not "userId". There might be more errors but that's all I can see.

1

In the future please write your code using the lua code function. It makes it much more neat and is easier to read.

0

Use SetAsync not GetAsync to save.