Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

NPC Damage script not playing animation or other things?

I've been going at this for around an hour now, and can't get my head around why it won't work. It's a damage script where if the NPC is below 65 health, it plays a hurt animation, as well as shows blood and changes face. However, when I change

if script.Parent.Health <= 65 then

to

if script.Parent.Health >= 65 then

it works perfectly but does above 65 health, but when I change it to <, symbolizing below, the script simply does not function. If you have a fix, PLEASE help me.

local humanoid = script.Parent
local cHealth = humanoid.Health
local animation = script.Parent.Parent.LowHealth
local LA = Instance.new("Animation")
LA.AnimationId = "rbxassetid://5540835623"
local LowHealth = humanoid:LoadAnimation(LA)

if script.Parent.Health <= 65 then
	LowHealth:Play()
	script.Injured.Parent = script.Parent.Parent.Head
	script.Parent.Parent.Head.Normal:Destroy()
	script.Parent.Parent.Head.Blood.Enabled = true
	script.Parent.Parent.Head.Blood2.Enabled = true
	script.Parent.Parent.Head.Blood3.Enabled = true
	wait(2)
	script.Parent.Parent.Head.Blood.Enabled = false
	script.Parent.Parent.Head.Blood2.Enabled = false
	script.Parent.Parent.Head.Blood3.Enabled = false
-----------------------------------------------------------------------------
elseif script.Parent.Health <= 45 then
	LowHealth:Play()
	script.Injured.Parent = script.Parent.Parent.Head
	script.Parent.Parent.Head.Normal:Destroy()
end

2 answers

✓ Accepted answer

Because it doesn't constantly check if its health is low.

Try doing this:

local humanoid = script.Parent
local cHealth = humanoid.Health
local animation = script.Parent.Parent.LowHealth
local LA = Instance.new("Animation")
LA.AnimationId = "rbxassetid://5540835623"
local LowHealth = humanoid:LoadAnimation(LA)

humanoid.Changed:Connect(function()
	if script.Parent.Health <= 65 then
		if LowHealth.IsPlaying == false then 
	 		   LowHealth:Play()
		end
	    script.Injured.Parent = script.Parent.Parent.Head
	    script.Parent.Parent.Head.Normal:Destroy()
	    script.Parent.Parent.Head.Blood.Enabled = true
	    script.Parent.Parent.Head.Blood2.Enabled = true
	    script.Parent.Parent.Head.Blood3.Enabled = true
	    wait(2)
	    script.Parent.Parent.Head.Blood.Enabled = false
	    script.Parent.Parent.Head.Blood2.Enabled = false
	    script.Parent.Parent.Head.Blood3.Enabled = false
	-----------------------------------------------------------------------------
	elseif script.Parent.Health <= 45 then
	    if LowHealth.IsPlaying == false then 
	 		   LowHealth:Play()
		end
	    script.Injured.Parent = script.Parent.Parent.Head
	    script.Parent.Parent.Head.Normal:Destroy()
	end
end)

You have to place that code in a loop, so your code runs more than one time

local humanoid = script.Parent
local cHealth = humanoid.Health
local animation = script.Parent.Parent.LowHealth
local LA = Instance.new("Animation")
LA.AnimationId = "rbxassetid://5540835623"
local LowHealth = humanoid:LoadAnimation(LA)

while true do -- The loop will make the code run infinitly
    if script.Parent.Health <= 65 then
        LowHealth:Play()
        script.Injured.Parent = script.Parent.Parent.Head
        script.Parent.Parent.Head.Normal:Destroy()
        script.Parent.Parent.Head.Blood.Enabled = true
        script.Parent.Parent.Head.Blood2.Enabled = true
        script.Parent.Parent.Head.Blood3.Enabled = true
        wait(2)
        script.Parent.Parent.Head.Blood.Enabled = false
        script.Parent.Parent.Head.Blood2.Enabled = false
        script.Parent.Parent.Head.Blood3.Enabled = false
-----------------------------------------------------------------------------
    elseif script.Parent.Health <= 45 then
        LowHealth:Play()
        script.Injured.Parent = script.Parent.Parent.Head
        script.Parent.Parent.Head.Normal:Destroy()
    end
    wait(1) --Don't forget to add waits on all loops
end

This discussion is preserved as it was. New answers, comments, and votes are closed.