PSA: Connections can memory leak Instances!

Does this also apply to events?

Say for example I have a remote event connected and that script is part of the player’s character. When the character is destroyed the script is too, but is the connection destroyed?

Yes. When a script is destroyed any connections created by it will be disconnected.

No, it does not. :Remove() is essentially a wrapper for parenting an instance to nil, :Destroy() takes extra steps (such as removing all connections) to ensure an object is actually ‘destroyed’. A trip to the API reference pages for destroy and remove could have quickly answered your question, so in the future try the API first :+1:

1 Like

You mentioned to always do :Destroy(), what if I want something to have a delayed destroy? Currently I use game.Debris, does this also remove the connections? Or should I switch to

task.delay(2, function()
   OBJECT:Destroy()
end)

Answer is yes:

1 Like

Can I insert a reference to a connection
local a; a = event:Connect(function()

end)
table.insert(connections, a)

and then do

connections[a]:Disconnect ??