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)

3 comments
DO NOT answer your own question. Add your answer's information to your original question using 'edit' instead.
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.
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 :)