Is it possible to call task.spawn() or other task() schedulers too many times?

In a game I am working on, I used that task.spawn() function to speed up the setting up and booting up of some tycoons. Before using task.spawn(), they all tended to boot up linearly, and that would take 1-3 minutes when the server first boots up - not super ideal.

So I started using task.spawn() to speed that process up - and it worked! Except that sometimes, one or two out of the 16 or so tycoons would not be set up right.

I thought this was something with the tycoon kit I was using, but then I discovered the error in the script that set them all up. Apparently, for the last one or two tycoons, it appears that any code within the task.spawn would not be executed after task.spawn() was called about 16 times within a very short period of time (within a for loop). I cut this down to calling it 8 times within the same period, and so far, no new reports about the issue.

This was an extremely rare glitch in the game but it was still a glitch, nonetheless.

Is it possible that task.spawn() or any of the other task schedulers can be called too many times in a certain time period, and will they time out and ignore the code within them?

1 Like

What are you doing that takes multiple minutes to boot up? That’s insane. And it’s worrying that whatever you’re doing has inconsistent behavior.

I can’t exactly answer your question, but:

If task.spawn truly sped up the process, then it’s most definitely an issue with the tycoon kit you’re using; that kind of improvement only comes if the script to boot the tycoons has any yielding in it. task.spawn is still ‘linear’ – otherwise known as single-threaded; if you want multiple tasks to truly run at the same time, I suggest looking into parallel Luau, it’s pretty wild stuff.

1 Like

There’s a funny “yes and no” answer to this thread - as in, no it’s not possible to pseudothread with the task library too much, but yes it’s also possible. The difference in those is between an actual limitation and a code-related issue (e.g. bad practice on your part).

As some of the above posts mention, if using task.spawn is supposedly speeding up the setup of your tycoons, this isn’t an issue of scheduling but rather that your setups are yielding too much. You should only ever yield when it’s appropriate to do so or is an unavoidable part of whatever functions you’re calling. Relying on spawn to speed up your setup is a code smell.

There shouldn’t be any reason why your tycoons are taking even the minimum amount of time you specified in your range, that being 1 minute. They should be ready the moment clients are able to connect into the server. I do advise you to perform a deeper investigation on your code.

This isn’t a problem that Parallel Luau would resolve, nor would be needed for. That’s an overkill solution. The root problem should be addressed which is too much yielding.

1 Like