NPC does not detect any other nearest players when it's chasing someone

Hello. I am making an enemy NPC, it works fine, but when it’s chasing someone, and another player is near the NPC, it won’t go to that player and will keep chasing the current player, I don’t know why this is happening. Help is appreciated :slight_smile: Here’s my code:

local noob = script.Parent
local human = script.Parent:WaitForChild("Humanoid")
local rootpart = script.Parent:WaitForChild("Torso")

rootpart.Touched:Connect(function(hit)
 
    local character = hit.Parent
    
    if character and character:FindFirstChild("Humanoid") then
        character:BreakJoints()
    end
    
end)

local runservice = game:GetService("RunService")

local pathfindingservice = game:GetService("PathfindingService")

local paths = game.Workspace:WaitForChild("Paths"):GetChildren()

local dist = 200

local function getDistance(humanoidRoot, targetPosition)
    return (humanoidRoot.Position - targetPosition).magnitude
end

local function moveToFinished(root, target)
    repeat runservice.Stepped:Wait() until (rootpart.Position - target).magnitude <= 5
end

local function checkSight(t)
    local ray = Ray.new(rootpart.Position, (t.Position-rootpart.Position).Unit * dist)
    
    local hit, position = workspace:FindPartOnRay(ray, noob)
    
    if hit and hit.Parent and hit:IsDescendantOf(t.Parent) then
        return true
    else
        warn("Nope!")
        return false
    end
    
end

local function returnWaypointsAndCreatePath(startPoint, endPoint)
    local path = pathfindingservice:CreatePath()

    path:ComputeAsync(startPoint, endPoint);

    return path:GetWaypoints()
end
local function setNetworkOwner(npc)
    for i, v in pairs(npc:GetChildren()) do
        if v:IsA("BasePart") then
            v:SetNetworkOwner(nil)
        end
    end
end

local function findTarget()
    local playersNearly = {}
    for index, target in pairs(workspace:GetChildren()) do
        if target:IsA("Model") and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 and target.Name ~= noob.Name and target.PrimaryPart and (target.PrimaryPart.Position - rootpart.Position).Magnitude <= dist then
            table.insert(playersNearly, {target.PrimaryPart, (target.PrimaryPart.Position - rootpart.Position).Magnitude})
            
            table.sort(playersNearly, function(a, b)
                return a[2]<b[2]
            end)
            
            return playersNearly[1][1]
            
        end   
    end
    return "random", Vector3.new(math.random(-50, 50),0, math.random(-50, 50))
end

local function followPath(startPoint, endPoint)
    local waypoints = returnWaypointsAndCreatePath(startPoint, endPoint)
    
    for index, point in pairs(waypoints) do
        if point.Action == Enum.PathWaypointAction.Jump then
            human.Jump = true
        end
        local target, vector = findTarget()
        if target ~= nil and target ~= "random" and checkSight(target) then human:MoveTo(target.Position) break end; 
        human:MoveTo(point.Position);
        human.MoveToFinished:Wait();
    end
end

setNetworkOwner(noob)

while wait() do

    local target, vector = findTarget()

    if target ~= "random" and vector == nil then
        followPath(rootpart.Position, target.Position)
    else
        followPath(rootpart.Position, rootpart.Position+vector);
    end
end
3 Likes