How can i clean up references in different locations?

Soo, i made electronics system and it creates elements such as logic gates, buttons, lamps ect. as OOP objects, the problem is that there are 3 references to it inside different scripts, idk how to clean them from :Destroy() methood that i made, any suggestions???

You can use :Destroy() that u made on it tho, you can also set the references to nil, so the garbage collector can clean up easily.

1 Like

But how can i make those other scripts with those references will know when to destroy those references, and even if how do i make it doesn’t end up having 4 functions to remove only 1 reference?

If you are using OOP in your module, then you must have already one Destroy() function that you can call on that object, even tho you can connect them all to an event and fire that event when you want them destroyed.

1 Like

Use maids.

You can pass objects to maids and once you destroy the maid, the objects within the maid are also destroyed, as long as they have a proper destructor method (e.g. Destroy(), Disconnect())

1 Like

hmmm, but how do i clean up references in those other scripts? should i store them and have custom function to remove them

I don’t want to use someone’s else plugin, i want to know how to make it myself

A maid is really just an array that stores objects, classes, and connections to be cleaned up when you destroy the maid.

local maid = require(...)

maid:add(workspace.Baseplate)

maid:Destroy() -- Baseplate is also destroyed

That’s what i like to do as well.

1 Like

Hmm, soo in every class that stores other references i should have table that stores those references, and eventually when i call :Destroy() i go to this table and clean it up?

Yes, that’s an advantage of using OOP, you can handle destroying the instances with functions easily.

1 Like

There are a couple garbage collection modules out there, to make this easier for you. Maid being my go-to one, Maid, but there are other choices too. Ex.: Janitor, Trove

1 Like

thx, but as i said i prefer to make stuff myself, soo i know it and can change it to my needs

Refer to my old responses and if you have questions let me know.

1 Like