Any better alternatives to using task.spawn()?

my game heavily relies on using task.spawn() with it being in just about every script. (keep in mind this was when i was sort of new to actually optimizing games good, so i poorly optimized this)

the main use i use for it is executing code without yielding any code after it, i used to use coroutine.wrap() for this back when i didnt know about task.spawn(). however after learning about task.spawn() i (painstakenly) went through every script and replaced coroutine.wrap() with task.spawn(). this seemed to fix a bunch of lag and delay issues but arised some new lag issues, mainly random lag spikes every now and then.

i don’t want to go through all of my scripts again and replace task.spawn() with another thing only for more problems to show up again, so i came here to ask if anyone knows a better solution for executing code without yielding any code after it.

(example:)

Short answer is no, there’s no alternative that performs better than task.spawn in your case.

Long answer is… complicated, but is fundamental in programming.

In your case I would advise taking a look at your system as a whole. Each task.spawn is quite literally a “task”. The more tasks you have operating at once the more complex situations are going to happen, which appears to be what your case is like.

Take a long look at your system, the problems aren’t gonna be obvious, but if you open your mind and consider “what can happen at the same time”, you’ll start to understand the systems you have put in place yourself just a little bit better.

That’s really all there is to this. If you’re having errors pop up and your system mainly comprises of task.spawns, then you should focus on the cases where two, three, or more tasks are happening at once.

The alternative is to rebuild your systems to where you have a flow in your system (that is, an action happens and you’re able to easily read and follow through to the end of that action in your scripts), as well as reducing the amount of random tasks being given.

In the example image provided, you really only need 1 spawn (or a task.delay) and that’s just to clean up the VFX stuff.

Hope this helps a bit!

1 Like

thanks! ill try to go through my scripts and remove some unnecessary ones.

1 Like

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