Code support - how to earn cash every 2 minutes (+20$)

Hello! I would like to know how I can adapt my code so that when 2 minutes pass, 20 cash is earned, how can I make that possible? could someone help me please?

Example, time in the game 2 minutes, after those 2 minutes +20 cash is added to all players who are connected (online)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error with getting your data.")
		wait(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)
2 Likes

At the bottom of the script, run this code. For texting purposes, change the wait to something like wait(5)

while wait(120) do --every 120 seconds
       for i, player in pairs(game.Players:GetChildren()) do  --loop through players
              player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
       end
end
2 Likes

is it ok how i put it? :frowning:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error with getting your data.")
		wait(errormessage)
	end
	
	-- Earn 20$ every 2 mins
	while wait(120) do --every 120 seconds
		for i, player in pairs(game.Players:GetChildren()) do  --loop through players
			player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
		end
	end
	-- End code
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)
1 Like

The code above will make a new connection every time someone joins. You want only one loop at the very bottom of the script. Like this:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error with getting your data.")
		wait(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)

	-- Earn 20$ every 2 mins
	while wait(120) do --every 120 seconds
		for i, player in pairs(game.Players:GetChildren()) do  --loop through players
			player:WaitForChild("leaderstats"):WaitForChild("Cash").Value += 20
		end
	end
	-- End code
1 Like

Thanks you a lot! :smiley: It works very well :smiley:

1 Like

No problem!!! If you have any questions at all on why this works, feel free to ask :smile: .

2 Likes

Please use spawn() or coroutine.resume(coroutine.create(function() before the while loop. Not doing so will make so that the rest of the script stops running.

3 Likes

damn you got me, i thought i am going to earn 20$ every 2 minutes

4 Likes

jajajajjajaja :scream: :rofl: :rofl: :rofl: :rofl: :rofl:

1 Like

Definelity got me too lol I don’t think we were the only ones Lol

2 Likes

jajajajajaja omg I can’t believe it :rofl: :rofl: