How To Detect Every Time A Player Dies

I want To detect the player’s death but, After one death the script loses the Humanoid,
Is there a way to detect the humanoid every time the player dies?

6 Likes
humanoid.Died() is possibly the most useful one

example:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Char)
         Char.Humanoid.Died:Connect(function()
            --do code
         end)
    end)
end)
13 Likes

Does the player gets removed if the character dies, i Don’t get it.

1 Like

If the Character of the Player died, then the Player would respawn.

Humanoid.Died() will run when the Character is dead, meaning when Health is 0.

1 Like

But this line of code only runs once(only when the player joins)

1 Like

That’s why we added CharacterAdded, which will run each time the Character of the Player is in-game ,whether he’s just joined, or restarted, or respawned.

1 Like

Valkyrops solution here is correct. I recommend marking it as the solution under the assumption it works, and it should looking at the code. However just to make sure you understand what is going on here, if you’d like more information on what’s going on here, allow me to explain:

Keep in mind this isn’t the solution and I don’t intend it to be. I simply want to help you out in understanding the concept!

game.Players.PlayerAdded:Connect(function(Player)

Your assumption that this line only fires once is correct. Whenever they player joins a game, or a client initialized connection to the server, this event is fired. Anything between here and a character added event will only happen one time.

Player.CharacterAdded:Connect(function(Char)

Now, as implied, this line fires whenever the actual character is added. I think one thing that’s confusing you here is that you think that the condition of PlayerAdded must be fulfilled before this line runs again, under normal circumstances that would be correct. However, because the CharacterAdded connection has been made, anything within that event will be called whenever the character is added. Because the player has already been added a single time, anytime the character of that specific player is added, that event will fire. It does not require the player to be removed once more.

For illustration purposes, imagine you have a leaderstat value called points. You give one leaderstat per PlayerAdded, and one leaderstat per CharacterAdded. The player will get:

  • 1 point added whenever they join the game. This is the PlayerAdded event.
  • 1 point when there character loads in the game. This is the CharacterAdded event.
  • 1 additional point each time there character is added once more.

Anways, TL;DR the actual connection to CharacterAdded is created on PlayerAdded. That connection can and will be used regardless of if the PlayerAdded connection fires once more. That’s just how connections work! Sorry if it seems like I overcomplicated such a simple concept with this, just wanted to make sure you had all information available.

7 Likes

It’s Good and Understandable but, Still didn’t work.
Btw the script is local And Parented under a ScreenGui.
Here is the script

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Char)
		Char.Humanoid.Died:Connect(function()
		-- my code
		end)
	end)
end)

Thanks for the help, I am sorry for wasting your time.

3 Likes

Ah, I was unaware this was local!

Try the following instead. Because LocalScripts have the ability to get the LocalPlayer, you probably only need to do something like the following. Might be beneficial to add something to detect and see if player.Character actually exists already. Other than that this would probably work locally.

local Player = game.Players.LocalPlayer
local Char = Player.Character

Char:WaitForChild("Humanoid").Died:Connect(function()
	-- code
end)
16 Likes

If you only need to detect when LocalPlayer dies, use a LocalScript and remove the PlayerAdded connection. On the 2nd line, replace Player.CharacterAdded with Players.LocalPlayer.CharacterAdded.

1 Like

This will work only once, because Died connection will disconnect automatically when Humanoid object removes.

1 Like

Under the assumption this is in a ScreenGui with ResetOnSpawn enabled as it is by default, the script would reload every single time when the character spawns, thus getting the new character.

Keep in mind though if it isn’t set up this way, I’d recommend going with NeoBuilds solution.

1 Like

Sorry to Disappoint, Still didn’t work for some reason.

Have you tried the solution provided by @NeoBuilds ? As he pointed out correctly my script will only work under very specific conditions, those being that your ScreenGui has the “ResetOnSpawn” value set to true. If it’s not, my script won’t work or will only work a singular time. Give the script Neo created a try, if it doesn’t work we can try something else. :slight_smile:

The ScreenGui Reset on spawn is off
Edit: I made it a separate Gui with the reset on spawn On

1 Like

StarterPlayer > StarterCharacterScripts > (Create a local script here)

game.Players.LocalPlayer:WaitForChild("Character"):WaitForChild("Humanoid").Died:connect(function(char)
	-- Execute code here.
end)