I wanted to make a NPC that was able to make new way points on a player but i'm not to sure were I should implement it in my code I have in my current NPC. Here's what I have:

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 25
    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("Torso")
            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
--wait(math.random(0,5)/10)
while true do
    wait(0.5)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
    end

I was assuming I would add some sort of compute path I'd find online.

2 comments

0

P.S I'm very new to this website.

0

is it just me or i saw this exact code on a youtube video