Coroutines and their use cases

Hello everyone

I understand that coroutines are used to run multiple threads/tasks simultaneously inside of a script. However what I am wondering is:

  1. What actually are the practical cases where coroutines are used?

  2. Is it bad practice to use coroutines/ are there alternatives?

All answers are greatly appreciated
Thank you

To defer a one task from another, for example, if you want to run 2 while loops.

while task.wait(0.1) do
	print("loop 1 is on.")
end

while task.wait(0.1) do
	print("loop 2 is on.")
end

This code will only print “loop 1 is on.” Because while loops yeild, basically, the while loop has to finish/stop before the next block of code can be ran.

You can use coroutines to run them at the same time, heres an example:

coroutine.wrap(function()
	while task.wait(0.1) do
		print("loop 1 is on.")
	end
end)()

while task.wait(0.1) do
	print("loop 2 is on.")
end

Here we wrap it in a coroutine, making a seperate thread, so while the first runs, the code can continue.

They are useful if you want to run code at the same time, or if your having timing issues with like animations for example.

Its just a good tool to know. Test these blocks of code out and see what you get.

there is also other functions in coroutines, though im too lazy to go over them, read some documentation, might be more examples and explanations there.

Its fine to use them, of course keeping everything in 1 thread complicates everything less, but if needed, its fine to use coroutines, even encouraged sometimes.

There is task.spawn() but it doesn’t really differ to coroutine.wrap().

Hope this helps!

I just want to add that you can also accomplish funky stuff like this with coroutines. :woozy_face:

local CountDown = coroutine.create(function(Tm) -- Be cautious! Coroutines create / resume masks errors!
	print("Got the time! Resume to begin the countdown!")
	coroutine.yield()
	for CurrentTime = Tm, 1, -1 do
		print("Time left:", CurrentTime)
		coroutine.yield()
	end
end)

coroutine.resume(CountDown, 20) -- Got the time! Resume to begin the countdown!
while coroutine.status(CountDown) ~= "dead" do
	coroutine.resume(CountDown) -- 20, 19, 18, 17, ...
	wait(1)
end
local ArmNewKillBrick = coroutine.wrap(function(prt)
	prt.WarmingUp:Play()
	wait(2)
	prt.Armed:Play()
	prt.BrickColor = BrickColor.new("Bright green")
	prt.Touched:Connect(function(hit)
		...
	end)
end)

for _, prt in ipairs(Workspace.KillerBricks:GetChildren()) do
	ArmNewKillBrick(prt)
end

Pretty neat stuff you can do with coroutines if you know what you’re doing with them.

1 Like

This looks really trippy since I mainly only use the task library, lol. Do coroutines hold their place in the function when it hits a coroutine.yield()? Does this work with threads created with task.spawn/task.delay/task.defer?

I’ve never used task.spawn, task.delay nor task.defer so I couldn’t say.

Yeah, coroutine.yield yields the thread until it is resumed (via coroutine.resume, which isn’t limited to just coroutine.create).

If you wanted to test this (as I don’t have access to Roblox Studio atm), you could try…

local thread = nil
task.spawn(function()
	thread = coroutine.running()
	print("I'm going to yield now!")
	coroutine.yield()
	print("I had resumed!")
end)

task.wait(1)
coroutine.resume(thread)

It should output I'm going to yield now! first, followed by I had resumed! thereafter.

1 Like

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