How to use coroutines

This is a repost because I put in wrong area at first and didn’t notice. If you see my other post in Community Resources just flag it.

Hello Robloxians! This is tutorial on how to use coroutines/threads. These are very useful from using while loops without delaying your other code in your script. To calling something over and over with a while loop without yielding your other code. Just treat these threads a another script running.

Please tell me if I miss anything

Steps

  • Make a new script in ServerScriptService if it is a LocalScript put it in StarterGui
  • This can be a LocalScript or ModuleScript or ServerScript
  • Have fun!

Entries

Entry 1.

Make your first coroutine!

local TestCoro = coroutine.create(function() --// This creates a new thread (script)
    --// Great Job!!
end)

Entry 2.

You can now assign code in it

local TestCoro = coroutine.create(function()
    --// Anything that is run in this thread will be run in another thread (script)
    while true do wait()
        print("This is my first coroutine!")
        --// Now you can call the thread
    end
end)

Entry 3.

But wait we need to run/resume it and maybe yield after a certain amount of time

local Amount = 0

local TestCoro = coroutine.create(function()
    while true do wait()
        if Amount >= 10 then
            --// Do not call break call this below since it will keep the coroutine running but not the loop which in some cases crashes your studio
            coroutine.yield() --// This yields/breaks the loop. Yield just means makes it wait
        end
        Amount += 1
        print("This is my first coroutine!")
    end
end)

coroutine.resume(TestCoro) --// You need to run/resume the new thread (script you made) and if it is yielded it will run again with this
--// This print will run whilst the while loop is in a different thread (script)
print("This Script Was Not Stopped!")

Entry 4.

Now to check if the thread is yieldable

local Amount = 0

local TestCoro = coroutine.create(function()
    while true do wait()
        if coroutine.isyieldable() then --// Checks if the thread can be yielded
            if Amount >= 10 then
                coroutine.yield()
            end
            Amount += 1
            print("This is my first coroutine!")
        else --// If the thread cannot be yielded then
            warn("This thread can not be yielded!")
        end
    end
end)

coroutine.resume(TestCoro)

There you go you have a working coroutine! But there is more.

Other Things

local Coro = coroutine.create(function()
    while true do wait()
        print("Hello")
        coroutine.yield()
    end
end)

coroutine.resume(Coro)

print(coroutine.running()) --// Prints the ID of the thread that is running
print(coroutine.status(Coro)) --// It prints the threads status try it above the coroutine.resume and below

local Wrap = coroutine.wrap(function()
    while true do wait()
        print("Hello")
        coroutine.yield()
    end
end)() --// Add the two () to call the wrap since it calls create aswell
--// But if you are calling it with the variable then remove the () and it will run
Wrap()

--// This calls a function that plays the coroutine.wrap function everytime it is called!

You can assign variants to coroutine.yield() but I do not use that ever and I think it is not useful since the loop stops no matter what.

6 Likes

I know this is a repost of my other one but I never noticed till today that other post was in Community Resources so I told everyone to flag it and here is the better one with a suggestion by a guy name @LightningLion58 who showed me how to explain it better