Face wont appear when npc dies

I have a problem with my script where the npc’s health is set to 0, where it should change the face to a death face. But it wont appear

     if npc.Humanoid.Health <= 49 and npc.Humanoid.Health > 0 then
          babyface.Texture = "rbxassetid://14437808130"
          print("face printed")
          task.wait(6)
     else
          if npc.Humanoid.Health <= 0 then
               babyface.Texture = "rbxassetid://910076134"
               print("face printed 2")
          end
     end
end)
3 Likes

What object is babyface? Where is it defined?
If babyface doesn’t have a Texture property, the code won’t work.
Could you tell us what error it outputs if you’ve confirmed all of my questions?

babyface is the decal of the head and it has the Texture property and it doesn’t output any errors
local babyface = script.Parent:WaitForChild("Head"):WaitForChild("face")

2 Likes

Are you sure that the Health of the Humanoid is 0 or under 49?

does the script loop properly? what does it print?

Did you forget to use “While true do”?

local npc = script.Parent
local babyface = script.Parent:WaitForChild("Head"):WaitForChild("face")

while true do -- checks every 0.1 seconds to see if the Humanoids health changes
	wait(0.1)
if npc.Humanoid.Health <= 49 and npc.Humanoid.Health > 0 then
	babyface.Texture = "rbxassetid://14437808130"
	print("face printed")
	task.wait(1)
else
	if npc.Humanoid.Health == 0 then
		babyface.Texture = "rbxassetid://910076130"
		print("face printed 2")
		end
	end
end

This should work

Since when the health can be lower than 0 npc.Humanoid.Health <= 0 then isn’t it npc.Humanoid.Health == 0 then

1 Like

You should put all NPCs in a folder in workspace, rename the folder to “NPCs” and make a serverscript in the NPCs folder.

for _, npc in pairs(script.Parent:GetChildren()) do
	if npc:IsA("Model") then
		local humanoid = npc:FindFirstChild("Humanoid")
		local head = npc:FindFirstChild("Head")
		local face = head and head:FindFirstChild("face")

		if humanoid and face then
			humanoid.HealthChanged:Connect(function(health)
				if health > 0 and health <= 49 then
					face.Texture = "rbxassetid://14437808130"
				elseif health == 0 then
					face.Texture = "rbxassetid://910076134"
				end
			end)
		end
	end
end

The script is simply looping through the NPCs folder and is checking if it’s a model and if it is itt checks if it finds the face in the head and the humanoid in the npc. If so, it binds the humanoid of each NPC to an event checking the health. Its much efficienter than a while loop in every NPC.

The script is the same, what do you mean? The while loop is horrible.

To clarify more, The script works without any errors. I’ve also tried :Died and it works but it still doesn’t show the face

     if npc.Humanoid.Health <= 49 and npc.Humanoid.Health > 0 then
          babyface.Texture = "rbxassetid://14437808130"
          print("face printed")
          task.wait(6)
     end
end)

npc.Humanoid.Died:Connect(function()
     if npc.Humanoid.Health <= 0 then
          babyface.Texture = "rbxassetid://4820464526"
          print("face printed2")
     end
end)

npc.Humanoid.Health = 0 --testing purposes

face not appearing:

I found the solution. So basically, i have to duplicate the head along with its decal then i put the dead face to the duplicated decal then the script gets the face from the duplicated decal

local babyface = script.Parent:WaitForChild("Head"):WaitForChild("face")
local ohyeayea = game.Workspace:WaitForChild("ohyeayea"):WaitForChild("deadface")
local ohyeayeaface = ohyeayea.Texture
local injuredface = "rbxassetid://14437808130"

npc.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
     if npc.Humanoid.Health <= 49 and npc.Humanoid.Health > 0 then
          babyface.Texture = injuredface
          print("face printed")
     else
          if npc.Humanoid.Health == 0 or npc.Humanoid.Health <= 0 then
               babyface.Texture = ohyeayeaface
               print("face printed2")
          end
     end
end)

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