Pathfinding npc gets confused whenever its facing with the truss

the issue’s is blatant but the solutions i’ve tried so far was from a “thread”? and it didn’t work
if im being honest i dont know how to fix this

preview: https://cdn.calones.xyz/15aab11fd8273480.mp4

code:

local humanoid = character:FindFirstChild("Zombie")
local humroot = character:FindFirstChild("HumanoidRootPart")

local pathfindingservice = game:GetService("PathfindingService")
local path = pathfindingservice:CreatePath({
	AgentRadius = 1,
	AgentHeight = 4,
	AgentCanJump = true,
	AgentCanClimb = true,
	WaypointSpacing = 8,
	Costs = {
		Climb = 2,
	}
})

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local follow = true

local function visualizeWaypoint(waypointPosition)
	local visual = Instance.new("Part")
	visual.BrickColor = BrickColor.Red()
	visual.Anchored = true
	visual.Size = Vector3.new(0.5, 0.5, 0.5)
	visual.CanCollide = false
	visual.Transparency = 0.5
	visual.Position = waypointPosition
	visual.Parent = workspace
end

local function followpath(destination)
	local success,errormsg = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position,destination)
	end)
	
	if success 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
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
					visualizeWaypoint(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
		
		
		nextWaypointIndex = 2
		humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		visualizeWaypoint(waypoints[nextWaypointIndex].Position)
	else
		warn("error lol: ", errormsg)
	end
end

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

coroutine.wrap(function()
	local target = findNearestTorso(humroot.Position)
	while follow == true and target do
		task.wait()
		followpath(target.Position)
	end
end)()

i just need to figure out how to fix this so that’s my achivement

I just searched ‘make npc climb truss’ and found the announcement from November:

1 Like

Hi popbob,

Thank you for reporting.
Could you share a repo of a rbxl file? Then I can look into this issue.

Thank you.

pathfinding rbxl.rbxl (57.5 KB)
the rbxl to it

1 Like

Thank you. I will take a look as soon as I can.

pathfinding-with-test-pather.rbxl (61.2 KB)

Looks like the followpath is keeping calling path:ComputAsyc too many times. Somehow this case the problem.
Check the rblx file I uploaded. I add a workable code to game.workspace.Test. Which can move the Zombie and climb the truss to the destination. You can take a look.

um that’s great that you figured it out but how would i make it so it’d be able to find players constantly in a
loop while not doing the thing earlier?


I debugged and found the reason why the NPC is wandering at the bottom of the TRUSS. Pathfinding marks this path as passable by generating a ClimbLink from the bottom of the TRUSS to the top. In the screenshot, the blue circle under the TRUSS is the starting point of the ClimbLink, but there is a small distance between it and the TRUSS. When the NPC is between the TRUSS and the starting point, it turns around and walks to the starting point before climbing up the TRUSS.
Because your loop is constantly calling FindPath and MoveTo, the NPC keeps walking towards the TRUSS and is pulled back to the starting point of the ClimbLink.
A quick fix is to use task.wait(1) instead of task.wait(), and don’t call followPath when the NPC is climbing.

1 Like

thank you so much that fixed it :pray:t6:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.