Coroutine.create/coroutine.wrap or task.spawn()?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

So yeah. Basically as you might know Roblox released the “task” library some months ago.

  1. What is the issue? Include screenshots / videos if possible!

My problem is that im confused on whats the difference between coroutines and task.spawn… I Couldnt find anything helpful when googling this question. I just found old stuff talking about the old “spawn()” function

At the moment. I use this to replace the old “spawn()” function or sometimes just the coroutine.create thing…

Any help would be helpful. Since im kinda confused on if i should switch to task.spawn and delay :grinning:

function SPAWN(FUNC)
	return coroutine.wrap(FUNC)()
end
1 Like

The difference is pretty easy to summarize as coroutines are more in-depth/feature rich, so they are almost exclusively used over Spawn. When you Spawn a thread, there usually is a delay of up to 30ms (citation needed) before that thread starts to run as well; minute to the average player but a huge difference for programmers.

If you’re looking to create simultaneous threads, I always recommend coroutines above all else:

coroutine.wrap(function()
	-- Stuff
end)()
1 Like

This isn’t true anymore. spawn() had a delay because of an internal wait(), but task.spawn resumes immediately. task.defer is more similar to spawn() because it waits until the next resumption cycle before continuing.

Coroutines give you more control over a thread, however I’ve heard they can make stack trace information a bit weird. task.spawn() is typically used if you need to spawn a thread IMMEDIATELY and you don’t need much control over it.

I typically use task.spawn() / task.defer(), but if you need the controls that a coroutine gives you (like status, resume, yield) then use coroutines

5 Likes