I have an NPC Monster that has a Humanoid named "Human" and it is supposed to be destroyed after its health reaches 0 or lower. It is a Server Script and is located inside the head of the Monster.
This is the code:
if script.Parent.Parent:FindFirstChild("Human").Health <= 0 then script.Parent.Parent:Destroy() print "Monster has died" end
It does not print "Monster has died" and the only reason I can think of it not working is because the "Monster" for now is just a wall named "Head" and chases the player with a chasing script.
I'm a new scripter so I might have done something very stupid but I really can't figure this out :/
First of all, your print
doesn't have the brackets on line 3.
Secondly, health is not found in a thing called Human
. It's a Humanoid
.
So, here would be the fixed script:
if script.Parent.Parent:FindFirstChild('Humanoid').Health <= 0 then script.Parent.Parent:Destroy() print("Monster has died") end
Hope I helped!
Figured it out! The code should be:
script.Parent.Human.HealthChanged:Connect(function() if script.Parent:FindFirstChild("Human").Health <= 0 then script.Parent:Destroy() wait() end end)
It is because my first code simply checks the health at the beginning and sees it's at full health and ignores it afterwards. However, this one checks whenever its health changes and when it finally changes to 0 or under, it is destroyed.