If I have a script that contains two functions and one gets called while another is running, will the running one get cut off?
If I have a script that contains a while loop and a function, will the while loop get stopped if the function is called?
If I have a script that contains two functions and one gets called while another is running, will the running one get cut off?
If I have a script that contains a while loop and a function, will the while loop get stopped if the function is called?
Well yes and no. You can create a new thread which will run at the same time as your first thread through the coroutine API or spawn.
When you call a function think about it as if those lines were directly inserted where the function is called. If you wait in your function you’ll also yield the thread that called it.
I don’t believe the other will ever cut off, unless you are waiting for a specific response from the function
Like
If thingfunction() then
.....
Well, yes, there are different ways to do this.
delay(3, function() --Executes function after 3 seconds.
--CODE
end)
spawn(function() --Executes a new function in about 1/60 of a second (build in wait).
--CODE
end)
coroutine.resume(coroutine.create(function() --Executes immediately.
--CODE
end))
coroutine.wrap(function() --Same thing here.
--CODE
end)()
All these things will run multiple lines of code at the same time, just figure out which one works best for whatever you want or are trying to do.
No, code cannot run simultaneously on the same machine. When you yield (with wait
, coroutine.yield
, et cetera), the code (thread) that is queued to be executed next in the thread scheduler begins running (is resumed) until it too yields, where the next thread is resumed, and so on.
No code actually executes simultaneously, as Roblox does not support multi-threading. Read more about the thread scheduler here.
Is there a article or anything about Roblox not supporting multi-threading? Why is that? I’m actually curious.
All your Lua code is being executed in a single thread (VM’s OS thread), there is no true multi-threading, so two lines of code in your script are never executing simultaneously. Code is executed line by line without interruption until it reaches a function that yields, which includes wait() statements and functions marked as “yield function” in the API docs. When one of these functions is encountered, the task manager can stop execution there and pick up running code in another part of your game. It’s common to call these execution contexts “threads” since–while not OS threads–the intent is the same. You get one of these threads any time you run a coroutine, or a function Connected to an event is called, and you have one per Script or LocalScript that’s running.
There is one special case to be aware of, which is WaitForChild(), because it’s sometimes a yield function and sometimes not. Internally, it calls FindFirstChild() and does not yield if it finds what it’s looking for. But if the instance is not found, it yields until it is or until a provided wait time has expired (if specified).
Lua code runs from top to bottom. If you call a function in a while loop, then the while loop won’t continue “looping” until the function finishes.
local function somefunction()
wait(3)
end
while true do
somefunction()
print("hello") --this will run after 3 seconds
print("world") --the loop won't repeat again until this function finishes
end
The same applies for calling a function inside another function:
local function GrabPlayers()
return game.Players:GetPlayers()
end
local function LoadData()
print("Loading data...")
local plrs = GrabPlayers()
print("Grabbed players!") --this runs once GrabPlayers() finishes
end
If a remote event is called with a connect function will it immediately jump from wherever it already was to that portion of the code?
No, because there is no pre-emptive multithreading. Your remote event’s listener function gets queued up in the task scheduler. The incoming remote event can’t interrupt the currently-executing code. The multi-threading model is co-operative, so execution only changes to a new thread when the current one yields or completes (reaches the end of the function).