Does GC clean up models left orphaned in nil?

Situation:

  1. Central script clones a character model into workspace.
  2. Script within character clones a Tool out of ReplicatedStorage and parents it to nil. Occasionally, it will move it into the character, and back.
  3. Option A: Central script disables all scripts in the character.
  4. Option B: Chracter is destroyed by any means (central script or by itself).
  5. Tools are left in nil and will never get destroyed or moved again. They have scripts inside them that have event handlers.

Will the tools eventually get destroyed due to having no parent and no reference? If yes/no, does option A/B make a difference? Does it make a difference if the tools have running (event connection) scripts inside them?

1 Like

According to the Roblox. Docs about Instance:Destroy()

“Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections”

It’s very similar if not the same thing but as I understand it can still be accessible in scripts . For your case I don’t understand why :Destroy() is not an option but it seems like it could be a good option for you for disconnecting the events.

Placing it in nil is a tad different as if you reference it, it won’t be destroyed at all with time. If nothing is connected to it including variables, yes it will be removed

Objects parented to nil should get Garbage Collected as long as there are no references to the object. That includes connections to events on it.