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

UpdateAsync script is repeating itself?

Asked by 6 years ago
Edited 6 years ago

So thanks to Zenith_lord I now have this script that adds cash to a global leaderboard based on the amount of cash a player has in their leaderstats

while true do
    for _,Player in pairs(game.Players:GetPlayers()) do
        CashODS:UpdateAsync(Player.Name, function(oldValue)
        local newValue = oldValue or 0
        newValue = newValue + Player.leaderstats.Stage.Value
        return newValue
    end)
    end

Why is this script repeatedly adding the players leaderstat value to the leaderboard? I only want it to add the leaderstat value when the player earns cash. Help would be much appreciated!

Leaderstat/datastore script:

local Data = game:GetService("DataStoreService"):GetDataStore("CashDataStore")

local function Added(Player)
    local LS = Instance.new("Folder",Player)
    LS.Name = "leaderstats"

    local Stage = Instance.new("NumberValue", LS)
    Stage.Name = "Stage"
    Stage.Value = Data:SetAsync(Player.UserId, 0)

end

game.Players.PlayerRemoving:Connect(function(Player)
    Data:SetAsync(Player.UserId, Player.leaderstats.Stage.Value)
end)

game.Players.PlayerAdded:Connect(Added)

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

UpdateAsync(...) takes a key and a function to change the value. Instead of giving it a function, you gave it the value itself. You should change Player.leaderstats.Cash.Value to a function that changes the value.

For more info, read this

Also, in terms of your game, you shouldn't get data from leaderstats. Exploiters can easily modify the leaderstats values. You should store the values in ServerStorage.

I hope this answered your question.

Ad
Log in to vote
0
Answered by 6 years ago

Thank you Zenith_Lord, I fixed the script, however this script is repeatedly adding the players leaderstat to the leaderboard, how do I prevent this?

while true do
    for _,Player in pairs(game.Players:GetPlayers()) do
        CashODS:UpdateAsync(Player.Name, function(oldValue)
        local newValue = oldValue or 0
        newValue = newValue + Player.leaderstats.Cash.Value
        return newValue
0
DO NOT answer your own question. Add your answer's information to your original question using 'edit' instead. laughablehaha 494 — 6y
0
Also to the above question, there isn't enough information to delineate the problem. None of these lines of code directly adds the player leaderstat. You may have an 'Instance.new("IntValue")' in your while true do loop without 'if' statements to check if it already exists. laughablehaha 494 — 6y
0
Sorry i'm new to this website, I've made edits to the post. I'm still having some issues with the script. I've added more detail to my question now if want to look at it :) DannyKyun 14 — 6y

Answer this question