In a repeat task.wait() until true
The script runs a task.wait() until something is met but this is not good for memory.
Now can you explain how exactly does a :Wait() works?
How does the computer or the hardware know what :Wait() is?
In a repeat task.wait() until true
The script runs a task.wait() until something is met but this is not good for memory.
Now can you explain how exactly does a :Wait() works?
How does the computer or the hardware know what :Wait() is?
So RBXScriptSignal:Wait() yields the current thread until the event is triggered. It is better practice to use this.
Keep in mind though that this does not have a timeout parameter. You should be certain that your event will fire or it will continue to exist until the end of time or until you collect garbage.
Edit 2: You may alternatively find it useful to just add a new function entirely for that specific event. It could save you a headache in case something unexpected occurs. Quoting buildthomas here:
“prefer using structures of :Connect(…) in the cases that you can’t be sure that the event will actually fire, rather than sequential sections of code with :Wait() checkpoints, and then save the connection you get back from Connect, and disconnect it when you need it to be disconnected.”
:Wait() is by far better because the script just pauses, meanwhile a repeat is constantly using processing power to check the conditions to continue
How does a script “just pauses” and what keeps track of it if the script itself is paused?
Task scheduler will halt execution of the thread and then resume execution after the specified event is fired.
The idiom repeat task.wait() until
is known as polling and is considered code smell. RBXScriptSignal
s already provide a more accurate and performant solution so you should always be using events wherever possible.
As for :Wait()
, my personal advice would be to steer clear from actively yielding your code because it introduces the issue of having your code unintentionally halt or hang.
A more robust way would be to use the given :Connect()
and :Once()
methods for RBXScriptSignal
s and separate your code into event-driven functions. Here’s an example I gave on another thread:
How do we know :Wait() itself isn’t polling?
A simple google search could of answered this for you:
I’ve read that already. Don’t be snarky.
I was already wondering if :wait() uses polling itself or not.