Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My saving script only saves one time and now I cannot earn more. Does anybody know how to fix this?

Asked by 4 years ago
Edited 4 years ago

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!

0
It's "UserId", not "userId". There might be more errors but that's all I can see. ParticularlyPenguin 71 — 4y
1
In the future please write your code using the lua code function. It makes it much more neat and is easier to read. ParticularlyPenguin 71 — 4y
0
Use SetAsync not GetAsync to save. SethHeinzman 284 — 4y

Answer this question