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

Why won't any of my stats save? They stick at 1 value and don't save.

Asked by 9 years ago
local ds = game:GetService("DataStoreService"):GetDataStore("stats")
local stats={"Intelligence","MaxEXP","EXP","MagicEnergy","MaxMagicEnergy","Level","LastLevel","Defense","Strength","Team","Cash","Wind","Fire","Earth","Water","Reputation"}
game.Players.PlayerAdded:connect(function(plyr)
    local a=Instance.new("NumberValue")
    a.Parent=plyr
    a.Name="leaderstats"
    for i=1,#stats do
        local stat=Instance.new("NumberValue")
        stat.Parent=a
        stat.Value=0
        stat.Name=stats[i]
    end
    local child=plyr.leaderstats:GetChildren()
    for i=1, #child do
        child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
    end
end)

game.Players.PlayerRemoving:connect(function(plyr)
    local child=plyr.leaderstats:GetChildren()
    for i=1, #child do
        child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
    end
end)

0
Why are you setting the stat value when you are saving? NotsoPenguin 705 — 9y

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

You're trying to set child[i]'s value to a function that doesn't return a value on line 22... this doesn't make any sense. Just use the SetAsync function without trying to set the value of child[i].

Also, on line 15, you're setting the value when a player joins directly to the GetAsync function.. the GetAsync function can return nil, if this is the case then your code will error. This can happen if the player has never been to your game before, and they have no saved stats. So check if there are saved stats before setting them.

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
local stats={"Intelligence","MaxEXP","EXP","MagicEnergy","MaxMagicEnergy","Level","LastLevel","Defense","Strength","Team","Cash","Wind","Fire","Earth","Water","Reputation"}

game.Players.PlayerAdded:connect(function(plyr)
    local a=Instance.new("NumberValue",plyr)
    a.Name="leaderstats"
    for i=1,#stats do
        local stat=Instance.new("NumberValue",a)
        stat.Name=stats[i]
        local data = ds:GetAsync(plyr.userId..stats[i])
        if data then    
            stat.Value = data
        else
            stat.Value = 0
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plyr)
    local child=plyr.leaderstats:GetChildren()
    for i=1, #child do
        ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
    end
end)
0
OH, thanks that made it easier to understand. attackonkyojin 135 — 9y
Ad

Answer this question