NPC pathfinding not working

  1. What do you want to achieve?
    A NPC that follows the player.

  2. What is the issue?
    Randomly out of nowhere the NPC stops working. All he does is somehow float in the air even though none of his body parts are anchored.

  3. What solutions have you tried so far?
    I have tried remaking the pathfinding and it still doesn’t work. There is no errors or anything.

local path = pathfindingService:CreatePath({
                    AgentRadius = 3,
	                AgentHeight = 5,
	                AgentCanJump = true,
	                Costs = {
		                Slate = 20
	                }
                })

                local waypoints
                local nextWaypointIndex
                local reachedConnection
                local blockedConnection

                local function followPath(destination)
                    -- Compute the path
                    local success, errorMessage = pcall(function()
                        path:ComputeAsync(noob.PrimaryPart.Position, destination)
                    end)
                 
                    if success then
                        repeat
                            task.wait()
                        until path.Status == Enum.PathStatus.Success
                        if path.Status == Enum.PathStatus.Success then
                            -- Get the path waypoints
                            waypoints = path:GetWaypoints()
                 
                            -- Detect if path becomes blocked
                            blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
                                -- Check if the obstacle is further down the path
                                if blockedWaypointIndex >= nextWaypointIndex then
                                    -- Stop detecting path blockage until path is re-computed
                                    blockedConnection:Disconnect()
                                    -- Call function to re-compute new path
                                    followPath(destination)
                                end
                            end)
                     
                            -- Detect when movement to next waypoint is complete
                            if not reachedConnection then
                                reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
                                    if reached and nextWaypointIndex < #waypoints then
                                        -- Increase waypoint index and move to next waypoint
                                        nextWaypointIndex += 1
                                        humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
                                    else
                                        reachedConnection:Disconnect()
                                        blockedConnection:Disconnect()
                                    end
                                end)
                            end
                     
                            -- Initially move to second waypoint (first waypoint is path start; skip it)
                            nextWaypointIndex = 2
                            humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
                        end
                    else
                        warn("Path not computed!", errorMessage)
                    end
                    return
                end

                local nameTag = noob.Head.nameTag
       
                nameTag.ownerName.Text = string.format("%s's", player.Name)
                nameTag.ownerName.shadow.Text = string.format("%s's", player.Name)
                task.wait(1)
                noob:SetPrimaryPartCFrame(character.PrimaryPart.CFrame)
                task.spawn(function()
                    while true do
                        followPath(character:GetPrimaryPartCFrame().Position)
                        task.wait()
                    end
                end)
                character.Humanoid.Died:Connect(function()
                    path:Destroy()
                end)
1 Like