I made an entire thread on how to avoid wait() and why. There is, and as I say in the thread, wait() does not belong in production codebases at all.
Whatâs not made clear anywhere above is why you are asking how to write code without using wait
. You seem to think there is something bad about âsleeping a threadâ when being able to suspend and resume coroutines is actually a huge and desirable benefit of having them in the first place. You keep mentioning things like âexecution freezingâ and âneeding things to happen in betweenâ, and all this just suggests to me is that youâre not using coroutines effectively or correctly. Of course you donât want to block your main thread, but no one is suggesting (or doing) that. Wait(n) statements go in your coroutines and event handlers (functions passed to event.Connect()), not your main game loop!
Just to be clear, creating a coroutine that uses wait(n)
is a correct, efficient and desirable way to do something like a long fixed delay or periodic, infrequent execution of a block of code. The next best way would be a timestamp comparison inside an existing game loop thatâs executing periodically on something like RunService.Heartbeat. For and while loops should generally be avoided for long-running game loops, since they can stop unexpectedly (e.g. if some error causes a script execution timeout) with no way to resume.
This discussion is only about the wait(seconds) variant. The no-argument variant of wait() is a different beast, and I donât think anyone will contest that there is always a better option for that one.
Itâs there in the OP? My reason for writing this is scattered everywhere.
Iâm not using coroutines. Itâs explicitly stated in the OP that Iâm using an event-based system. Iâm fully aware of how to use coroutines. This thread pertains to abdicating the use of wait(n). Suggesting coroutines initially would been fine, but that assumes I have any in the first place. Iâm asking how to write codebases without wait(n), given coroutines or not.
See the responses on this thread and my reply to each one of them. If it wasnât suggested, a solution would be marked now. See above as well. Putting a wait in my event handlers doesnât solve my issue.
I do not want to use wait(n). I donât know how many times that can be reiterated in my responses to this thread.
Iâm just going to flag this closed. I donât feel like itâs yielding anything productive anymore.
This thread seems beyond redemption at this point. Rather than continue arguing about it, lets just put our keyboards down and let it close.
Requested by topic creator.