#Very bad Path finding problem..

1 messages · Page 1 of 1 (latest)

elfin shale
#

Hello i am new and have been learning about PathFinding sense yesterday (no sleep yet), My Problems are: NPC stops / pause at every waypoint white neon circles (look at video to know what i mean), When Npc has no path or Path is blocked it says nill which means its not finding a part so what do i do? it doesn't reset, and it keeps Running into my Costs ( yes collision is off, and costs is set to math.huge)

My goal is to know what i did wrong and what is should do going forward.

Here is the entire script:

--// Services
local PathService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

--// NPC Setup
local NPC = script.Parent.Parent
local Humanoid = NPC:FindFirstChild("Humanoid")
local HumanoidRootPart = NPC:FindFirstChild("HumanoidRootPart")
local HitBox = NPC:FindFirstChild("HitBox")

--// Attributes & Settings
local IsActive = NPC:GetAttribute("IsActive") or false
local WalkParts = workspace:FindFirstChild("WalkPaths") and workspace.WalkPaths.PollyWalkPath:GetChildren() or {}
local Range = 30 
local ShowWayPoint = true

--// Path Object
local Path = PathService:CreatePath({
    AgentRadius = 3,
    AgentHeight = 3,
    WaypointSpacing = 10,
    Costs = {
        No = math.huge
    }
})

--// Visualize waypoints
local function VisualizeWaypoint(waypoint)
    local part = Instance.new("Part")
    part.Shape = Enum.PartType.Ball
    part.Material = Enum.Material.Neon
    part.Anchored = true
    part.CanCollide = false
    part.Size = Vector3.new(0.5, 0.5, 0.5)
    part.Position = waypoint.Position
    part.Parent = workspace
    Debris:AddItem(part, 1)
end

--// Pathfinding Function
local function PathFinding(targetPosition)
    if not HumanoidRootPart or not Humanoid then return end

    local success, errorMessage = pcall(function()
        Path:ComputeAsync(HumanoidRootPart.Position, targetPosition)
    end)

    if not success or Path.Status ~= Enum.PathStatus.Success then
        warn("Path failed: ", errorMessage)
        return
    end

    -- Get waypoints from the path
    local waypoints = Path:GetWaypoints()
    
    -- Recalculate if path gets blocked
    Path.Blocked:Connect(function()
        warn("Path blocked, recalculating...")
        PathFinding(targetPosition)
    end)

    -- Move through waypoints
    for _, waypoint in ipairs(waypoints) do
        if ShowWayPoint then
            VisualizeWaypoint(waypoint)
        end

        Humanoid:MoveTo(waypoint.Position)
        local reached = Humanoid.MoveToFinished:Wait()

        -- If NPC gets interrupted, break out
        if not reached then
            break
        end
    end
end

--// Get Closest Player
local function GetNearestPlayer()
    local nearestPlayer, nearestDistance
    nearestDistance = math.huge

    if not HumanoidRootPart then return nil end

    for _, player in ipairs(Players:GetPlayers()) do
        local character = player.Character
        local root = character and character:FindFirstChild("HumanoidRootPart")
        if root then
            local distance = (root.Position - HumanoidRootPart.Position).Magnitude
            if distance < nearestDistance then
                nearestDistance = distance
                nearestPlayer = root
            end
        end
    end

    if nearestPlayer and nearestDistance <= Range then
        return nearestPlayer
    end
    return nil
end

--// Chase Behavior
local function ChasePlayer()
    local target = GetNearestPlayer()

    if target then
        -- Chase nearest player
        PathFinding(target.Position)
    else
        -- Wander to walk parts if no player is near
        for _, part in ipairs(WalkParts) do
            if part and part:IsA("BasePart") then
                PathFinding(part.Position)
            end
        end
    end
end

--// Main Loop
task.spawn(function()
    while task.wait() do
        if NPC:GetAttribute("IsActive") then
            ChasePlayer()
        end
    end
end)
toxic yarrow
#

why not put a timer?

#

like a 5 second timer?

elfin shale
elfin shale
#

If you guys have any tips just lmk ill have discord in the background, until then ill try to play around with this path finding

toxic yarrow
#

Cuz i would put a timer on the detection, Not the "log player position" or whatever you do to see the player

elfin shale
# toxic yarrow It didnt work? Or did it work?

Oh my bad, i didn't understand because you just said timer, which i thought you meant a loop, Remember i am new to scripting so i didnt know timers were a thing, A timer is a really good idea because it wouldn't have to loop thru the script so i will try that right now thank you so much,
I also mange to fix one of my problems which was the NPC stopping at everyone waypoint.. TURNS out the loop was waiting on each waypoint to be removed before it went to the next one

Ill update you on the timer method