Pathfinding NPC tries walking into walls

i’ve created a script that uses PathfindingService. big deal. however, i’ve run into a glaring issue. whenever (and i mean, without fail!) i try telling an npc to move along a path that is generated by the PathfindingService, it always tries to ram itself headfirst into the nearest wall.

i’ve been stuck trying to figure this out for the past 2 hours. any help would be appreciated.

here is my script (whipped together to re-create the issue):

i’ve also tried raycasting, but that wasn’t very helpful.

local pathfinding_service = game:GetService("PathfindingService")
local vis_mod = require(script.waypoint_visualizer)

local npc = script.Parent
local root = npc.HumanoidRootPart
local personoid = npc.Humanoid
local goal = workspace.End

local path = pathfinding_service:CreatePath({AgentRadius = 3.5; WaypointSpacing = math.huge})

while task.wait(0.5) do
	path:ComputeAsync(root.Position, goal.Position)

	local waypoints = path:GetWaypoints()
	vis_mod:view_waypoints(waypoints)

	for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			personoid.Jump = true
		else
			personoid:MoveTo(waypoint.Position) -- please stop ramming yourself into walls it will not help you
		end
	end
end

the ModuleScript that visualizes waypoints accurately, not my code, i just renamed the variables:

local module = {}

local visuals = {}

function module:view_waypoints(wp)
	for n, x in pairs(visuals) do
		x:Destroy()
	end
	local att = Instance.new("Attachment")
	local beam = Instance.new("Beam")
	beam.FaceCamera = true
	beam.LightInfluence = 0
	beam.Color = ColorSequence.new(Color3.fromRGB(1, 255, 170))
	
	for n, x in pairs(wp) do
		local point = att:Clone()
		point.Parent = workspace.Terrain
		point.Position = x.Position
		if n ~= 1 then
			local con = beam:Clone()
			con.Attachment1 = point
			con.Attachment0 = visuals[n - 1]
			con.Parent = point
		end
		visuals[n] = point
	end

	task.wait(0.5)
	for n, x in pairs(visuals) do
		x:Destroy()
	end
end

return module

screenshots:



2 Likes
    for i, waypoint in pairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			personoid.Jump = true
		else
			personoid:MoveTo(waypoint.Position) -- please stop ramming yourself into walls it will not help you
		end
	end

MoveTo does start moving the Humanoid towards the point, but it doesn’t block the caller until it’s reached the target point. That means your for loop just keeps going. Consider what happens on the 2nd iteration: the Humanoid is already moving to the 1st waypoint, but subsequent calls to MoveTo just overwrite that. At the end of the 2nd iteration, it’s moving towards the 2nd waypoint. At the very last iteration, it overwrites the 2nd-to-last MoveTo call and the end result is that your code is equivalent to just calling MoveTo with the very last waypoint. This fits with your observations: the character seems to be moving exactly towards the last waypoint.

You fix it by waiting in the loop until the humanoid has actually reached the waypoint of the current iteration.

There are also some quirks with MoveTo that you need to know about. Check out this old post: What is the best way to move an NPC to a specific point? - #5 by ThanksRoBama and also the post I linked to from there. Hope this helps! :slight_smile:

2 Likes