Player.CharacterAdded, Humanoid.Died, etc improperly cleaned up

Images of the leak via LuaHeap before implementing a disconnection handler :


{0FDDCA4B-76DF-45D4-BD51-9B983A143B0B}

Player.CharacterAdded, Humanoid.Died, Humanoid.HealthChanged and so on do not disconnect upon their respective ancestors being destroyed/removed/respawned. When a player is removed, .CharacterAdded continues to take up memory (hours later), and the same goes for when a character respawns/dies. If a character respawns, Humanoid.Died is not called and left taking up memory, so :Once() is not relevant here. .HealthChanged stays alive even after Character destruction.

I had to implement individual connection disconnection handlers for each case in my game, this was an unexpected development hiccup as this is not the behaviour described on the documentation for Instance:Destroy().

This issue also occurs when running : task.spawn(function() Player.CharacterAdded:Connect … end) and likewise, it is important to note that :Start() is ran in a new thread as well (task.spawn(function() Module:Start() end) so this may be the cause.

Direct Link : [NEW] ⛽ fight at a gas station - Roblox

Expected behavior

I expect that connection such as .CharacterAdded, or Humanoid.Died or Humanoid.HealthChanged will be automatically disconnected upon destruction of the Player (player leaving) or Character objects respectively.

The fix :
{9BADA1F5-8DC4-4FA7-A470-FB209662FA60}

Roblox documentation stating this behaviour is supposed to happen : Instance | Documentation - Roblox Creator Hub

If I did something wrong please correct me, but from my understanding I am not required to implement disconnection handlers for these events.

2 Likes

It is only true if Player or Character are destroyed, which is not the case if they only respawn or disconnect.
Many developers actually expect them to hang around after those events.

On the server, you can opt in to an option for Character/Player destruction: New Player and Character Destroy Behavior

We did get some requests to enable this on Client as well, but that’s currently not available: PlayerCharacterDestroyBehavior doesn't function on the Client

While testing that feature, we found a good percent of games on the platform will become broken, so it’s unlikely that automatic destroy for Players or Characters will become possible in the future (See update in New Player and Character Destroy Behavior Enabled by Default)

2 Likes

Can I ask how you discovered that enabling this feature would result in games being broken? Are you just monitoring for new errors?