What are coroutine's, how do they work?

Hello! I’m trying to learn scripting! I head of this thing cald coroutine’s and I wonder how I can use it, what for, and it broken down so I can understand it!


Api Page: coroutine
Devforum Page: Coroutines - When and how to use them

You really should have looked this topic up before making a thread about it. :slight_smile:

7 Likes

Wait I’m a little confused, the Devforum Post Is hard to understand.

this code in specific:

local Write = function(Message)
    print(Message);
end

local Thread = coroutine.create(Write);
print(Thread); --// thread: <hexadeciamal memory adress>
coroutine.resume(Thread, "Hello, World!") --// Hello, World!
1 Like

I have also linked the api page if you are having touble on that too.
If you have ever coded in java/js you might know thread.sleep(miliseconds).
Its kinda like that.

1 Like

Let me explain the code to you

local Write = function(Message) -- This is just a normal function
    print(Message);
end

local Thread = coroutine.create(Write); --Creates a new thread
print(Thread); --// thread: <hexadeciamal memory adress>
coroutine.resume(Thread, "Hello, World!") --// Hello, World! -- Resumes or runs the thread

Coroutines can be useful if you wanted to pause/resume a function when needed as well as to run two functions or pieces of code at the same time

(Sorry if the formatting is bad, I’m on phone)

2 Likes

Yes, exactly. It is very useful for minigame scripts if you need to wait on something before continuing the script.

2 Likes