How do i do wait() in a loop without slowing it down?

heya! I have this loop changing stuff in players guis which is basically:

for i, v in pairs(game.Players:GetPlayers())

im wondering if theres any way i can do wait() and allow it to continue with the next person?

2 Likes

what exactly do you mean by this? so Like wait for something but allow to code to function without yielding?

If so, you can use task.delay

This can be technically done by either coroutines, parallel lua, the task library.

Coroutines are more complicated to use, parallel lua isn’t really necessary (unless you need that performance). So for the sake of simplicity, let’s use the task library.

for i, v in ipairs(game.Players:GetPlayers()) do
	task.spawn(function()
		task.wait(5)
		--@ do something
	end)
	
	--@ this will pass down to the next player
end

There are also other options like task.delay() (as @DasKairo mentioned), task.defer()

1 Like

thank you. I was aware of coroutines, etc, but I did not know how to implement them.

task.delay() did not work for me. it for some reason stopped the entire loop. probably a problem on my end.

spawning a task works perfectly for me.

1 Like

Did you format your task.delay() like this?

for i, v in ipairs(game.Players:GetPlayers()) do
	task.delay(5, function()
		--@ do something
	end)

	--@ this will pass down to the next player
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.