Should I disconnect connections?

I won’t provide the code because I don’t think it’s necessary.

I create connections for GUI objects, and at the moment I don’t disconnect the connections because when I would need to disconnect them, the objects are destroyed anyway.

So my question is, should I disconnect the connections for an instance if I am destroying the instance anyway? If I don’t disconnect the connections, will it cause a memory leak?
Logically, I assume that since I’m destroying the instance, all the relevant connections would automatically get disconnected but I’ve had people tell me otherwise.

Any insight on this is much appreciated!

2 Likes

When an instance is destroyed, all connections to its events are disconnected. In this case, you do not have to worry about disconnecting events.

In the event that you’re NOT destroying the instance that the event is a member of, you should definitely disconnect the connections when you’re done with them; the only reason you get away with it when the instance is destroyed is because Roblox disconnects the connections for you under the hood.

7 Likes

Thank you! Some tutorials I’ve seen would disconnect connections even if the instance is destroyed and justfied it by “preventing memory leakage” so I was confused.

1 Like

Gotta be careful about them folks who harp on “best practices!!1!” without actually knowing why and when they’re best practices or having a hint of knowledge about the Roblox engine.

Yeah I kind of thought so myself but some of the statements they make I have no idea how to prove or disprove

That’ll come as you develop more knowledge of the Roblox engine and programming/computing in general. Just keep learning and experimenting. You will pick it up as you go.

Also remember to use :Once instead of :Connect if you only want to listen once (eg. animation or sound ending).

2 Likes