Log in to vote
0

NPC follows humanoid named zombie how?

Asked by 3 days ago

So my npc follows every humanoid in existence and i will have it to follow humanoids named zombie

function findNearestTorso(pos)
    local list = game.Workspace:GetChildren()
    local torso = nil
    local dist = 10000
    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
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

while true do
    wait(0.1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

1 answer

Log in to vote
0
Answered by 2 days ago

function findNearestTorso(pos) local list = game.Workspace:GetChildren() local torso = nil local dist = 10000 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 if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end

while true do
    wait(0.1)
local target = workspace.zombie.Torso --Models doesnt have a position so it uses torso to find  
if target ~= nil then
            script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

--https://www.youtube.com/watch?v=PaNio-_Rvp0 a video for demonstartion

Ad

Answer this question