Coroutines vs task?

I usually use task.spawn to make a new thread, but sometimes coroutine.wrap. My scripting inconsistent made me realize, what’s the difference between task.spawn and coroutines?

4 Likes

Coroutines you can stop and start with the script, but task.spawn will just run until all the code has been executed.

16 Likes

There isnt really a difference, but spawn() has a built in wait() into it before the code within it executes. Whereas coroutine executions are instantaneous.
And like @Orbular3 said. You can stop and resume a coroutine. You cannot with spawn()

6 Likes

Any difference performance wise?

spawn() (or the new task.spawn()) will fire code immediately.
spawn() is deprecated, and task.spawn() should be used to enhance performance

overall, there isn’t a performance difference, coroutines fire code alongside a thread, task.spawn does it with the engine sheduler.

7 Likes

Late reply, but is this true? Because you can also stop a new thread of task.spawn with .cancel

edit: Wait no, you can pause a coroutine and continue it later but a task function no

1 Like

Yes, my solution is wrong.

The task library actually uses coroutines and functions like task.spawn are just abstractions of the coroutine library.

In other words task and coroutine are the same, except task simplifies the process in exchange for less control - at least that’s what roblox says:

In most cases using a function is sufficient, but for more advanced cases it’s recommended you familiarize yourself with the coroutine library.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.