I have been studying roblox threads for quite some time now but I still have a lot of questions. In this case I would like to know the following:
In the manual it says this about spawn:
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
As if it has a built in wait. Many in the forum say that it is better to use coroutines. But when we use coroutines we typically do this:
coroutine.wrap(function ()
-- some code
wait()
-- some code
end)
So using spawn and coroutines with wait are exactly the same?
-- this
coroutine.wrap(function ()
wait()
-- some code
end)()
--is exactly the same as
spawn(function ()
-- some code
end)
If you think about how threads work, they’re not the same, as the coroutine is guaranteed to run without causing any code delays (whatever you put inside it). Unlike the spawn function, which always wait a tick, also causing delay to your script.
local start = os.clock()
spawn(function()
print(os.clock() - start) --Got 0.03 to 0.04 seconds
end)
local start = os.clock()
coroutine.wrap(function()
wait()
print(os.clock() - start) --An average of 0.03 to 0.04 seconds
end)()
My bad for my slight misinformation! You were right (me saying that it causes delays in the entire script, looks like I misunderstood the point of spawns), although coroutines never cause a delay in running the thread, spawns do.