My script is not detecting when a player dies. Anyone know why?

In my script below, when a player dies, a line is supposed to be printed stating that the player has died. It seems pretty simple, and I thought that I did everything right until I tried it out multiple times and it didn’t work every single time. Here is my code below, anyone see any issues? *Something to note is that every player spawns in as a cube. The cube is a model that I named HumanoidRootPart and inserted it into a model called StarterCharacter under StarterPlayer as a Parent. I don’t know if this affects anything though…
This script is in StarterPlayerScripts btw.

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid:GetPropertyChangedSignal("Health"):Connect(function()
			if humanoid.Health <= 0 then
			print(player.Name .. " has died!")
			end	
		end)
	end)
end)

Here is a player’s children in the Workspace when they enter and play the game:

Screen Shot 2021-04-22 at 10.05.50 AM

Humanoid.Died Event exists, use that instead.

Couldn’t you just print it out using a :Died() function?

I tried Humanoid.Died, that didn’t work for me too…

Why is the script in StarterPlayerScripts? Move it to ServerScriptService and make it a regular script, or just do this if it must be local

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

humanoid.Died:Connect(function()
	print(player.Name .. " has died!")
end)

Your player is almost always added before the event has a chance to detect you if you use a localscript, but at this point you can just properties

I tried this but it didn’t work.

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died():Connect(function()
			print(player.Name .. " has died!")
		end)
end)
end)

The local script should function properly and detect when a player dies if it is in StarterPlayerScripts.

PlayerAdded has no time to detect if your localplayer has been added if it’s in a localscript, you need to use something like how I had mentioned with my example

Character loads after the player is in the game so the character would be detected?

Hello,

You could just put a script inside grafik
StarterPlayer>CharacterScripts

The script would be sth like this

script.Parent.Humanoid.Died:connect(function()
--ur things here
print(script.Parent.Name, " Died")--  | example
end)

You can use a .Died event for this.

Your script worked, but now when I try to detect when the player has respawned, it doesn’t work. Here is my code below:

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

humanoid.Died:Connect(function()
	print(player.Name .. " has died!")
	cubecolor = tostring(player.Character:WaitForChild("HumanoidRootPart").BrickColor)
	print(cubecolor)
end)


player.CharacterAdded:Connect(function(character)
	wait()
	print(cubecolor)
	if cubecolor ~= nil then
		character:WaitForChild("HumanoidRootPart").BrickColor = BrickColor.new(cubecolor)
end
end)

Maybe try this?

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
print(player.Name, "has died!")
end)
end)
end)

Try this if it’s still i nStarterPlayerScripts

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

local function death()
	print(player.Name .. " has died!")
	cubecolor = tostring(character:WaitForChild("HumanoidRootPart").BrickColor)
	print(cubecolor)
end

humanoid.Died:Connect(death)

player.CharacterAdded:Connect(function(char)
	wait()
	print(cubecolor)
	if cubecolor ~= nil then
		char:WaitForChild("HumanoidRootPart").BrickColor = BrickColor.new(cubecolor)
	end
	character = char
	humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(death)
end)

I believe it could because it’s working 1 time since those character and humanoid variables only work for that current session alive and or your character parameter overlapping with the variable? Also forgot to ask, does it at least print cubecolor?

1 Like