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

Greetings. I have tried to use the Humanoid.Died function to despawn an NPC if it dies, but the function doesn’t seem to be working. I even checked other scripts to see how they handle this; they either use this method, or a “while true do” loop which doesn’t seem like a good idea to me.

On my end, it just doesn’t do anything. I have set the script to print something whenever the NPC dies, but nothing shows up in the output, not even an error.

Here’s the code I’m using for this, located in a script inside the NPC’s character model:

local humanoid = script.Parent.Humanoid

humanoid.Died:Connect(function()
	if humanoid then
		print("dead")
	end
end)

I wish to know why this function isn’t working, or if I’m using it incorrectly in some way. I have looked for other threads related to this issue but I have not found anything useful.

2 Likes

check for the humanoid after it is loaded in, try using :WaitForChild()
usually it’s because of the character not loaded in yet, so also add a :CharacterAdded event

The script is inside an NPC in the workspace, not a player. Regardless, checking the humanoid with WaitForChild() did not change anything, neither did it solve the problem in any way.

hmm, could you show me your explorer tab? a bit of a redundant question but don’t mind me :coefficients:

also, could you try removing that if statement? it wouldn’t really matter anyways, but just to check as I think it’s a bit weird cuz the humanoid is already there

1 Like

The if statement is unnecessary. Try adding a print before humanoid to check the script runs.

print("runs")

local humanoid = script.Parent.Humanoid

humanoid.Died:Connect(function()
	print("dead")
end)

If it doesn’t print “runs”, make sure that it is a server script, and not disabled.

Do you run your script as LocalScript by any chance?

The Humanoid.Died part isn’t the only bit of the script. It is located inside the same script that makes the NPC walk, and said script functions just fine. It is also a server script. I find this to be very strange as there is no reason for it to not be working.

Can you show the explorer if possible? Humanoid.Died should fire so this is kinda weird. Maybe there’s something in your other code that tinkers with the Humanoid state

image
“Script” is the script where this code is located. I still do not understand what could be causing this but I will check the rest of the code and try to identify if something might be interfering.

Edit: I have also put a “print(“initialized”)” line in the script and it doesn’t seem to do anything, but everything else that isn’t the print and the humanoid.Died function works fine.

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.