Using spawn() or coroutine to execute code faster

Is it good to use spawn() function and coroutines to execute the code faster by avoiding yielding? Recently, my playerdata folder loads a lil bit later than when I had nothing in the code. (Btw I am using one general code for all functions)

This has already been asked before: here

That’s not the same question. Would you please re-read the question again?

It is.


It talks about the exact same thing. In this case, coroutine would be faster to use.

No, my question is : Is it good to use spawn() and coroutines to avoid the code from yielding?

Yes, that’s why they exist. It doesn’t really matter which one you use though.

The code won’t yield unless you ask it to yield. Nobody can tell you something is good in 100% of cases - it’s quite specific to what you’re trying to do.

Unnecessarily using them can be confusing to debug issues and can present race conditions if used improperly.

I’m using ALOT of loops so, sometimes, the last lines takes some time to execute.

There’s a difference between yielding and something just taking time.

Coroutines only allow another task to take over if they themselves yield. If you create a coroutine to contain your slow code, it won’t speed anything up if that code doesn’t yield, as the non-coroutine tasks won’t execute until the coroutine yields.

Lua on Roblox doesn’t run anything in parallel, it simply switches from one task to another, but it only switches when the current thread yields.

1 Like