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

Stop NPC from getting to closed?

Asked by 6 years ago

How do I keep my NPC it's called police car and I want it to keep it's distance away from player. It can chased players down but I want it not to get too close to player cars.

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    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("Right Arm")
            human = temp2:findFirstChild("Target")
            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(math.random(1,5))
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid.Script.Disabled = false
        script.Parent.Humanoid:MoveTo(target.Position, target, -10)
    else
        script.Parent.Humanoid.Script.Disabled = true
    end

end

Answer this question