Get the player's character after the player leaves

I’m trying to get the player’s character after they leave for a tool system.

I’m wondering if it’s possible to get the character after the player leaves, I’ve tried a few different methods and I either don’t get anything or i get the error: ServerScriptService.Script:7: attempt to index nil with 'FindFirstChild'

I’ve looked around on the DevForum, there’s been 2 posts that were relevant to my issue, one of them had a solution that didn’t work for me, and the other hasn’t been solved for 2 years.

local keyModule = require(game.ServerScriptService:WaitForChild("Game Logic").KeyModule)

game.Players.PlayerRemoving:Connect(function(player)
	local char = player.Character
	
	print("Player left")
	if char:FindFirstChild("HumanoidRootPart") then
		if player:FindFirstChild("Contestant") and game.Workspace:FindFirstChild("Map") then -- added
			keyModule.ResetTools(player,game.Workspace.Map)
			print("Tools dropped")
		end
		
		if player:FindFirstChild("Contestant") then
			player.Contestant:Destroy()
		elseif player:FindFirstChild("Piggy") then
			player.Piggy:Destroy()
		end
	end
end)

I believe Player.Character will always be nil by the time Players.PlayerRemoving is called.

Maybe try using Player.CharacterAdded or Player.CharacterRemoving event to store a reference of the character instance when they spawn or despawn. Then when PlayerRemoving is fired, you can use that reference instead of trying to get it through player.Character

1 Like

when a players character is added clone it, and then save the clone to a table, once they leave just fetch the clone and boom. Done

I don’t think you even need to clone it. As long as you hold a reference to it somewhere, you’ll be good.

the players character gets deleted after they leave, so depending on what you are doing cloning it can be the best option

I think character removing event should work.

1 Like

Thank you, and thank you @Amritss for these solutions. Using Player.CharacterRemoving did work.

1 Like

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