How to Detect When A Player Dies?

Here is my script

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:Connect(function()
	print("Dead")
end)

It is located under StarterPlayerScripts

Here is the error…
Players.LargeHotDogs13.PlayerScripts.WhenDie:1: attempt to index nil with ‘WaitForChild’

Please Help

LargeHotDogs13

8 Likes

Use a serverscript. Do something like this:

game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function)
print("dead")
end)
end)
end)

Local scripts in characters get reset when the character dies I think.
Also, The error was you get the character immediately. The character needs time to load in, plus, you should always use ServerScripts wherever possible. If for whatever reason you can’t, the CharacterAdded event is useful:

local Character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
--when the character is added, you can use it as the character.
10 Likes
game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid").Died:Connect(function()
	print("Dead")
end)

A humanoid is a class object, meaning you can retrieve it using the FindFirstChildOfClass function. I’d highly advise against using WaitForChild on a Humanoid, because it will most likely error from what I’ve expereinced.

7 Likes
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	print("Hello world!")
end)

You need to wait for the character to be added/loaded before attempting to index its “Humanoid” instance.

3 Likes

Is there anyway I can make it so when you die the script is still enabled bc when u die again it doesnt work

You need to re-connect it every time they die. Just re-connect it in the .Died function. I may be right, or wrong, but I think you need to.

1 Like

You’re right. You can use the CharacterAdded event to get the new Humanoid and make a new connection for that Humanoid’s Died event.

So, something like this:

local function onCharacterAdded(character)
    local player = game:GetService("Players").LocalPlayer -- This is how you could get the player here

    local humanoid = character:WaitForChild("Humanoid", 5)
    if not humanoid then
        return
    end

    humanoid.Died:Connect(function()
        print("Player died!")
    end)
end

game:GetService("Players").LocalPlayer.CharacterAdded:Connect(onCharacterAdded)

do -- Should do something like this for code that starts after the game does. In this case it's not needed because the character won't exist when the player joins/when LocalScripts start.
    local alreadyExistingCharacter = game:GetService("Players").LocalPlayer.Character
    if alreadyExistingCharacter then
        onCharacterAdded(alreadyExistingCharacter )
    end
end

37 Likes