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

How to make an enemy target and follow ONLY after being hit?

Asked by 4 years ago
function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 60
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then--and not temp.Parent:findFirstChild("NPC") then
                if (temp.Position - script.PartSu.Position).Magnitude < dist then
                    torso = temp
                    dist = (temp.Position - script.PartSu.Position).Magnitude
                end
            end
        end
    end
    return torso
end


while true do
    wait(.1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil and not target.Parent:findFirstChild("NPC") then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    else
        script.Parent.Humanoid:MoveTo(script.PartSu.Position, script.PartSu)
    end
end

This is my current script which is super glitchy due to the bottom part. When more than 1 enemy is around its like the enemy always runs back to PartSu.Position.

2 answers

Log in to vote
0
Answered by 4 years ago

What I would do is use a StringValue and name it something like Tag and put it into the humanoid of the NPC. Then, for my damaging script, each time I hit something, I would look for Tag in their humanoid. If there is Tag in their humanoid, then change the value to the player who hit the NPC. Then, in the NPC script, I would use a .Changed event for the Tag to check whenever the value is changed. That would be the target.

Ad
Log in to vote
0
Answered by 4 years ago

ok idk if this works, but if the enemy gets hit it should take damage so lets say the max health is 100 then you could put this at the start of the script

function follow()
local hp = script.Parent.Humanoid.Health
if hp <100 then
end
-- the follow player script

then at the bottom you can try this

script.Parent:Connect(follow)

Answer this question