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

Leaderstats not saving?

Hi guys! I have 3 leaderstats that I originally was saving when the player left the game, but if the game crashed it didn't save. So, I am now saving when it changes! Any ideas why this is not saving? Thanks!

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
lolz={"Escapes","Coins","Diamonds"}

game.Players.PlayerAdded:connect(function(plyr)
    local a=Instance.new("NumberValue")
    a.Parent=plyr
    a.Name="leaderstats"
    for i=1,#lolz do
        local stat=Instance.new("NumberValue")
        stat.Parent=a
        stat.Value=0
        stat.Name=lolz[i]
    end
   child=plyr.leaderstats:GetChildren() -- made it global for efficiency
    for i=1, #child do
        child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
    end
    for i,v in pairs (child) do
        v.Changed:connect (function ()
            ds:SetAsync (plyr.userId,v.Value)
        end)
    end
end)

1 answer

✓ Accepted answer

You're pulling plyr.userId .. child[i].Name but you are saving it under plyr.userId

Try this:

ds:SetAsync(plyr.userId .. v.Name, v.Value)

on line 20

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