Auto Leaderstat Giving System

Hello there!
I have a script that saves players’ stats, and I want to add a system that adds 1 to each player’s stats every 5 minutes. I know this would use a while true do loop and wait(300), but I don’t know where to add these things in. I’m fairly new to scripting, so any and all help would be appreciated!
Script:
local DataStore = game:GetService(“DataStoreService”)
local ds = DataStore:GetDataStore(“CashSaveSystem”)

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new(“Folder”,player)
leader.Name = “leaderstats”
local Cash = Instance.new(“IntValue”,leader)
Cash.Name = “Commends”
Cash.Value = ds:GetAsync(player.UserId) or 0
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)

1 Like

I dont see any reason to save cash every five minutes. If cash is changed, it will auto update, but you could do

function StatsSave(ds,Cash)
  ds:SetAsync(player.UserId,Cash.Value)
end

while true do
  StatsSave()
  wait(300)
end

although I definitely don’t recommend that much

I mean, I want it to add 1 to every players stat every 5 minutes.

function StatsSave(ds,Cash)
  local newcash = Cash.Value + 1
  ds:SetAsync(player.UserId,newcash)
end

while true do
  StatsSave()
  wait(300)
end