Question about function connections

Hey everyone, hope your doing well.

This may sound like a silly question but I just want to clarify it.

Lets say I have a CharacterAdded function, and the player resets or dies will that event get disconnected or is it recommended to check for the players death via the Humanoid and disconnect the event from there?

I’m just making sure of this as I don’t want my game memory to slowly build up other resets and deaths.

2 Likes

This is the first line from the CharacterAdded Documentation.

“The CharacterAdded event fires when a player’s character spawns (or respawns).”

If you want this script to happen once, when a player joins you could use the PlayerAdded function which only happens once, or alternatively you may be able to put the script in StarterPlayerScripts depending on the purpose of the script, if you make it local.

There may be other solutions as well, but that is what I know, hope this helps!

2 Likes

Right thank you I semi understand but as you mentioned in the documentation it fires when they spawn or respawn, but does that mean it will disconnect the old function from when they originally spawned?

1 Like

No.

Remember, events can only be disconnected if you expressly do so, or if any of its instances or parent instances are destroyed.

Here’s the thing though, character models and player instances don’t actually get destroyed when they despawn for any reason, be it when the player leaves the game, or if they die or reset - their parents are just set to nil.

This means that events bound to a player or character instance will stay indefinitely unless you expressly disconnect them or destroy the instances entirely.

On the surface, this doesn’t seem to be an issue, no matter how many times a player resets or dies.

The problem is when the player leaves the game.
Since the character and player instance doesn’t actually get destroyed when a player leaves, over the lifespan of a server you’ll accumulate enough player/character event connections that the server degrades.

This is the behavior for as long as Roblox’s lifetime, and if you ever wonder why servers seem to slow down when they are up for quite a while, this is a big reason why.

UNLESS, you set workspace.PlayerCharacterDestroyBehavior to true.
This ensures that the character gets destroyed properly, meaning event connections are also cleaned up.

Some things to read up on if all this still doesn’t make sense to you:

1 Like

Amazing thank you, so it seems to be worth setting that property to true then. I’m surprised its not default.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.