Waypoints In Pathfinding Not Working

I am trying to make the NPC walk around obstacles and get to the destination. The problem is it can walk directly to the destination but it’s not going around obstacles. For some reason the for loop does not run and the waypoint array seems empty but nothing gets printed in the output even from the checks (I am not sure if I did the checks right). My friend made a script that was very similar to this one and tested it on this game and a different one with the same script. The first game didn’t work but the second game did and I have no idea why :frowning: . I have tried looking at other similar devforum pages but they seem to be not exactly what I am looking for.

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position


local pathService = game:GetService("PathfindingService")


 function walkForward()
	local path = pathService:CreatePath({AgentRadius = 4,
		AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
	})
	path:computeAsync(person.Position, goal)
	
	
	local waypoints = path:GetWaypoints()

	if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
		error("no path")
	end

	if waypoints == 0 or nil then
	error("waypoints is either nil or 0")
	end
		
	for i, waypoint in pairs(waypoints) do

		print("for loop running")
		print(waypoint.Position)
		humanoid:MoveTo(waypoint.Position)
		humanoid:MoveToFinishedWait(2)
	end

	

	
	--local function blockedPath(blockedWaypointIdx) not sure if i really need this
	--	local path = pathService:CreatePath()

	--	if blockedWaypointIdx > numWaypoints then
	--		path:computeAsync(person.Position, goal)

	--		if path.Status == Enum.PathStatus.Success then
	--			walkForward()
				
	--			else

	--			error("path not found")
	--		end
	--	end
	--end

	--path.Blocked:Connect(blockedPath)
	humanoid:MoveTo(goal)
end

while true do
	walkForward()

	task.wait(1)
end


*the yellow part is the destination and the wall is blocking the NPC

1 Like

I’m not too familiar with PathfindingService, but I’m pretty sure the “C” in “compute” should be capitalized in path:ComputeAsync

Try that and see if you still get any errors!

1 Like

It seems like it doesn’t matter which way you write it (nothing changed). I don’t think the problem is a syntax error but I don’t really know because I am just learning how to use pathfinding.

This is always gonna be true:
if waypoints == 0 or nil then

You have to use if waypoints == 0 or waypoints == nil then instead, like this:

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local goal = workspace.goal.Position
local pathService = game:GetService("PathfindingService")

function walkForward()
    local path = pathService:CreatePath({
        AgentRadius = 4,
        AgentHeight = 6,
        WaypointSpacing = 1,
        AgentCanJump = true,
        AgentCanClimb = true
    })

    path:ComputeAsync(person.Position, goal)

    local waypoints = path:GetWaypoints()

    if pathService:FindPathAsync(person.Position, goal) == Enum.PathStatus.NoPath then
        error("no path")
    end

    if waypoints == 0 or waypoints == nil then
        error("waypoints is either nil or 0")
    end

    for i, waypoint in pairs(waypoints) do
        print("for loop running")
        print(waypoint.Position)
        humanoid:MoveTo(waypoint.Position)
        humanoid.MoveToFinishedWait(2)
    end

    humanoid:MoveTo(goal)
end

while true do
    walkForward()
    task.wait(1)
end
1 Like

Im confused, if it’s always going to be true how come in the output it didn’t give any error?

Have you checked if it even reaches that part of the script?

1 Like

I see in your code you said “humanoid:MoveToFinishedWait(2)” when it should be 'humanoid.MoveToFinished:Wait(2)"

Here is a fixed version


local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position


local pathService = game:GetService("PathfindingService")


function walkForward()
	local path = pathService:CreatePath({AgentRadius = 4,
		AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
	})
	
	path:computeAsync(person.Position, goal)


	local waypoints = path:GetWaypoints()

	if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
		error("no path")
	end

	if waypoints == 0 or nil then
		error("waypoints is either nil or 0")
	end

	for i, waypoint in pairs(waypoints) do

		print("for loop running")
		print(waypoint.Position)
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait(2)
	end




	--local function blockedPath(blockedWaypointIdx) not sure if i really need this
	--	local path = pathService:CreatePath()

	--	if blockedWaypointIdx > numWaypoints then
	--		path:computeAsync(person.Position, goal)

	--		if path.Status == Enum.PathStatus.Success then
	--			walkForward()

	--			else

	--			error("path not found")
	--		end
	--	end
	--end

	--path.Blocked:Connect(blockedPath)
	humanoid:MoveTo(goal)
end

while true do
	walkForward()

	task.wait(1)
end
1 Like

Nope, after checking it seems like it should though. Which makes me even more confused.

Thanks, I have a question. Randomly I keep getting this error “PathfindingService: Path request failed due to a navmesh update error”. Is this my fault?

I don’t think they’ve solved this yet, but from the sound of it, if you have any meshes, it could be the problem.

PathfindingService Error: “Path request failed due to a navmesh update error” - Bug Reports / Engine Bugs - Developer Forum | Roblox

1 Like

Make some tests using the print function.

1 Like

Well I have 7.2k mesh parts so um maybe that’s the problem. :skull:

1 Like
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local numWaypoints = 1
local goal = workspace.goal.Position


local pathService = game:GetService("PathfindingService")


function walkForward()
	print("1")
	local path = pathService:CreatePath({AgentRadius = 4,
		AgentHeight = 6, WaypointSpacing = 1, AgentCanJump = true, AgentCanClimb = true
	})
	print("2")
	path:computeAsync(person.Position, goal)
	print("3")
	print("4")
	local waypoints = path:GetWaypoints()
	print("5")
	if pathService:FindPathAsync(person.Position,goal) == Enum.PathStatus.NoPath then
		error("no path")
	end
	print("6")
	if waypoints == 0 or nil then
		error("waypoints is either nil or 0")
	end
	print("7")
	for i, waypoint in pairs(waypoints) do

		print("for loop running")
		print(waypoint.Position)
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait(2)
	end
	print("8")



	--local function blockedPath(blockedWaypointIdx) not sure if i really need this
	--	local path = pathService:CreatePath()

	--	if blockedWaypointIdx > numWaypoints then
	--		path:computeAsync(person.Position, goal)

	--		if path.Status == Enum.PathStatus.Success then
	--			walkForward()

	--			else

	--			error("path not found")
	--		end
	--	end
	--end

	--path.Blocked:Connect(blockedPath)
	humanoid:MoveTo(goal)
end

while true do
	walkForward()

	task.wait(1)
end

Im not sure if this is what you wanted me to do but all of the numbers were printed so I think it would have to go through the if statement.

That does not make sense, as if waypoints == 0 or nil then always returns true and should output the error, can you try putting a print inside that if statement?

1 Like

I think I am doing something wrong because it returns nil…

print(waypoints == 0 or nil)

This also returned false.

print(waypoints == 0 or waypoints == nil)

Try running this:

local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local person = npc:WaitForChild("HumanoidRootPart")
local goal = workspace.goal.Position

local pathService = game:GetService("PathfindingService")

function walkForward()
    print("1")
    local path = pathService:CreatePath({
        AgentRadius = 4,
        AgentHeight = 6,
        WaypointSpacing = 1,
        AgentCanJump = true,
        AgentCanClimb = true
    })
    print("2")
    path:ComputeAsync(person.Position, goal)
    print("3")
    
    local pathStatus = path.Status
    if pathStatus == Enum.PathStatus.NoPath then
        error("no pathing found")
    elseif pathStatus == Enum.PathStatus.Complete then
        local waypoints = path:GetWaypoints()
        for i, waypoint in pairs(waypoints) do
            print("flag lr")
            print(waypoint.Position)
            humanoid:MoveTo(waypoint.Position)
            humanoid.MoveToFinished:Wait()
        end
    else
        error("path status: " .. tostring(pathStatus))
    end

    print("8")
    humanoid:MoveTo(goal)
end

while true do
    walkForward()
    task.wait(1)
end
1 Like

Yea it gives an error now


if pathStatus == Enum.PathStatus.NoPath then
		error("no pathing found")

Have you turned on any of the visuals like “show navigation mesh” to see what it’s seeing?

1 Like

I turned on both settings. I don’t see anything different, am I doing something wrong?