What Is Coroutine?

Hello Developers! :wave:

I don’t understand coroutines at all. What are they, some use cases. And what are coroutine.yield, .resume, .wrap, .create and what do they do? If you have any videos or links send them my way. Thanks! :wave:

Roblox has a developer hub page for it: coroutine | Documentation - Roblox Creator Hub

2 Likes

A coroutine is essentially a pseudo-thread. Much like spawn(), it enables a script to run two or more pseudo-threads depending on the number of coroutines, meaning simultaneous(and asynchronous?) runs.

3 Likes

in monke works: you can run multiple lines of code in one script at the same time. something like spawn() but much more advanced.

A more simple way to say it, is it’s essentially a function that can run “in the background”. When a script is running, it goes through each line from bottom to top.

Lets say you want a BrickColor to change after 3 seconds, but the transparency in 5 seconds. You can’t just do:

local part = game.Workspace.Part

wait(3)
part.BrickColor = BrickColor.new(“Electric Blue”)
wait(5)
part.Transparency = 1

The first wait will yield the second wait. You could always do:

local part = game.Workspace.Part

wait(3)
part.BrickColor = BrickColor.new(“Electric Blue”)
wait(2)
part.Transparency = 1

but that’s inconsistent! Instead, we do:

local part = game.Workspace.Part

coroutine.resume(coroutine.create(function()
    wait(5)
    part.Transparency = 1
end))

wait(3)
part.BrickColor = BrickColor.new(“Electric Blue”)

or

local part = game.Workspace.Part
local testCoroutine = coroutine.create(function()
    wait(5)
    part.Transparency = 1
end)

coroutine.resume(testCoroutine)
wait(3)
part.BrickColor = BrickColor.new(“Electric Blue”)

Hope this helps! :slight_smile:

9 Likes

Ohhhh. Then what is a pseudo-thread or thread?

Roblox’s engine runs on a single thread, where code is executed at one line at a time. However, if you used a pseudo-thread, it is still running a single thread, but on, from what I know, stacks. If you run a script with a coroutine with a loop and a loop on the global scope, both loops will run approximately simultaneously. Only one loop will be ahead of the other though and never exactly simultaneous.

1 Like

Cool. But saying:


local part = game.Workspace.Part

wait(3)
part.BrickColor = BrickColor.new(“Electric Blue”)
wait(5)
part.Transparency = 1

Also works. So there’s no difference??

It allows you to run multiple things separately, as well as spawn()

Let’s say you have this loop:

for i = 1, 100 do
coroutine.wrap(function()
wait(1)
print("yes")
end)()
end

For each iteration, instead of waiting each time, it goes through each of them and runs them separately.
The code will run in 1 second instead of 100

3 Likes

Coroutines, or as I like to call them, Quarantines, are a thing in Lua where you can run two or more threads at the same time. I’ll link some resources for you, since they are a bit difficult at first.

TheDevKing Video on Coroutines
DevHub Intro to Coroutines
DevHub Coroutine API

1 Like

In that example, the BrickColor will change after 3 seconds, and then wait another 5 seconds to change the transparency adding up to a total of 8 seconds. But with coroutines, we can wait for 3 seconds to change the BrickColor while also simultaneously waiting 5 seconds to change the transparency.

1 Like