Short answer: yes
Long answer:
Depends on what you mean by in parallel. If you define parallel as two separate contexts running independent of each other, then yea. If you define parallel as code running simultaneously, then no.
task.spawn will immediately yield the calling code and run the function you used in task.spawn. No other code will run until that function completes or yields (e.g., task.wait). So in a sense, it may look like two pieces of code are running separately whereas in reality, they are more so passing control back and forth to each other.
You can see the behavior with the following script
task.spawn(function()
print("Inside task.spawn")
for i = 1, 10 do
print("spawn:", i)
end
end)
print("Outside task.spawn")
for i = 1, 10 do
print(i)
end
If you run this, you’ll notice that the loop inside task.spawn will run to completion before “Outside task.spawn” even gets printed.
Inside task.spawn
spawn: 1
spawn: 2
...
spawn: 10
Outside task.spawn
1
2
...
10
However, if you introduce task.wait inside the task.spawn function
task.spawn(function()
print("Inside task.spawn")
for i = 1, 10 do
print("spawn:", i)
task.wait()
end
end)
The output should look something like
Inside task.spawn
spawn: 1
Outside task.spawn
1
2
...
10
spawn: 2
spawn: 3
...
Because you called task.wait, you sort of give up control of that thread and allow other threads to continue running until they complete or yield.
I make this distinction because Roblox does have an API for multithreading which actually does have code running simultaneously.