Pathfinding NPC often slams into corners or walls sometimes

Basically, I have a Bot that patrols the Map, and there’s waypoints around areas, the bot will sometimes not detect physical objects and run into them when moving to the point it’s going to, I tried raycasting the nearest point, didn’t work, the npc would only stop once it reached a point with a wall in front of it, so I scrapped that idea, there has to be another way, like making nodes that avoid the objects or something?? I have no clue, as I am new to Pathfinding stuff. Here’s the code;

function BotModule.Patrol(bot, pathparams)
local PathfindingService = game:GetService(“PathfindingService”)
local RunService = game:GetService(“RunService”)

local State = bot:FindFirstChild("State")
local Humanoid = bot:FindFirstChild("Humanoid")

local PatrolTable = {}

if not workspace:FindFirstChild("PatrolPoints") then
	error("Missing Patrol Point Folder; must be in workspace.")
	return
end

if workspace:FindFirstChild("PatrolPoints") then
	for i, patrolpoint in pairs(workspace:FindFirstChild("PatrolPoints"):GetChildren()) do
		if patrolpoint:IsA("BasePart") and patrolpoint:FindFirstChildWhichIsA("Decal") then
			patrolpoint.Transparency = 1
			patrolpoint:FindFirstChildWhichIsA("Decal"):Destroy()
		end
	end
end

if workspace:FindFirstChild("TrapPoints") then
	for i, trappoint in pairs(workspace:FindFirstChild("TrapPoints"):GetChildren()) do
		if trappoint:IsA("BasePart") then
			trappoint.Transparency = 1
		end
	end
end

for index, patrolPoint in pairs(workspace:FindFirstChild("PatrolPoints"):GetChildren()) do
	if patrolPoint:IsA("BasePart") then
		table.insert(PatrolTable, patrolPoint)
	end
end

spawn(function()
	while bot.State.Value == "Patrolling" do
		local patrolPath = PathfindingService:CreatePath(pathparams)
		
		local randomPoint = math.random(1, #PatrolTable)
		patrolPath:ComputeAsync(bot.HumanoidRootPart.Position, PatrolTable[randomPoint].Position)
	for index, waypoint in pairs(patrolPath:GetWaypoints()) do
		if State.Value == "Patrolling" then
			if waypoint.Action == Enum.PathWaypointAction.Walk then
				    Humanoid:MoveTo(waypoint.Position)
					Humanoid.MoveToFinished:Wait()
				elseif waypoint.Action == Enum.PathWaypointAction.Jump then
					Humanoid.Jump = true
					Humanoid:MoveTo(waypoint.Position)
				end
			end
		end
		wait(.1)
	end
end)

end

I think there was a way to train the path finding but I don’t know how.

what does that mean?? I never heard of training the pathfinding, but if it works and if you figure out how, then I could try using it.

Have you tried using Path params? You can set the agent radius bigger and maybe that could help with the pathfinding but you can look at other forum post about it.

it’s a module connected to a server script in the bot that inserts the path Params into it, yeah. I don’t wanna set the radius to be too large or it won’t be able to enter certain areas on the map.

You could try and show us a video of it. Would be more helpful

Well judging from the video it seems like the Npc just gets stuck from how big he is so maybe you could just try and turning off the collisions. Pathfinding can be easier based on the model and the map too.

Many Roblox worlds are dynamic; parts might move or fall and floors may collapse. This can block a computed path and prevent the character from reaching its destination. To handle this, you can connect the Path.Blocked event and re-compute the path around whatever blocked it.

Paths may also become blocked somewhere behind the agent, such as a pile of rubble falling on a path as the agent runs away, but that doesn’t mean the agent should stop moving. The conditional statement on line 31 makes sure that the path is re-computed only if the blocked waypoint is ahead of the current waypoint.

Code sample:

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local path = PathfindingService:CreatePath()

local player = Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local TEST_DESTINATION = Vector3.new(100, 0, 100)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function followPath(destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				followPath(destination)
			end
		end)
	end
end

like what collision should I turn off specifically?

Just turn off cancollide mb, also I think you should check out the post I made above, it may help.

well I turned off the can touch on the arms and stuff, it works fine now.

Never mind this did not resolve the issue, plus Path.Blocked isn’t all that reliable.

I recommend that you give it a shot.

Didn’t work, Path.Blocked doesn’t do anything in this case.