How do I clean objects when i'm not using them?

For example I create objects whenever a player gets a new tool

When the player dies it loses the weapon, how do I clean the unused object?

--Example 
local Pillow = PillowClass.new(Type.Value, Child.Handle, Player)
Pillow:onTouched()

``
1 Like

Custom classes don’t have the destroy methods.

@OP, to actually clear out the objects out of the memory, you can write a custom function that handles the equivalent function of :Destroy(), which will:

  1. Remove all connections and clearing them, preventing memory leaks.
  2. Deletng the necessary objects that are then unused, if the class has accessible properties that contain those.
1 Like

Does Debris service is a solution for this?

Technically, it clears out all Instance objects associated to the class, but not its remaining bits of connections or certain variables holding references, if those ever existed. However, that API is solely for purposes of a delayed removal of an object without it ever experiencing an error from the object being already destroyed.

3 Likes

How do I clear connections?

Pillow = nil? 

If the reference is somewhere, then you can do exactly that.

1 Like