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

What is Delay and Lua thread? Roblox Developer doesn't say about these things

Asked by 4 years ago

delay(3, function() --Executes function after 3 seconds. --CODE end) What is Delay? i tried to find this but no results and i read about yielding script on roblox developer, it says: " Yields

This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. " What is Lua thread?

0
Yield meaning makes me confused too ;-; Creeperdo123 -17 — 4y
0
Im actually working on a project using these things, and I;m still new to them too Houlardy642 28 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

The simple answer is that yielding stops a thread for the duration of the yield. You can think of a thread as a "script." A different thread is a different "script." One example of a yield is wait. It stops the current thread (i.e. current script) for the time provided as the argument:

print("hi")
wait(3)
-- three seconds after hi prints, bye prints
print("bye")

delay just starts another "script" and puts a yield at the front. Since the code is run in another "script," the wait/yield doesn't stop the current thread (i.e. current script). For example,

delay(3, function()
    print("bye")
end)
print("hi")

does the same thing as the first example script I gave, but it allows you to put the bye first without preventing print("hi") from running since the print("bye") and yield are in a different "script." This is a rather simplistic example, but it gets the idea across.

I realise that this description is not entirely accurate in terms of terminology, but I think it presents the best way to think about it. I hope this helps. Have a great day scripting!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Delay is very useful and simply delays a function by X seconds without yielding your script.

So, what does this mean? What do the terms function and yield mean?

A function is basically a Block or Chunk of code.. several lines if you will that can normally be "used" several times.

Yielding can be described as pausing, in our case, delay sort of "yields" the function you provided by the amount of time you gave. 

These are simple definitions however, not exactly accurate but may help you understand more.

delay(3, function()
    print("Hello")
    print("World")
end)
print("!")

The above will print "! Hello World" in your output instead of the ! being last like the prints are written, the first two prints are delayed by 3 seconds which cause ! to be printed first, showing you delayed your code by 3 seconds WITHOUT stopping the code. If you were to use a wait(3) without delay, nothing in your script would progress until those 3 seconds are complete.

The lua thread is a bit complicated, but for now I suggest thinking of it as a service running all your scripts. If you use delay, you create a thread that is delayed while your current thread (the script being used) still continues. If I have confused you, please feel free to comment below any question!

0
I wrote this post slowly, I do not think AetherProgrammer provided a bad answer, I simply was in edit mode while they published the answer! alphawolvess 1784 — 4y

Answer this question