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

Difference between coroutines and spawn()?

Asked by
D3LTA_Y 72
5 years ago

Hello, i'm trying to find the difference between a spawn() thread and a coroutine, are these two basicly the same thing when starting?

2 answers

Log in to vote
2
Answered by 5 years ago

Hi Lunatic,

Compare and Contrast Spawn and Coroutine

Spawn and coroutine are both the same thing, just that with a thread like spawn functions, you can't run any special functions on them like yielding them and resuming them and such. They can only run two blocks of code simultaneously and don't have special functionalities.

Which One I Think Is Better

Still, I think spawn is more useful because it's easier to implement and it's coming from the Roblox engine rather than just the Lua language, so it probably has fewer chances of causing errors and increasing latency.

Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

1
Under the hood, spawn() just creates a new coroutine, it's just that the lack of coroutine.resume() and coroutine.yield() allow Roblox to optimize yielding functions better and run more things in parallel. Neither are currently buggy, but if a bug shows up, it's gonna either show up in both or in spawn() brianush1 235 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

There is actually a slight difference in the performance of each event.

coroutines are less likely to stop under normal circumstances. For example if the script hosting the routine is disabled, it's likely the coroutine will keep running. Also once the coroutine is called it runs immediately.

In contrast, spawn is definitely better because it ends when it logically should and you don't need to keep that close of an eye on it. Downside is rather than running immediately when called, spawn() will make a new task that won't be run in the task scheduler until a script calls the wait service by using wait() so something like this would run instantly:

spawn(function() end) wait()

A better method than either of those is simply delay, this works exactly the same as spawn but with no need of the wait service(if you're not the kind of person who uses wait()). You can simply call:

delay(0,function() end)

and that would run instantly in a single block. You could also set delay times which is a good plus.

Answer this question