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

Pathfinding Not Tracking Player Properly?

Asked by 4 years ago

I have been working on a Path-finding script that is supposed to track a player, and if the player is right in sight, it goes to the player regardless of the waypoint that's next, and s supposed to break the entire loop. However, that isn't happening and eventually, the NPC gets stuck. I linked to a video of what is happening https://youtu.be/KiGHgdtnitY

My code:

script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

function findNearestHuman(curPos, ran)
    local foundNear = false
    local NearestPlayer = nil
    for i,v in pairs(game.Players:GetPlayers()) do
        if v and v.Character and v.Character.Humanoid then
            local distance = v:DistanceFromCharacter(curPos)
            if distance < ran then
                foundNear = true
                ran = distance
                NearestPlayer = v.Character.UpperTorso
            end
        end
    end
    return foundNear, NearestPlayer
end

function findDoor(curPos, ran)
    local foundNear = false
    local NearestPlayer = nil
    for i,v in pairs(workspace.Map:GetChildren()) do
        if v and v:FindFirstChild("alienDetector") then
            local distance = (curPos - v.Door.Position).Magnitude
            if distance < ran then
                foundNear = true
                ran = distance
                NearestPlayer = v.Door
            end
        end
    end
    return foundNear, NearestPlayer
end
pathBlocked = false
local pathService = game:GetService("PathfindingService")
lastNearPos = nil
--[[{AgentRadius=3,AgentHeight=8}]]--

local hum = script.Parent.Humanoid

points, oldPath, path, hit, position = nil

while true do
    wait(1/200)
    local nearDoor, door = findDoor(script.Parent.Torso.Position, 25)
    local near, plr = findNearestHuman(script.Parent.Torso.Position, script.Parent.Configurations.AggroRange.Value)

    if near then 
        hit,position = workspace:FindPartOnRayWithIgnoreList(Ray.new(script.Parent.Torso.Position, (plr.Position - script.Parent.Torso.Position).Unit * 200), {script.Parent}) 
    end

    if nearDoor then
        door.Parent.Button.ClickDetector.open:Fire()
    end

    if near and (hit and hit.Parent:IsDescendantOf(plr.Parent) ~= true) and path == nil then
        path = pathService:FindPathAsync(script.Parent.Torso.Position, plr.Position)

    elseif (hit and hit.Parent:IsDescendantOf(plr.Parent) == true) then
        script.Parent.Humanoid:MoveTo(plr.Position)

    end

    if path ~= nil then
        if path.Status == Enum.PathStatus.Success then
            path.Blocked:Connect(function() pathBlocked = true end)
            script.pathObj.Value = path
            script.pathUpdated.Value = true
            points = path:GetWaypoints()
            recomputed = false
            for i,p in pairs(points) do
                hit,position = workspace:FindPartOnRayWithIgnoreList(Ray.new(script.Parent.Torso.Position, (plr.Position - script.Parent.Torso.Position).Unit * 200), {script.Parent})
                if hit then
                    if hit.Parent:IsDescendantOf(plr.Parent) then
                        script.Parent.Humanoid:MoveTo(plr.Position)
                        path = nil
                        break
                    else
                        hum:MoveTo(p.Position)
                        hum.MoveToFinished:Wait(2)
                    end
                end
                if (points[#points].Position - plr.Position).magnitude > 2 and i >= #points - 2 and recomputed == false then
                    path = pathService:FindPathAsync(points[#points].Position, plr.Position)
                    recomputed = true
                end
                if pathBlocked == true then
                    path = nil
                    break
                end
                if i == #points - 1 then
                    hum:MoveTo(path:GetWaypoints()[1].Position)
                    break
                end
            end
        else
            path = nil
        end
    end
end

I am trying to make it, more or less, smoothly track the players, so it can go around corners and such without getting stuck. However, that just isn't happening. Is there something I am missing? Or AM I going about solving my problem the wrong way?

Answer this question