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

Can anybody help with DataStores?

Asked by
wackem 50
9 years ago

Ok, I was the first person to join the game and now I'm the only person with a value in the leaderboard, the issue is when another person joins it errors saying "Argument Missing or nil" on line 12 of the following script and I have no idea why. Anyone know why?

DataStore = game:GetService("DataStoreService"):GetDataStore("Values")


game.Players.PlayerAdded:connect(function(plr)
    local val = 0
    local save = "plr-"..plr.userId
    local savedVals = DataStore:GetAsync(save)
    if savedVals then
        val = savedVals
    else 
        val = 0
        DataStore:SetAsync(save)
    end

    local stat = Instance.new("IntValue", plr)
    stat.Name = "leaderstats"   

    local cred = Instance.new("IntValue", stat)
    cred.Value = val
    cred.Name = "Credits"
end)

game.Players.PlayerRemoving:connect(function(plr)
    local val = plr.leaderstats.Credits.Value
    local save = "plr-"..plr.userId
    DataStore:SetAsync(save)
end)

1 answer

Log in to vote
1
Answered by
rexbit 707 Moderation Voter
9 years ago

You are forgetting to save your value on line 12 and line 26

DataStore = game:GetService("DataStoreService"):GetDataStore("Values")


game.Players.PlayerAdded:connect(function(plr)
    local val = 0
    local save = "plr-"..plr.userId
    local savedVals = DataStore:GetAsync(save)
    if savedVals then
        val = savedVals
    else 
        val = 0
        DataStore:SetAsync(save,val)
    end

    local stat = Instance.new("IntValue", plr)
    stat.Name = "leaderstats"   

    local cred = Instance.new("IntValue", stat)
    cred.Value = val
    cred.Name = "Credits"
end)

game.Players.PlayerRemoving:connect(function(plr)
    local val = plr.leaderstats.Credits.Value
    local save = "plr-"..plr.userId
    DataStore:SetAsync(save,val)
end)

Hope this helps

0
Thank you so much! wackem 50 — 9y
Ad

Answer this question