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.
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.
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.
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
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