Script is not detecting players death

I want to run some code when the player dies but I can’t even detect it, anybody knows why?

When I died I expected to see the print in the output, nothing happened along with no errors.

Players.PlayerAdded:Connect(function(player)
	PlayerHandler:SetupPlayer(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print( player.Name.." has died")
		end)
	end)
end)

Video of me resetting:

https://gyazo.com/1f985b5e20fb410bab7f384a7e418da7

Try resetting again after you die and see if it prints. Also are you running this in studio or online?

It doesn’t print, and yes I’m testing this in studios

Do you mind testing it for me in-game? Sometimes playeradded doesn’t fire when in studio.

Firstly, I’m assuming this is a server script. It’s generally a good practice to not only have a PlayerAdded connection, but to loop over all the players (from Players:GetPlayers()) if there is any chance that your PlayerAdded connection is going to be made after someone has joined the server.

Likewise, if anything in your PlayerHandler:SetupPlayer() yields, it’s possible that the player’s character has loaded before you reach line where you connect to CharacterAdded. In this case, their initially-spawned character will not report their death, only characters that they respawn subsequently. You can resolve this by checking if player.Character already exists and attach the Died connection to it, in addition to listening for CharacterAdded (which is still needed for subsequent respawns).

2 Likes

By the time you reach
player.CharacterAdded:Connect
The character has already spawned.

Do

function characterAdded(character)
	character:WaitForChild("Humanoid").Died:Connect(function()
		print( player.Name.." has died")
	end)
end

Players.PlayerAdded:Connect(function(player)
	PlayerHandler:SetupPlayer(player)
	player.CharacterAdded:Connect(characterAdded)
	if player.Character then
		characterAdded(player.Character)
	end
end)

Just tried it in a Roblox game, still doesn’t print.

Try checking if the Character already exists when they join the game. If so, connect your Died event to that existing humanoid.

You were right, I removed the wait() I had in the player setup module and it worked, thanks for the help.