Get new character after death (Humanoid.Died)

Hello.

I’m trying to get a hat to parent to the new player’s character after the player dies. Anyone know how to?

(Example)

-- Yes, I know this is client-sided. It's meant to be like that.
local Player = game:GetService("Players").LocalPlayer;
local Humanoid = Player.Character:FindFirstChild("Humanoid");

local Hat = game:GetService("Workspace"):FindFirstChild("Hat");

Humanoid.Died:Connect(function()
   Hat.Parent = newCharacter -- newCharacter doesn't exist, anyone know how to get it?
end)

I recommend using WaitForChild() like so

game.Workspace:WaitForChild(Player.Name)

Or CharacterAdded like so

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		print(Character) --New character
	end)
end)
4 Likes

Wouldn’t CharacterAdded return the dead player’s character?

No? It fires when player respawns, not when player dies.

2 Likes

Why would you parent a hat to a dead character, it would disappear once they respawn.
And, can’t you just parent it without waiting for them to respawn?

1 Like

No because it fires once they respawn

Not sure if it’s because I used it in a Localscript, but it returned the dead character.

Nevermind, I got it to work. Wasn’t using it properly.

My intention with the script isn’t to parent a hat to a dead player’s character. It’s just an example which makes understanding easier. In reality, I’m making client-sided visualizers and when the character dies, I want it to parent it to a folder until the new character’s respawned and then parent it to the new character again.

Then you would so something as such:

local Player = game.Players.LocalPlayer
local Character = game.Workspace:WaitForChild(Player.Name)

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if game.YourStorage:FindFirstChild("Hat") then --Storage area
			game.YourStorage.Hat.Parent = Character
		end
	end)
end)

Character.Humanoid.Died:Connect(function()
	local Hat = Character.Hat --Get the hat
	Hat.Parent = game.YourStorage --Storage area idk maybe a folder
end)
1 Like

In that case, here’s some pseudocode for you, that you can work with.

local Players = game:GetService('Players')
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')


local function HumanoidDied()
	print(('Player %s died!'):format(Player.Name))
	-- parent their hat.
end


Humanoid.Died:Connect(HumanoidDied)
1 Like