How would I give (x) cash every (y) seconds to every player?

I dont know how to do this. Should it be on the player?

Basically what I want is a player to be given a certain amount of cash (determined by a stat in leaderstats) every y seconds (also in leaderstats) Its also worth to note that every player may be different. So player a might have to wait 5 seconds for 20 dollars, while player b might have to wait 2 seconds for 40 dollars.

Should It be on the player, or the server? If so, how would I do that? I have no idea where to start with this, so I want to ask people whos a lot smarter than me. I have the amount of time already set, its in the leaderstats (same with the wait time)

Should I make a while loop that just loops through all the players to check their wait time and money per wait. Another problem that may face this script is that there’s gonna be multiple, so should I just loop through all the players and check the wait time and money, or should I have wait time x and wait time y checked on different scripts?

I have never had this problem before so I have no idea. Going to sleep right after this so ill reply in the morning. Cheers

1 Like

I believe you mean something like this?

while task.wait(plr.leaderstats.waittime.Value) do
    plr.leaderstats.Cash.Value += leaderstats.Amount.Value
end)

these names are completely made up and should be handled within the leaderstats script.

1 Like
local CashToGive = 100
while task.wait(however_Long) do
     for _, plr in pairs(game.Players:GetPlayers()) do
          plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value + CashToGive
     end
end

This is not tested. But this should be exactly what you’re looking for.

3 Likes
local Players = game:GetService("Players")

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
	
	task.spawn(function()
		while task.wait(120) do --Every two minutes.
			Cash.Value += 100 --One hundred cash.
		end
	end)
end)

Nobody really provided an example of how/where but you’d do it inside whatever script is handling leaderstats creation.

2 Likes

Everyone here didn’t really seem to read the prompt you gave. You wouldn’t want this to be a client-sided script as it wouldnt properly replicate to the server causing issues later on and leaving it vulnerable to exploiters. Enjoy Man :slight_smile:

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new('Folder', plr)
	leaderstats.Name = 'leaderstats'
	
	local money = Instance.new('IntValue', plr)
	money.Name = 'Money'
	
	-- How much money will be added each time
	local incrementAmount = Instance.new('IntValue', plr)
	incrementAmount.Name = 'IncrementAmnt'
	
	-- How long it'll wait to give money
	local WaitTime = Instance.new('NumberValue', plr)
	incrementAmount.Name = 'WaitTime'
	
-- Main function that gives out $ based on the players values
	local function upd()
		while true do
			task.wait(WaitTime.Value)
			plr.leaderstats.Cash.Value += incrementAmount.Value
		end
	end

-- Threading 
	local thread = coroutine.wrap(upd)
	thread()
end)
2 Likes

All of the provided solutions are server-sided. Also avoid using the parent parameter of the instance class constructor function as it’s inefficient. You’re better off setting the instanced object’s properties first before setting its “Parent” property last.

2 Likes

Keep in mind you can use compound assignments to save having to type a.Value = a.Value + numbervalue

e.g

plr.leaderstats.Cash.Value += CashToGive
2 Likes

thanks man. you really saved me.