Need help with pathfinding. (I hate this so much)

So, I’m making a pathfinding script and I’m having some big issues. sometimes they randomly just stop, and they can’t walk upstairs. allowing them to jump fixes this, but they then just start to jump in place and run into walls.

code:

local function followPath(destination)
	
	local sucess, errorMessage = pcall(function()
		path:ComputeAsync(Character.PrimaryPart.Position, destination)
	end)
	
	
	if sucess and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then 
				blockedConnection:Disconnect() 
				followPath(destination)
			end
		end)
		if not reachedConnection then
			reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					nextWaypointIndex += 1
					LastHit = 0
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					if waypoints[nextWaypointIndex].Action == Enum.PathWaypointAction.Jump then
						humanoid.Jump = true
					end
				else
					waypoints = nil
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		
	else
		warn("Path not computed! :(", errorMessage)
		return false
	end
end
while task.wait(.1) do 
	local target = findTarget()
	CDMGCD += .1
	LastHit += .1
	SinceGoodPath -= .1
	if humanoid.Health == 0 then
		break
	end
	if target and GlobalValues.IsDay.Value == false then
		humanoid.WalkSpeed = 16
		followPath(target.HumanoidRootPart.Position)
	else
		humanoid.WalkSpeed = 10
		if CurrentDestination == nil then
			GetCurrentDestination()
		end
		if (CurrentDestination.Position - humanoid.RootPart.Position).Magnitude < 5 or SinceGoodPath <= 0 then
			GetCurrentDestination()
			SinceGoodPath = 20
		end
		local p = followPath(CurrentDestination.Position)
		if p == false then
			print("Forced Move")
			GetCurrentDestination()
			followPath(CurrentDestination.Position)
		end
	end
	if LastHit > 5 then
		nextWaypointIndex += 1
		LastHit = 0
		print("Hit Reset")
	end
end

if you want a clip or any more code please say so, feel free to use the code as well

1 Like

vid of what’s happening


this happens after a bit. it seems kind of random, as it can happen back-to-back or after a while (I had to wait about 30 seconds for it to happen, this time. other times it happened instantly).

1 Like

When I use stairs with pathfinding they are purely cosmetic. I always make sure there is an invisible wedge covering the stairs which is where the NPCs walk. It’s good practice for all stairs as it makes character movement smoother.

For the pathfinding stutter, have you set Network Ownership:

NPC.PrimaryPart:SetNetworkOwner(nil)

Have you also made sure to set your path parameters:

local pathParams = {
	AgentRadius = 3,
	AgentHeight = 2.5,
	AgentCanJump = true,
	AgentCanClimb = false,
}
1 Like

I should probably do that, but they still have issues even if it is a wedge.

Also if I set can jump to true, they start to jump in place and run into walls.

With the stutter, I’ve learned it’s because it doesn’t detect that it’s reach the destination. I’m using move to finished. For this I made a debounce, if it hasn’t reached a waypoint in 5 seconds it will just give up. I should probably make it based of how far the waypoint is.