Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Could someone explain coroutines to me, and maybe give an example?

Asked by 10 years ago

If you could, that'd be great! (I want to know to further my knowledge and in the end help more people here. If anyone knows how to explain it, please do!)

1 answer

Log in to vote
4
Answered by 10 years ago

A coroutine is essentially a new thread within a script. This allows you to have multiple loops at the same time and to continue a script while also yielding, amongst other things. For example:

local loop = coroutine.create(function()
    while true do
       wait()
       print("Hello")
    end
end)
coroutine.resume(loop)
while wait() do
    print("World!")
end
--Both loops here would run simultaneously 
0
Thanks for this, and ESPECIALLY for the link. You well deserve the Community Mod rank. xDeathBear 90 — 10y
Ad

Answer this question