Is there a way to wait until a connection is disconnected without repeatedly checking?

I attempted to do this using Changed to detect when the Connected property in the connection is changed, but received the error below:
image

Is there any possible way I can detect the INSTANT a connection is disconnected, without using a repeating loop? If not, is there a way I can wait until I make the connection = nil?

Would you mind providing a bit more of a background as to what you’re trying to do to give more context?

I do not see the necessity for context. I am simply asking if it is possible to do. If I provide context, people may try to provide me an alternative method, such as saying I can do:

repeat task.wait() until not connection.Connected

This is not my goal at all, and I want to do exactly as I described rather than a workaround.

Ok, well, assuming that Connected is a property, based on “the Connected property”, you can use :GetPropertyChangedSignal("Connected")

If I assumed wrong, that’s what I meant by needing more context. Your background is unclear and could relate to almost anything.

The problem is, RBXScriptSignal is not an instance, so the GetPropertyChangedSignal method does not exist.

Unfortunately then, I don’t know if there’s a way, at least that I’ve found. I don’t think I can help much further.

what kind of connection is being disconnected that you aren’t manually disconnecting it? just run whatever code you need where you disconnect it

There is no way to detect when a connection was disconnected without repeated checks. Generally what you’d want to do is perform whatever action that needs to happen precisely where you actually perform the disconnection.

local Connection
Connection = function(something)
    if condition == true then
        Connection:Disconnect()
        Connection = nil --Set it to nil to easily check if connection exists
        --Perform necesarry actions here
    end
end

having a loop check the status of a connection is also possible but seems unnecessary. If you want to check if a connection is active whether by loop or single action, an if statement will easily be good enough

if Connection then
    --do stuff
else
    --No connection
end
1 Like

RBXScriptConnection doesn’t provide any events for its disconnection. Polling the .Connected value, using a custom implementation of Signal that provides this feature, or manually firing another signal/calling a function after :Disconnect() is called are the only ways to wait until a signal connection is disconnected.

RBXScriptSignals and RBXScriptConnections are not Instances so they don’t have built-in change signals.

You state that you want to wait until the variable holding the connection is nil. Simple variables do not signal when they’re changed. Polling is also the only way to detect a change. If this connection is stored in some sort of proxy table implementation, then the __newindex metamethod of that implementation can fire a signal when the connection entry is set to nil. I’d suggest you reflect and pivot your concern away from signal connection disconnections. There’s probably a more intuitive way to accomplish your goal.