Custom Changed event for variables

This isn’t actually completely true, logically it would be, but when you mess with instances the entire thing gets messed up and garbage collection is way more of a crybaby.

There is info about this here.

Usually self-referencing references would not cause an issue to the garbage collector, however when it comes to using a BindableEvent this is a lot more problematic.
Basically the layer between Lua and C (Instances and by extension BindableEvents and RBXScriptSignals) doesn’t understand references that well, and basically if you ever reference anything that is or has a reference to that BindableEvent, it will count as a strong reference even if the only reference is a reference inside that Instance’s event connection which would usually not matter since its disconnected from the rest of the game.

Basically, referencing your bindableevent inside that bindablevent’s connection completely breaks garbage collection for that instance, and so if you want to clean that up you need to destroy the bindableevent or disconnect everything before hand in some way.

What this means is that if you reference the Node in a connection on .Changed on it, that will be a memory leak unless you destroy the Node before getting rid of its references.

The other better way is to not use a BindableEvent or any instance in general, and instead use a Lua implementation of a Signal. That way you don’t depend on weird Instance shenanigans like these.

1 Like