My pathfinding ai doesn't work properly

I already tried everything…

function Move()
	if isChasing and not enemyRootPart.Anchored then
		local MainTorso = fetchPossibleTargets(enemyRootPart.Position)

		if MainTorso then
			currentlyWandering = 1

			local path = PathfindingService:CreatePath({
				AgentCanJump = true,
				Costs = {
					Water = 20
				},
				SupportPartialPath = true
			})

			path:ComputeAsync(enemyRootPart.Position, MainTorso.Position)

			if path.Status == Enum.PathStatus.Success then
				local waypoints = path:GetWaypoints()

				if #waypoints > 0 then
					for i, waypoint in ipairs(waypoints) do
						if waypoint.Action == Enum.PathWaypointAction.Jump then
							enemyHumanoid.Jump = true
						end

						enemyHumanoid:MoveTo(waypoint.Position)
						enemyHumanoid.MoveToFinished:Wait()
					end
				end
			else
				print("Path unsuccessful")
			end
		end
	end
end

while RunService.Heartbeat:Wait() do
	task.spawn(Move)
end

Video: https://www.mediafire.com/file/yyc7gkyrp62rzln/lowkeyBuggy.mp4/file

from the video, the npc gets stuck on the cart or smth, try disabling collison to it, let me know if that was the problem in the video

entire map has these (benches too)

in actual game, they all have collisions.

it feels like it’s trying to decide a path.

try making a test map and see if it still issues occur, if so i can share you my version

I’ve already tested it in my map for testing, it’s still buggy.

85904199952901.rbxm (30.6 KB)
try this pathfinding ai i made, see if it works for you




don’t ask why it jumps when it shouldn’t…

before trying mine, try going to studio and show navigation mesh, it shows how the npc is pathfinding, make sure if you are playing it on studio, go to server to see how its pathfinding as it wont work when play testing on client side

your script focuses on an end part, not on characters

you want me to make it chase nearest player? if so i can send you a code for the npc

if you want, but the only thing I rlly want to fix is this twitching and uneccessary jumping…

ok try this one:

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local Bot = script.Parent
local Humanoid = Bot.Humanoid
local RecalculationInterval = math.random(0.3, 0.6)  -- Randomize intervals slightly per bot
local MinimumRecalculationDistance = 5  -- Recalculate if target moves more than 5 studs
local waypoints
local NextWaypointIndex
local ReachConnection
local BlockConnection

-- Function to find the nearest player
local function findNearestPlayer()
    local nearestPlayer = nil
    local nearestDistance = math.huge

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

    return nearestPlayer
end

local function FollowPath(targetPosition)
    -- Avoid recalculating if the bot is already close to the target
    if (Bot.HumanoidRootPart.Position - targetPosition).Magnitude < MinimumRecalculationDistance then
        return
    end

    local path = PathfindingService:CreatePath({
        AgentRadius = 6,
        AgentHeight = 5,
        AgentCanJump = true,
        WaypointSpacing = 1e6,
    })

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

    if success and path.Status == Enum.PathStatus.Success then
        waypoints = path:GetWaypoints()
        NextWaypointIndex = 2

        if ReachConnection then ReachConnection:Disconnect() end
        if BlockConnection then BlockConnection:Disconnect() end

        BlockConnection = path.Blocked:Connect(function(blockedWaypointIndex)
            if blockedWaypointIndex >= NextWaypointIndex then
                BlockConnection:Disconnect()
                FollowPath(targetPosition)
            end
        end)

        ReachConnection = Humanoid.MoveToFinished:Connect(function(reached)
            if reached and NextWaypointIndex < #waypoints then
                NextWaypointIndex += 1
                Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
            else
                ReachConnection:Disconnect()
                BlockConnection:Disconnect()
            end
        end)

        Humanoid:MoveTo(waypoints[NextWaypointIndex].Position)
    end
end

-- Main loop for staggered recalculation
while wait(RecalculationInterval) do
    local nearestPlayer = findNearestPlayer()
    if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
        FollowPath(nearestPlayer.Character.HumanoidRootPart.Position)
    end
end

the only issue i find with it is that it has some problems with pathfinding to slopes,


it twitches here…

okay, kinda fixed, now I only have to make it to always update path and to not walk to last position

Edit: nvm, it’s still kind of stupid…

you want me to send you the version that can jump or what

I made it jump now, it’s just kinda buggy

did you try showing navigation mesh on studio settings?, you can see what path its following