:Disconnect() is broken?

I want to disconnect every function in the table from a connection.
When I print “v” it says “Connection”, which means I should be able to use :Disconnect()
When I use disconnect it gives no error but it doesn’t disconnect. I’ve tried spamming :Disconnect() to see if it would error, no error, no disconnection.

This is the code I have to disconnect everything, I know there’s probably a really simple solution that I’m just not realizing yet.

for i,v in pairs(cmds) do
	v:Disconnect()
end
2 Likes

Could you show me the table of connections?

1 Like

The names are encoded in Base64, but that shouldnt change anything.

I think they meant seeing “cmds” in your script, which I’d also like to see if possible. Might help fix the issue, since we can’t see the code for the connections in that list.

The code of the command is very old, so just ignore that part.
image

1 Like

This is intended behavior and is somewhat of quirk because the connections are still stored in memory due to them being in a table.

Try purging the table after disconnecting the events and see if that works for you.

2 Likes

And you’re sure there’s no disconnect happening? Just because it’s still in the list and still prints “Connection” doesn’t mean it hasn’t disconnected. I just tested with this, and it is disconnecting, but v is still called a connection when printed. Also, there’s no errors if you try to disconnect twice, so that might also be misleading:

v = game:GetService("RunService").Heartbeat:Connect(function()
	print("Test")
end)

print(v)
game:GetService("RunService").Heartbeat:Wait()
game:GetService("RunService").Heartbeat:Wait()
v:Disconnect()
print(v)

Like @OppaStoppaStomppa said, try removing that from the table/resetting the table completely.

2 Likes

So i’ve purged the table after and it still doesn’t seem to change anything.

What’s your problem with :Disconnect()?

Connection:Disconnect() doesn’t destroy the connection, it only disconnects the function from the event. You can observe this by printing the Connected property of the connection.

for i, v in pairs(cmds) do
    print(v.Connected) -- true
    v:Disconnect()
    print(v.Connected) -- false

    print(v) -- Connection; still exists
end

You can get rid of connections by waiting for them to be garbage collected. I’m not sure about the technical ins-and-outs of garbage collection, but I do clear any references to the connections by wiping tables with my disconnected events.

for key, connection in connections do
    connection:Disconnect()
    connections[key] = nil -- Objects without references are garbage collected (destroyed)
end
2 Likes

so it is disconnected but for some reason the function still works, as when the function runs it will print two of the same thing, then three, then four, etc.

update, it all works, I think it’s something to do with there being multiple of the functions. I’ll problem solve it myself and will come back to this if I need anything.

Edit: for some reason the function that kept running wouldn’t be added to the table

Second edit: I had to just use numerical indexes instead :sob::sob:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.