Humanoid.Died:Connect(function() not firing / not working

Could the health be going in the negatives? This would prevent the .Died event from firing.

It is not. It reaches 0 after I kill the NPC and the NPC’s joints are broken on death, as normally occurs.

If this is the problem, I believe using :TakeDamage() instead of .Health -= would fix it. If not, then use a :GetPropertyChangeSignal(“Health”) instead of .Died

You could try using :GetPropertyChangeSignal(“Health”) as then you wouldn’t need to use a loop, yet it’d still have the same result.

Humanoid.Died will still occur with negative health as it just kills the humanoid

Did you say that the print(“initialized”) printed nothing on the output? I’m assuming it’s on the first line to check whether it the script is running or not

Yes. That line is unrelated to any other function, standing on its own inside of the script, and for some reason it doesn’t print anything out. The strangest part is that this script is where the code that makes the NPC walk is located, and said code works perfectly fine. It makes no sense.

Why not just put everything under the same script then? Or you can try to delete the Humanoid.Died script and make a new one if that will even do anything

If the print('initialized") statement didn’t even print out then it must be the script not running right from the start – nothing to do with your Humanoid.Died connection

It is already under the same script. The Humanoid.Died code is located inside the same script that handles the NPC’s movement and interaction, and said script works perfectly fine but refuses to do the print and the Humanoid.Died function. I will try making the died function in a separate script as I cannot figure out why this is happening.

Edit: Doing it in a separate script works. I still don’t understand why doing it in the same script doesn’t work.

Guessing it must be a return statement somewhere in your code. It’s weird though, roblox code editor should tell if a line can’t be reached, so maybe it’s something else – something like an infinite loop maybe?

Try this:

local PlrName = script.Parent.Name

local Plr = game.Workspace:FindFirstChild(PlrName)

if Plr then
	local Humanoid = Plr:FindFirstChild("Humanoid")
	
	Humanoid.Died:Connect(function()
		if Humanoid then
			print("Dead")
		end
	end)
end

this should work

How can error check for a humanoid if it’s gone/dead …
Just take out the: if humanoid then … It would not have fired unless they died.
No need for the error check. In this case the error check is the error.

1 Like

The humanoid instance will still be there for some time upon dying, so it is not the problem at all.

I understand that … but that time is not set in stone. If this isn’t working then humanoid is not set up right.

I think the issue, in truth, is that the script simply isn’t firing that part of the code at all. I have decided to handle the death using a separate script, which works just fine.

1 Like

You just gave us a snippet with no real context … in a perfect world that snippet is correct.

I found two posts (here and here) saying that .Died won’t fire if the NPC loses all its health in one go. It needs to take damage at least one other time before the fatal blow for .Died to work. If all else fails, then you may be able to do Humanoid:GetPropertyChangedSignal("Health").

local HealthHitZero = false 
script.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if HealthHitZero then return end
	local Value = script.Parent.Humanoid.Health
	if Value <= 0 then
		HealthHitZero = true
		--// Put code for responding to death here
	end
end)

If your script and everything work as intended without any flaw, Humanoid.Died should still fire upon instant death. You can try by setting the health immediately to 0 or do TakeDamage(math.inf)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.