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

Why are these DataStore issues happening?

Asked by 9 years ago
ds = game:GetService("DataStoreService")
plr = script.Parent.Parent

wait(5)

game.Players:WaitForChild(script.Parent.Parent.Name):connect(function(plrAdded)
    stats = Instance.new("IntValue", script.Parent.Parent)
    stats.Name = "leaderstats"

    kills = Instance.new("NumberValue", script.Parent.Parent:FindFirstChild("leaderstats"))
    kills.Name = "Kills"
    kills.Value = ds:GetASync("kills_"..script.Parent.Parent.userId)

    cash = Instance.new("NumberValue", script.Parent.Parent:FindFirstChild("leaderstats"))
    cash.Name = "Cash"
    cash.Value = ds:GetASync("cash_"..script.Parent.Parent.userId)
end)

wait()
cash = plr:FindFirstChild("leaderstats").Cash.Value
kills = plr:FindFirstChild("leaderstats").Kills.Value

ds:SetASync("cash_"..plr.userId, cash)
ds:SetASync("kills_"..plr.userId, kills)

Error line 6. And many other errors. Please do not just fix this script. Please explain in depth what you did because I want to learn from my mistakes not just give it to someone else to fix and not learn from it.

0
I used same variables twice. YellowoTide 1992 — 9y

1 answer

Log in to vote
1
Answered by
TofuBytes 500 Moderation Voter
9 years ago

It's a lot easier to store them into one DataStore. Also, a table could help. Here's a simplified version of what you have. The data will store into "Stats".

local ds = game:GetService("DataStoreService"):GetDataStore("Stats")
stats={"Kills","Cash"}
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)

Ad

Answer this question