Best method for multithreading?

Hi, I’ve been wondering what multithreading functions are the best overall for factors such as performance.

Those I know of are:

spawn(function()

task.spawn(function()

task.defer(function()

coroutine.resume(coroutine.create(function()

Which one of these, or any other method, is the best for creating multiple threads in a script?

Depends on your use case but in general task.spawn is the best one. Coroutines are more flexible if you store the thread hough.

Don’t forget about:

coroutine.wrap()
1 Like

coroutine.wrap() runs quicker than task.spawn() since task.spawn() works through Roblox’s scheduler.
Found here and here

3 Likes

I believe there is a small difference between spawn() and coroutines. I think I have read that coroutines start immediately, while a spawn() will not. But we’re talking about a difference of a fraction of a second, so that would only matter in very specific use cases. I use spawn() and have never had an issue with it.

Edit: I wrote this before I saw the above post, but that is a good answer that basically says the same thing.

This isn’t asking for support with a script he’s already written, it’s a discussion about optimization depending on the method one uses to write it. Definitely not off topic.

2 Likes