My Pathfinding NPC is walking into the walls

Hello, I was working recently on an NPC that’s supposed to move & walk in a store as an employee, and the only way that I know to move an NPC or a character is PathfindingService but the problem is after I finished all the scripting stuff with the Invisible Parts and Pathfinding Modifiers, there was a problem that the NPC is sometimes walking into the walls at specific points and not re-calculating the path in a correct way.

That’s an Image showing what happens:

image

I tried searching on all the Devforum and the DevHub to find a solution but none worked.

3 Likes

Can you show us the script that calculates the path or any errors you’re getting?

1 Like
local RandomChosenRestock = RestockWaypoints[math.random(1,#RestockWaypoints)]

					if RandomChosenRestock:IsA("BasePart") then

						local success, errorMessage = pcall(function()
							path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
						end)

						local currentRestockWaypoint = 0

						if success then

							boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
						else

							local success, errorMessage = pcall(function()
								path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
							end)

							if success and path.Status == Enum.PathStatus.Success then

								boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
							end
						end
					end

The function part:

for i,RestockPathWaypoint in pairs(PathWaypoints) do

		if currentRestockWaypoint < #PathWaypoints then

			idle1:Stop()
			idle2:Stop()

			humanoid:MoveTo(RestockPathWaypoint.Position)
			
			if not walkAnim.IsPlaying then

				walkAnim:Play()
			end

			humanoid.MoveToFinished:Wait()

			currentRestockWaypoint += 1
end
1 Like

Alvinblox had the same problem with pathfinding in this post. (Although I don’t think he ever solved the problem either).
I’ve also had issues with pathfinding in the past, and I think your best bet would be to use A* pathfinding and generate nodes around the store, removing nodes that are inside or too close to furniture.

You can find a working example implemented in Roblox here.
For a more detailed explanation on how A* pathfinding works, I recommend reading this article.

Try giving the NPC a higher AgentRadius

local Path = PathfindingService:CreatePath({
    AgentRadius = 3
})

There is a AgentRadius parameter in the CreatePath function of PathfindingService class; try adjusting it.

I tried but it didn’t work, I also tried changing the costs but none worked

Why is this line here? From the looks of it, the NPC will never reach the end.

Also, have you tried putting invisible walls at the sides of the shelf, or making them thicker?

1 Like

Use PathfindingModifiers instead, and create invisible parts around the furniture etc.

I tried Pathfinding Modifiers and Invisible Walls but none worked, unfortunately. I think I’m gonna try the method that amadeupworld2 talked about

Can you send the :CreatePath part of the code? And also, can you send everywhere that you use “currentRestockWaypoint”?

:CreatePath() part:

local path = PathfindingService:CreatePath({AgentRadius = 2,AgentHeight = 6,AgentCanJump = false,WaypointSpacing = 2, -- setup the path

	Costs = {EmployeesRoomDoor = 1,FreezerRoomSplitters = 1,FreezerRoomWall = 1,MiddleShelves = 1,RightShelves = 1,OpenedFreezers = 1,
		RightPart = 1, LeftPart = 1, PathfindingPart = 1
	}})

everywhere I used currentRestockWaypoint:
the code continues here but I didn’t put it because there is things that aren’t related to the topic

local PathWaypoints = path:GetWaypoints()

		for i,RestockPathWaypoint in pairs(PathWaypoints) do

			if currentRestockWaypoint < #PathWaypoints then

				idle1:Stop()
				idle2:Stop()

				humanoid:MoveTo(RestockPathWaypoint.Position)

				if not walkAnim.IsPlaying then

					walkAnim:Play()
				end

				humanoid.MoveToFinished:Wait()

				currentRestockWaypoint += 1

				if currentRestockWaypoint >= #PathWaypoints then

					local orientation = RestockOrientations[RandomChosenRestock.Name]
					local newCF = humanoidRootPart.CFrame * CFrame.Angles(math.rad(0),math.rad(orientation.Value.Y-humanoidRootPart.Orientation.Y),math.rad(0))

					walkAnim:Stop()
					currentRestockWaypoint = 0

second part:
RandomChosenRestock is another table of waypoints so don’t worry about it

if RandomChosenRestock:IsA("BasePart") then
					
					local currentRestockWaypoint
					
					if computePaths() then
						
						local waypoints = path:GetWaypoints()
						
						currentRestockWaypoint = 1
						
						for i,waypoint in pairs(waypoints) do
							
							if currentRestockWaypoint < #waypoints then
								
								humanoid:MoveTo(waypoint.Position)

								currentRestockWaypoint += 1

								if not walkAnim.IsPlaying then

									walkAnim:Play()
								end

								humanoid.MoveToFinished:Wait()
								
							else
								
								walkAnim:Stop()
								idle1:Play()
								idle2:Play()
								
								local function moveNPCtoRestock()
									
									local success, errorMessage = pcall(function()
										path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
									end)

									if success and path.Status == Enum.PathStatus.Success then

										boxThings(path,currentRestockWaypoint,RandomChosenRestock,TweenIO)
									else
										local success, errorMessage = pcall(function()
											path:ComputeAsync(humanoidRootPart.Position,RandomChosenRestock.Position)
										end)
										
										moveNPCtoRestock()
									end
								end
								
								moveNPCtoRestock()
							end
						end
					end
				end

I assume you’ve already tried increasing this to 4 or 6, maybe even 10?

I honestly have no idea why this is happening. I can’t reproduce this strange bug. Though, something that I do find strange is that WaypointSpacing is 2, but in the image the waypoints are much further apart.

The only thing I think could be happening is that the NPCs are far bigger than regular characters, though I doubt they are. I think you’re better off making a custom node-based pathfinding implementation.

1 Like

Correct, I made my own A* pathfinder algorithm. Feel free to DM me if you want to know how I did it because I struggled before I made it. @MarwanVRG4

7 Likes