Why is my NPC's pathfinding not working right?

I’m making a NPC that follows you but for some reason he has a small brain. He wont jump over obstacles, he wont go around them, and sometimes he gets creative mode and flys around (No clue how). I would like some help because I really want this working by the end of the day. Thanks for your help!

local parent = script.Parent
local pathfinding_service = game:GetService("PathfindingService")
while wait() do
	if not parent.Configuration.Owner.Value then return end
	local path = pathfinding_service:CreatePath()
	local playerCharacter = parent.Configuration.Owner.Value.Character
	local distance = (parent.PrimaryPart.Position - playerCharacter.PrimaryPart.Position).Magnitude
	if distance > 10 then
		path:ComputeAsync(parent.HumanoidRootPart.Position, playerCharacter.PrimaryPart.Position)
		local waypoints = path:GetWaypoints()
		for _, waypoint in pairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
			local distance = (parent.PrimaryPart.Position - waypoint.Position).Magnitude
			local distance_from_player = (parent.PrimaryPart.Position - playerCharacter.PrimaryPart.Position).Magnitude
			if distance_from_player < 10 then
				parent.Humanoid:MoveTo((parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3)).p)

I think that the NPC did not pathfind there.

nope still nothing works…

I do not know why it does not work, but what you can do is this:

  • Divide every action into a separate function, so that your main code (in your infinite loop) is simple and readable
  • Do some debugging by drawing the waypoints and coloring them based on ‘(waypoint.Action == Enum.PathWaypointAction.Jump)’
  • wait() is not recommendable, use RunService (probably .Stepped) instead (see Avoiding wait() and why)

Have you defined the AgentHeight, AgentRadius and CanJump parameters? If not, you need to. Define these:

local pathParams = {
	AgentHeight = 5,			-- NPC height in studs
	AgentRadius = 2,			-- 1/2 Width of NPC to avoid bumping into walls
	AgentCanJump = false 	-- Jumpy, jump jump, jump

Change this:

local path = pathfinding_service:CreatePath(pathParams)

Now your CreatePath has more to work with.

my bad I rewrote the code and there’s another issue here’s the new post: Why is my Pathfinding sometimes placing 2 nodes?