Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to check for a change in health?

Asked by 2 years ago

Hello! I was trying to make an AI but ran into issues checking when it took damage it just wouldnt do anything. After adding a print to it and it still not printing anything, google wasnt any help at all.

I've tried using :GetPropertyChange("Health") with the same result and yes I have been dealing damage on the server side not client side if u think that it the issue. I have even wrote a separate script specially to deal damage. AGAIN no results it just doesnt work... idk why

local Humanoid = script.Parent:WaitForChild("Humanoid")

local OldHealth = Humanoid.Health

local UnderAttack = false

function RunAway()
    print("fired")
    UnderAttack = true
    Humanoid.WalkSpeed = 20
    Humanoid:MoveTo(math.random(0,150), 0, math.random(0,150))
    Humanoid.MoveToFinished:Connect(function()
        Humanoid.WalkSpeed = 16
        UnderAttack = false
    end)
end


function RandomMove()
    Humanoid:MoveTo(script.Parent.PrimaryPart.Position + Vector3.new(math.random(0,100), 0, math.random(0,100)))
    Humanoid.MoveToFinished:Connect(function()
        Humanoid.WalkSpeed = 16
        UnderAttack = false
    end)
end


function Multiply()
    wait(math.random(5,200))
    local Clone = script.Parent:Clone()
    Clone.PrimaryPart.CFrame = script.Parent.PrimaryPart.CFrame:ToWorldSpace(CFrame.new(5,2,0))
end

function Main() 
    if not UnderAttack then
        wait(math.random(10,20))
        RandomMove()
    end
end

while wait() do
    if Humanoid.Health < 1 then
        script.Parent:Destroy()
        break
    end
        Main()
end

script.Parent.Humanoid.HealthChanged:Connect(function()
    print("fun")
    if Humanoid.Health < OldHealth then
        print("RUNNING")
        UnderAttack = true
        RunAway()
        OldHealth = Humanoid.Health
        UnderAttack = false
    end
end)
0
try moving "OldHealth = Humanoid.Health" (line 55) below the end (line 57) vendinY 5 — 2y
0
Had no affect like is said it wasnt even printing anything when the event fires but thanks treex070 35 — 2y

1 answer

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
2 years ago

Try this:

script.Parent:FindFirstChild("Humanoid"):GetPropertyChangedSignal("Health"):Connect(function()
    -- Code here
end)
0
Yeah same effect still doesn't work- but thanks for trying :( treex070 35 — 2y
Ad

Answer this question