Destroy Instance of Class?

I’ve created a class to handle my game’s rounds. When the round ends I want to destroy the instance of the class so that it can be garbage collected. Is there an easy way of doing this or do I need to go through every reference and destroy it?

Also, I’m not sure if it’s good practice to be placing events inside of methods. I have a :LoadMap() method that has MouseClick events hooked up to blocks.

2 Likes

I cannot figure out what you want to achieve. But if you want to destroy something, just call Destroy() on it.

For the events I’d recommend you insert them into an array which is directly in the Class, then you can iterate through the array and :Disconnect() the events.

For the overall class, it’ll get Garbage Collected anyways if it leaves the scope of where it was created and there are no further references to it.

If you are talking about metatable based “classes” (custom objects) then yes all references have to be removed for it to go away. That is why is it very very common to keep the usage of the same class instance within only one script and not spread it around like 30 arrays.

1 Like

If I remove all references, will the MouseClick events created in the class get gced too?

No. That only happens if the instance connected or the script itself gets deleted/Destroy()'d