• Register a free account to view all forum content. Click here to create your own account.

function timing?

Joined
Aug 18, 2018
Messages
15
Roblox
PLayerPerpetual
I probably got the name wrong.
So I was wondering, if had a script that ran a function, and then had: print("poop")
Would it wait for the function to finish before it printed/continued the script, or would it do print after the function finished?
 
Joined
Jun 4, 2018
Messages
240
Ratings
11
Roblox
proclet
Normally, the game waits for a function to be executed, then moves on to the next line of code (scope).
However, you can use spawn to force the game to execute a function at the same time with the rest of the code (without yielding).
For an in depth answer, check CallMeKY's links.


spawn
void spawn(function callback)
Runs the specified callback function in a separate thread, without yielding the current thread. The function will be executed the next time Roblox’s Task Scheduler runs an update cycle. This delay will take at least 29 milliseconds but can arbitrarily take longer, depending on the target framerate and various throttling conditions.

Consider the following two scripts :
Code:
function waits ()
    print ('test no.1')
    wait(2)
    print ('test no.2')
end

waits()

while wait() do
    print("Hello world!")
end
This one will yield. The system waits for the function to end, then move on to the next scope.
and
Code:
function waits ()
    print ('test no.1')
    wait(2)
    print ('test no.2')
end

spawn (waits)

while wait() do
    print("Hello world!")
end
This one will execute the function at the same time with the rest of the code.