Character variable will remain in a thread despite respawning

After further testing in my gear module I found the Character variable will remain the same despite being destroyed (natively now) and respawning, this shouldn’t happen naturally and the Character variable should be nil if the character respawns.



the third ‘a11’ print statement happens after spawn and Character is not nil

Expected behavior

For any destroyed instance to be cleaned and not have to be manually, specifically characters, I’m forced to use an identifier to make sure the character died and is not the same as the previous one.

Destroy doesn’t clean references and never has. There be dragons clearing strong references when alive code might still be using it.

Sets the Parent property to nil, locks the Parent property, disconnects all connections, and calls Destroy() on all children.

You can use Destroying (not all destruction events fire this for some reason) to tell when the character is destroyed, if it doesn’t fire, switch to AncestryChanged and check if the second parameter is nil.

2 Likes

Adding on to this, there is no way for Instance variables to “become” nil, because all Luau sees is a userdata, which is it’s own type that cannot be nil or falsey. There’s no easy way for the Engine to set every variable which refers to a destroyed Instance to nil either through Luau’s API.

As metatablecatmaid has explained, when something is destroyed, it doesn’t clean references. To clarify this in your case, it means when you’re holding a reference to a character and that character gets destroyed, that reference does not turn nil. It is, as far as Luau is concerned, still a valid reference.

Since player characters themselves are likely never unparented to nil for reasons other than cleanup, you can try checking its parent property to get an indicator whether it’s destroyed.

task.delay(10, function()
  if _Character.Parent ~= nil then
    -- Do stuff
  end
end)
1 Like

This isn’t a bug this is intended behavior.

I feel like that’ll use too much memory just to clear up a variable on every single module for every gear