How to use player.Character in PlayerRemoving?

CharacterRemoving happens whenever a Player’s character is removed, either when they are about to respawn, or they leave the game.

There’s no reason you can’t store the character, though, and then do your work after they leave.

The following only works if CharacterRemoving always fires before PlayerRemoving. I’m not sure if that’s true.

local characters = {} -- map from players to characters
local players = game.players

players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		characters[player] = character
	end)
end)

players.PlayerRemoving:Connect(function(player)
	local character = characters[player]

	if character then
		-- do whatever you want to their character
		-- note: their character's Parent will be nil and Locked here, but it's
		-- hierarchy should all still be the same as it was.

		characters[player] = nil -- could probably also just make characters a weak-keyed table but this works too
	end
end)
3 Likes