Do while loops for players still run after they've left?

After a few hours of a server being active, I get chat lag in my game and I think I have found the problem but I’m not sure.

I have a while loop when the player joins the game to give them cash every second but I want to know if that loop still runs when the player has left.
If so, how can I break the loop when they leave?

It depends on your code. You’re responsible for creating the break conditions for your loop. What does your loop look like?

Couldn’t you just add an if humanoid to your code?

local Players = game:GetService("Players")

while task.wait(1) do 
	if Players:FindFirstChild(player.Name) then 
		--give them cash 
	else 
		break 
	end
end

So I would do it like this?

game.Players.PlayerAdded:Connect(function(plr)
	while task.wait(1) do 
		if game.Players:FindFirstChild(plr) then 
			--give them cash 
		else 
			break 
		end
	end
end)

Yes, just instead of using plr inside FindFirstChild do plr.Name. Basically you just need to break the loop if the game is unable to find them.

1 Like

It’s kinda like this

game.Players.PlayerAdded:Connect(function(plr)
	while wait(1) do 
		plr.leaderstats.Cash.Value += 1
	end
end)
game.Players.PlayerAdded:Connect(function(plr)
	while task.wait(1) do 
		if game.Players:FindFirstChild(plr.Name) then 
			plr.leaderstats.Cash.Value += 1
		else 
			break 
		end
	end
end)
1 Like

Store the connection in a variable and when the player leaves just disconnect it e.g.:

local connection = game.Players.PlayerAdded:Connect(function(player)
end)

connection:Disconnect()

Or, yeah just check if the player is still in the game and break the loop if they aren’t

1 Like

Have you tested that?

This is an incredibly ineffecient method. You don’t even have to check if a player has joined the game. You can just create a single while loop that will iterate through a table of the players currently in the game. Also, your while loop won’t account for potential lag. You can connect a function to RunService | Roblox Creator Documentation instead. You an do it like this:

local Players = game:GetService("Players")

local CASH_ADDED = 1 -- setting for the amount of cash added after each delay
local CASH_DELAY = 1 -- setting for the delay in seconds until cash is added

local t = 0 -- the elapsed time
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	t += deltaTime
	if t >= CASH_DELAY then
		t -= CASH_DELAY
		
		for _,player in pairs(Players:GetPlayers()) do
			player.leaderstats.Cash.Value += CASH_ADDED
		end
	end
end)
6 Likes

Never thought of doing that, thanks!

Interesting way of doing this. Don’t think I’ve ever seen it before!

Well, as I stated earlier, the reason I do it like this is to account for lag that can potentially happen.

If the server lags then it will throttle. With a while loop this means that it will resume once the server resumes. However, all the seconds that passed, even if it was only a single one, will pass with players not being awarded the cash they are entitled to for their time spent. However, using RunService | Roblox Creator Documentation, the deltaTime parameter for the connected function will return the time that has passed since the last iteration. If lag happens, then this can go from it normally ranging from 0.01 to 0.04 seconds in-between each iteration to pretty much anything larger. It all depends on how long the server is throttled.

Given a theoretical scenario where 10 seconds passed since the last iteration, our t value will now be at or above 10. Now, as I’m sure you noticed, it doesn’t immediately remove 10 on the step. It will instead steadily reduce this by 1 until it is less than 1.

1 Like

how would this work if you wanted a specific player to get cash after spending 10 minutes in-game? it seems with this method it’s giving everyone cash indiscriminately. I would like it so players can’t hop from server to server and get cash in the “server’s” 10 minute time frame, but it be 10 minutes starting the moment that specific player joins the game.