I created a script to save the amount of cash a player has but it doesn't save the amount of cash they have. How would I fix it to make it work? Here is my code:
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder")
leader.Name = "leaderstats"
leader.Parent = player
local Cash = Instance.new("IntValue")
Cash.Parent = leader
Cash.Name = "Cash"
Cash.Value = ds:GetAsync(player.UserId)
ds:SetAsync(player.UserId, Cash.Value)
Cash.Changed:connect(function()
ds:SetAsync(player.UserId, Cash.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Cash.Value)
end)

5 comments
You shouldn't save the cash every time it changes. It's bad practice and can throttle the request limit if saving too often. Only save when they leave, and no need to save it right when they join.
I don't know how to do that, could you please show me how?
Also, will that fix the script not working?
You are already saving when they leave. Just don't save when the cash changes and right after they join.
Yeah lol if you keep on saving it, it'll get buggy and break down.