Characters not destroyed when respawned

After you respawn the old character still has all of its children as if it’s just parented to nil instead of actually being destroyed.

This can cause problems if you’re using a players Humanoid or something like that as they still have their parent, etc.

Running the following script shows that after you respawn the old character still has all of its children ( where as if you :Destroy( ) the old character first it has none )

Put this script in StarterPlayerScripts and watch what it prints:

repeat wait( ) until game.Players.LocalPlayer.Character

local OldChar = game.Players.LocalPlayer.Character

game.Players.LocalPlayer.CharacterAdded:connect( function ( )
	
	print( OldChar, #OldChar:GetChildren( ) )
	
	wait( 1 )
	
	print( OldChar, #OldChar:GetChildren( ) )
	
end )

EDIT: The same also happens for the Backpack and PlayerGuis

EDIT2: Because objects in the character / etc aren’t destroyed, events are not disconnected which is a waste of memory

e.g.

repeat wait( ) until game.Players.LocalPlayer.Character

local OldChar = game.Players.LocalPlayer.Character

local Hum = OldChar:WaitForChild( "Humanoid" )

Hum.HealthChanged:connect( function ( ) print"ran" end )

game.Players.LocalPlayer.CharacterAdded:connect( function ( )
	
	wait( 1 )
	
	Hum.Health = 5
	
end )

will print ran even though the character has respawned

1 Like

This should not be changed, because similarly, some things will break if a child instance of the character is ‘disassembled’ by :Destroy().

For example, in my game I have some surfacegui I parent in things, and parent it away to some other thing eventually.

But if the first thing was destroyed, the surfacegui will no longer have its children (as :Destroy() recursively parents all descendants to nil), and that breaks it (the surfacegui will no longer have its children when I parent it to the next place).

Generally, an instance should not be destroyed if you still have a reference to it. The whole point of destroy is that you use it when you know that you are not going to reuse the instance or any children of the instance. Roblox cannot know that, so they cannot and should not destroy characters.

Considering the character is dead and no longer even in the game, it really should be cleaned up using :Destroy( ) else it’s just leaving a load of garbage that is not used

As for people still having references to it, what are they going to do with a dead character that is no longer used by the Player? I can’t think of any reasons a player would knowingly keep references to it around

As for your GUI, recreate it?

If there was no reason to access things within removed things, there wouldnt be an option to .Parent=nil or :Remove().

I wont recreate state just because some annoying side-effect might wipe it out of existence for no reason.

Parenting stuff to nil doesnt leave garbage (itll get collected just like any other object, when nobody is referencing it anymore). The only way there will be a memory leak is if your code is faulty. Use :IsDescendantOf(workspace) if you want to be certain its in workspace (or just check the parent of the character instead of the humanoid).

They could add a special method to make it easier to check (:Exists() or something), as this is a pretty common thing to check for.

But destroying characters isnt good, because developers will have non-character stuff in that model (vehicles, tools…), and it will break scripts if the object hierarchy of those developer-owned objects is broken down unexpectedly. Side effects like this are undesirable.

If the control of character death/destruction was on the developer, so they can decide how to destroy the character and save any descendants that need to be saved, that would be fine. Like some humanoid.OnDeath function you can bind (humanoid.OnDeath=function() myVeryImportantGUI.Parent=nil character:Destroy() end)

1 Like

It won’t be garbage collected if it has events, which there probably will be for the humanoid

Edit: also, what you’re worrying about could easily be fixed by just moving your custom stuff out of the character when they die, as it would only be destroyed when the player actually respawns

1 Like