Disconnect :Wait()?

I searched for this on the forums and I came across a thread that showed that you can use bindable events to yield your script. I tried this in my script and it worked, but in some cases I would need to disconnect it. :Disconnect() doesn’t work for this.

Are there any ways to disconnect a :Wait()? I’ll use another method to yield the script in the meantime

if self.Yield then
    self.Finished.Event:Wait() --disconnect this
end

You can’t “disconnect” the wait() because in this case, :Wait() is a method on an event, not a function connected to the event. There’s a fundamental difference there. The solution is going to depend on what you’re trying to do overall and the cases in which you don’t want your script to yield.

3 Likes

If you want the technical and proper explanation: Connect returns a special object, RBXScriptConnection, to which Disconnect is a method of. This is why functions passed to Connect can be disconnected but Wait can’t be.

Disconnect prevents your listener (the function you pass to the Connect method) from running again when a signal fires. In the case of Wait, you’re calling a yielding function. It waits for a response first. If you’ve worked with RemoteFunctions before, the dynamic is similar to that - you invoke an environment, that environment performs work and then returns a result.

If you have a reference to the thread that you called RbxScriptSignal.Wait in, you can probably just call coroutine.resume to “disconnect” it. Whether or not needing to do this is code smell is a different conversation.