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 11 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 11 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:

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

Answer this question