I copied roblox's pathfinding AI function and it works except for when he got stuck on a wall

bro
are you kidding me lol

local path = pathfindingService:CreatePath()
		local character = script.Parent
		local humanoid = character:WaitForChild("Humanoid")

		local waypoints
		local nextWaypointIndex
		local reachedConnection
		local blockedConnection

		local function followPath(destination)
			print("Searching...")
			-- 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)

				-- Detect when movement to next waypoint is complete
				if not reachedConnection then
					reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
						if reached and nextWaypointIndex < #waypoints then
							-- Increase waypoint index and move to next waypoint
							nextWaypointIndex += 1
							humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
						else
							reachedConnection:Disconnect()
							blockedConnection:Disconnect()
						end
					end)
				end

				-- Initially move to second waypoint (first waypoint is path start; skip it)
				nextWaypointIndex = 2
				humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
			else
				warn("Path not computed!", errorMessage)
			end
		end

i literally just copied this and it works fine except for that
does it ignore walls? or is it just unable to reach the next waypoint or
idek
what do i need to change? the distance between waypoints in the createpath() table thing?

I have the same problem. Maybe change something where the script checks if the path is blocked? Please message me if you find the solution. :confused:

1 Like

You need to define AgentRadius:
https://developer.roblox.com/en-us/api-reference/function/PathfindingService/CreatePath

2 Likes