I’ve found myself in a scenario where I want to wait for an event to happen in order to continue an infinite loop. My current plan is to use a BindableEvent and wait for the event to be fired.
It got me thinking if I should just use wait() instead. What is more efficient? I’d have to assume waiting for the event to be fired is more efficient. What happens when you use :Wait() on an event? Perhaps someone has some knowledge about what’s happening under the hood?
Also how does wait(n) work? I think I’ve heard that it just calls wait() until n seconds has passed. That doesn’t sound right though.
while true do
while resource < resourceLimit do
-- Regenerate resource
end
bindableEvent:Wait()
end
while true do
while resource < resourceLimit do
-- Regenerate resource
end
wait()
end
Perhaps I should create my own event system so I could learn how they are usually implemented and work.
When you use :Wait() it will wait until the event is fired and once that event is fired it will immediately continue (within the same frame). For wait() the amount of time it waits can vary drastically compared to :Wait() so ultimately I would go with :Wait() because it is more reliable and predictable. I think efficiency here is negligible but I do not know for fact which is the most efficient although I would put my bet on :Wait() being more efficient.
I think they would both fire in the same frame. I would personally say that bindableEvent.Event:Wait() is more efficient because it is only yielding once whereas RunService.Heartbeat:Wait() is checking → yielding every frame.