Will the connections like On character added will be gc'd if the player left

That is a bit confusing cause in the docs they don’t clean the connection in 1 place where are CharacterAdded and Character removing, but they clean it in CharacterAppearanceLoaded example

Any events being connected to an instance will get removed once it has been destroyed. The player is also an instance with the class “Player”, so once they leave, the Player instance will get destroyed and all connections connected to this instance will disconnected and removed. Maybe the script in the documentation that you’ve mentioned disconnected it manually for a different purpose?

By the way, firing the :Connect() or similar methods returns a RBXScriptConnection which is basically the wire that connects events. Destroying instances will disconnect and remove every connection tied to it.

Here’s the code from the documentation

Which includes “Make sure do disconnect connection when the player leave”

For me it got confusing a bit while as you said Instance auto cleans the connections if destroyed
probably they still have old behaviour for Player or just the docs are outdated

Code from docs:

local Players = game:GetService("Players")

local function onPlayerAddedAsync(player)
	local connection = player.CharacterAppearanceLoaded:Connect(function(character)
			-- All accessories have loaded at this point
			local humanoid = character:FindFirstChildOfClass("Humanoid")
			local numAccessories = #humanoid:GetAccessories()
			print(("Destroying %d accessories for %s"):format(numAccessories, player.Name))
			humanoid:RemoveAccessories()
	end)

	-- Make sure we disconnect our connection to the player after they leave
	-- to allow the player to get garbage collected
	player.AncestryChanged:Wait()
	connection:Disconnect()
end

for _, player in Players:GetPlayers() do
	task.spawn(onPlayerAddedAsync, player)
end
Players.PlayerAdded:Connect(onPlayerAddedAsync)

Just making sure that there will be no memory leaks

Honestly, that’s probably redundant. Instances (including players) disconnect all of their events when they get destroyed. So when a player leaves, it should clean it all up automatically so calling connection:Disconnect() is redundant because roblox should call that one automatically.

The other one Players.PlayerAdded:Connect(onPlayerAddedAsync) however will never get cleaned up. It’s fine in this script because it is called exactly once and is supposed to exist for the whole lifetime of the game. But if you had that get connected by some other event where it could get called multiple times then you would end up creating a unique connection for each of them.

So the only time you need to worry about it is if you have a script that will dynamically connect events based on some other event really. Just make sure that it will get cleaned up by either handling it manually somehow or by ensuring the affected object gets destroyed which will clean up the connections.

1 Like

Prior to the release of Workspace’s PlayerCharacterDestroyBehavior property, it was the case that players and their characters had their parent set to nil, rather than be destroyed. As a consequence, it wasn’t guaranteed that their connections were garbage collected, so it would’ve been good practice to do so yourself

Now that the property exists, if you set it to Enabled, there shouldn’t be a need to manage these connections yourself, unless you experience any issues

More information about this property can also be found here:

1 Like