I have recently been messing around with the built-in pathfinding service on Roblox to make an npc system. Most of the time it seems to be working just fine, but sometimes it acts really weird. For example, npcs often try to walk through walls or fail to avoid obvious obstacles in their path.
Why is this happening and how could I fix it? If anyone knows what I’m doing wrong please enlighten me, as I have been trying to fix this issue for quite a long time now and I can’t seem to figure it out.
I have pasted the function that computes each path down below, which in my opinion must be the block of code where the problem originates from. I have also attached the entire script to this post in case anybody would like to see the whole thing. I also uploaded footage of npcs trying to walk through walls.
Thank you to anyone who’s willing to help, I really appreciate it!
Here is the function I mentionned earlier:
local function compute(startPart, endPart)
local path = PathfindingService:CreatePath({
AgentRadius = 1.5,
AgentHeight = 5.5,
AgentCanJump = false,
AgentCanClimb = true,
WaypointSpacing = 50
})
local success, errorMessage = pcall(function()
path:ComputeAsync(startPart.Position, endPart.Position)
end)
if success then
local waypoints = path:GetWaypoints()
return waypoints
else
warn(errorMessage)
end
end
local function followPath(character, startPart, endPart)
local humanoid = character:WaitForChild("Humanoid")
local waypoints = compute(startPart, endPart)
local nextWaypointIndex
local reachedConnection
local blockedConnection
if waypoints and #waypoints > 1 then
local finished = false
local distance = calculateDistance(waypoints)
local timeStamp = os.clock()
-- Detect when movement to next waypoint is complete
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
local currentWaypoint = waypoints[nextWaypointIndex]
if currentWaypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:MoveTo(currentWaypoint.Position)
task.wait(0.15)
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
else
humanoid:MoveTo(currentWaypoint.Position)
end
else
reachedConnection:Disconnect()
finished = true
end
end)
-- Initially move to the first waypoint
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
repeat
task.wait()
until finished == true
-- Clean up connections
if reachedConnection then
reachedConnection:Disconnect()
end
end
end
Here is the footage:
And here is the full script for those who are interested:
This is the Agents list from one of the bots I’ve made. Notice the last few, the H is for heavy or “high”, the bot will stay away from things with those materials as much as possible. The Cost definitions.
local Agents={WaypointSpacing=(5),
AgentRadius=(1),AgentHeight=(0),
AgentCanJump=(false),AgentCanClimb=(false),
Costs={Metal=H,Basalt=H,LeafyGrass=H,SmoothPlastic=H}
}
Just need to be a bit tricky where you place your “bot blockers”, to get them going to right ways.
You can also create alt materials and use them if needed. I’m sure there are other ways but this worked out for me.
Hello, thank you for the tip! I have already thought of a similar fix but came to the conclusion that it would take forever to manually block every single wall/obstacle in the game, and anyways it wouldn’t really fix the problem. I really have no clue why pathfinding is even attempting to make the npcs walk through such obvious obstacles in the first place. It doesn’t make any sense…
Because the path chosen isn’t considering the walls. Figured that was the point of the Costs for Pathfinding. You could make them transparent also. I use this in a maze game; the walls could be any one of the ones listed. Running 5 bots they navigate through complex mazes like pros.
Pretty sure all you need are blocks on the floor. Like if you made blocks the lined lower part of the walls. I may be wrong about that but, that seems to come to mind.
If the points didn’t lead to a walk into the wall they wouldn’t walk into the walk also. Good Luck!
Hi, I’m not sure if this is solved or not but I see an issue is the waypoint spacing, your NPC is trying to create waypoints that are way too far apart, which may cause it to walk through walls. Modify it to be a smaller value near 1-4 for good accuracy.
Use a PathfindingModifier and Map out areas where the pathfinding cannot go to.
Create Thicker Walls.
If these methods still don’t work for you here is the last one.
Experimental Method
This method would make life easier depending on your skill, create your own pathfinding.There are many videos on you tube. or here are some web-sources I found that may help you.
@leplusdetunepossible
Would you mind post a repo?
I downloaded your .rblx file, but it only contains code. After running it, I wasn’t able to reproduce the issue you mentioned about going through walls.