So I 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)

3 comments

0

Remove the while loop at your first script, and the parent argument to Instance.new is deprecated

0

Show full scripts? This definitely isnt the entire script. CashODS isn't defined.

0

Not quite sure how to help you, but if you show us the whole script, we could possibly be able to help you. Good luck on your game though!