Question about bindable events

So say you call Bindable:Wait() on a bindable for until it’s fired. If you use debris, and the bindable exceeds the time limit of the debris and is removed then will the :Wait() function no longer yield, or would it still be infinitely waiting for Event to trigger?

BindableEvent.Event:Wait()

I think this is what you meant. And until it is fired the current thread will yield.

1 Like

No, it seems it will still wait even if it is destroyed.

1 Like

Maybe instead of event:Wait() you could use:

function virtualWait(event)
	local occurred = false
	event:Connect(function()
		occurred = true
	end)
	while not occurred and event ~= nil do
		wait()
	end
end

such that instead of event:Wait() you would use virtualWait(event)

Event:Wait() is the most reliable method for optimization from this article:

So I’d like to know if there’s something else I can do to work around this instead of using a loop which it says is probably not the best way to go about waiting for something.

Not really sure of any other way.

Wait is not unbindable. Meaning once it is bound, the only way the rest of the code can execute is if the event is fired.

I guess you could check if the parent of the event is ChildRemoving and if the child is the event, fire it.

i.e

function FireOnRemove(event : BindableEvent)
    event.Parent.ChildRemoved:Connect(function(child)
        if child == event then
            event:Fire()
        end
    end)
end

But note that this will interfere with all other scripts. And make sure it is only called once. If multiple scripts call it, then it will fire multiple times. If you are only using :Wait then there will be no problem. But if you use :Connect the event will fire multiple times if this function is called from multiple scripts.