Thread and Scopes

Basic Lua knowledge

by LadyCelastia

Author Avatar

Introduction to Threads

Have you ever tried to run 2 while loops in a script and only 1 of them ran? It's because a while loop stops everything below the loop chunk from working. Why? It's because the while loop and the rest of the script are on the same thread. A thread runs from the first line to the last line. But if you put a while loop in the middle of the script, the while loop will go on forever (Assuming that you don't have any mechanic to break the loop) and the rest of the script will never be executed. How do we solve it?


We can solve the problem by creating a new thread within the script (Or you can say, thread). But how? Is it possible to create a thread within a thread?

Yes, it's possible. There are multiple methods to create new threads, we'll cover them one by one.


    while wait(1) do
        print("This loop will run!")

while wait(1) do
    print("This loop will also run!")

Spawn(f) is the most basic way of creating a new thread, it spawns a new thread that runs at the next frame while not stopping the script itself. You can use it for many things!


delay(1, function()
    print("This will print 1 second later!")

print("This will print first!")

delay(Int, f) works like spawn(f). But it will wait an amount of time (Determined by Int variable) before executing, while not stopping the script.


    print("This will print first!")

    print("This works just like the above!")

print("This will print later!")

Coroutine has an unique property. Instead of executing the wrapped code in the next frame, it executes them immediately. Can be helpful in certain circumstances.

Coroutine Advanced

Coroutine has way more methods and functions than spawn(f) and delay(Int, f).


local MyFunction = coroutine.wrap(function(Variable)
    print("I am called! " ..Variable)
    local NewCallVar = coroutine.yield()
    print("I am called again so I'm resuming! " ..NewCallVar)

Put a coroutine on yield until it's called again.


local function NormalFunction()
    print("I am called!")
    print("I am now dead!")

local NewThread = coroutine.create(NormalFunction)

print(coroutine.status(NewThread)) -- suspended



print(coroutine.status(NewThread)) -- running


print(coroutine.status(NewThread)) -- dead

Returns a coroutine's status as string. Return suspended if the coroutine is on yield or not resumed. Return running when the coroutine is running. Return dead when the coroutine isn't on yield nor running.


local IAmAFunction = coroutine.create(function()
    print("I am called!")
    print("I am now dead!")

print(coroutine.running()) -- Thread: XXXXXX



print(coroutine.running()) -- Thread: YYYYYY

coroutine.yield(coroutine.running()) -- This works!

Returns the current running thread.

Conclusion to Threading

There's endless possibilities with coroutine, spawn(f) and delay(Int, f) ! Make sure to explore them yourself!

Introduction to Scope

Scope is useful for creating a chunk of code that doesn't affect the rest of the code. For example, variables in scopes don't carry to the main code.

How to create a scope

It's actually very simple to create a scope. Just do:

    print("This will print first!")
    print("This is a new scope!")
    local MyVar = "I will not be carried to the main code!"
print("This will print 1 second later!")
print("I am outside the scope!")
print(MyVar or "Doesn't exist!") -- Doesn't exist!
print(MyVar) -- nil

Conclusion to Scope

Scope is useful for code that doesn't have anything to do with the main code. To be honest, it doesn't have much uses.

And that wraps it up! Thanks for viewing my community tutorial! Please leave a comment and a rating! :D

View in-game to comment, award, and more!