Saving data script

I have a script supposed to save your leaderstats. I have api services enabled.
local DS = game:GetService(“DataStoreService”):GetDataStore(“SaveData”)

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

wait()

local plrkey = “Id_”…plr.userId

local save1 = plr.leaderstats.Cash

local save2 = plr.leaderstats.Strength

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)

end

end)

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

DS:SetAsync(“id_”…plr.userId,{plr.leaderstats.Strength.Value, plr.leaderstats.Cash.Value})

end)
ps anything with … is actually … devforum just keeps correcting it.

2 Likes

What seems to be the issue here?

Also, if you don’t mind, would you add formatting and indentation to your code? It makes it a lot easier for us to read and understand.
This:
```lua
– code
```
Becomes:

-- code

Just a note, Roblox Studio is kind of bugged and data doesn’t actually save consistently, so I suggest you to test on the actual Roblox server as see if it works.

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "Id_"..plr.userId
local save1 = plr.leaderstats.Cash
local save2 = plr.leaderstats.Strength

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)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
DS:SetAsync("id_"..plr.userId,{plr.leaderstats.Strength.Value, plr.leaderstats.Cash.Value})
end)
1 Like

The issue is that the script is supposed to save leaderstats but it is not. I have commented the code in the format BTW

Youre doing "id_"as the key instead of “Id_” like you did in the

2 Likes

Oh i see it now, cant test rn as i have to go but I will reply to you tomorrow if it works :slight_smile: