Roblox Studio · Lesson 24

Auto-driving: allow robots to bypass barriers

Use PathfindingService to calculate waypoints and handle jumps, nowhere to go, and blocked paths.

10–15 years old 75 minutes PathfindingService Hands-on practice Testing and Saving

Course overview

When the robot sees the wall, it can plan a way around it.

We saw with our own eyes in the last class that walking directly towards the target may be blocked by a wall. PathfindingService will calculate a series of waypoints based on the traversable area, and then Humanoid will walk through them one by one.

Planning and walking success are still two things. The walls move, the roads can be blocked. You have to let the robots admit to failure, not keep stuck.

01 · BEFORE

Before starting

Complete Lesson 23, save as Lesson24; disable the old Patrol to avoid two scripts commanding the robot at the same time.

02 · MAKE

Your work

The robot bypasses a static wall and walks to the Goal; it stops safely when there is no path or it is blocked on the way.

03 · CHECK

Complete flag

Ability to check PathStatus, handle jump waypoints, and output completion messages at the endpoint.

Official screenshot of Studio showing navigation grid
Figure 24-1: Screenshot of the official navigation grid: the walkable area can help you find scene problems such as doors being too narrow and passages disconnected.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Learning route

Prepare first, then do it; after seeing the result, remember to save it.

Every time a small step is completed, it is tested. Say what you expect to see first, and then use Play and Output to check it. Don't rush to write all the code at once.

1

01 · Leave a real path for the robot to follow

Keep the PatrolBot and put a wall in front of it so that at least one of the left and right sides can go around. Don’t stuff your target into a closed box and expect the algorithm to walk through the wall.

2

02·Plan first, then check the results

AgentRadius and AgentHeight in CreatePath describe the space required by the robot. If a passage is too narrow, the planner should not consider it a passable route.

3

03 · Walk points in sequence, jumping if necessary

Roadpoint contains position and action. When Action is Jump, let Humanoid. Jump=true move to the road point.

4

04 · Look at the navigation grid, not just the beautiful ground

Studio's visualization options can display Navigation Mesh. It helps you see where the system thinks you can walk, rather than just seeing where the surface of the model seems to be connected.

5

05 · Give a clear answer to the dead end

A reliable robot does not need to succeed every time, but it must let you know why it stopped. No available paths, movement timeouts, and blocked paths are all normal situations that need to be handled.

Step by step

01 · Leave a real path for the robot to follow

Keep the PatrolBot and put a wall in front of it so that at least one of the left and right sides can go around. Don’t stuff your target into a closed box and expect the algorithm to walk through the wall.

Insert Goal waypoint and disable old Patrol. The same Humanoid receives MoveTo from two scripts at the same time, making the results difficult to understand.

  1. Goal: Position=0,0.5,-30, Size=4,1,4, Anchored=true, CanCollide=false.
  2. Wall: Position=0,5,-15, Size=12,10,2, Anchored=true.
  3. Create PathToGoal in ServerScriptService.
  4. Confirm that the ground near the robot is wide enough and that there is space for detours on both sides of the wall.

Step by step

02·Plan first, then check the results

AgentRadius and AgentHeight in CreatePath describe the space required by the robot. If a passage is too narrow, the planner should not consider it a passable route.

ComputeAsync may go wrong, so wrap it with pcall; after the calculation, you also need to check whether path.Status is Success. The success of pcall only means that the call did not throw an error, but it does not mean that there is a way.

  1. Reads two levels of the full code check with PathStatus.
  2. Start with the R15 Rig at the default scale and then adjust the parameters based on the character size.

Step by step

03 · Walk points in sequence, jumping if necessary

Roadpoint contains position and action. When Action is Jump, let Humanoid. Jump=true move to the road point.

Blocked only cares about the current or rear waypoints that will be passed. If the route ahead is blocked, this example stops and prompts for a retest; it does not implement dynamic re-planning to avoid turning the introductory course into chasing AI.

  1. Put the complete code into PathToGoal.
  2. After playing, watch the robot go around the wall and reach the Goal.
  3. If this fails, check Output and then check the ground, wall space and role size.
ServerScriptService > PathToGoal · Complete scriptLuau
local PathfindingService = game:GetService("PathfindingService")
local bot = workspace:WaitForChild("PatrolBot")
local humanoid = bot:WaitForChild("Humanoid")
local root = bot:WaitForChild("HumanoidRootPart")
local goal = workspace:WaitForChild("Goal")
root:SetNetworkOwner(nil)
humanoid.WalkSpeed = 8
local path = PathfindingService:CreatePath({
    AgentRadius = 2,
    AgentHeight = 5,
    AgentCanJump = true,
})
local ok, message = pcall(function()
    path:ComputeAsync(root.Position, goal.Position)
end)
if not ok or path.Status ~= Enum.PathStatus.Success then
    warn("No usable path:", message, path.Status.Name)
    return
end

local nextIndex = 2
local blocked = false
local connection = path.Blocked:Connect(function(index)
    if index >= nextIndex then
        blocked = true
        humanoid:MoveTo(root.Position)
    end
end)
local finished = true
local waypoints = path:GetWaypoints()
for index = 2, #waypoints do
    nextIndex = index
    if blocked or humanoid.Health <= 0 then finished = false; break end
    local waypoint = waypoints[index]
    if waypoint.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end
    humanoid:MoveTo(waypoint.Position)
    local reached = humanoid.MoveToFinished:Wait()
    if not reached or blocked then finished = false; break end
end
connection:Disconnect()
if finished then print("Reached goal") else warn("Route interrupted; test a new route") end
Visualization of roadpoints generated by auto-searching
Figure 24-2: Official waypoint diagram: the path consists of multiple waypoints. Waypoint actions are read one by one to handle jumps correctly.Roblox Creator Documentation · CC BY 4.0 · The original image has not been modified · Description of the permit

Step by step

04 · Look at the navigation grid, not just the beautiful ground

Studio's visualization options can display Navigation Mesh. It helps you see where the system thinks you can walk, rather than just seeing where the surface of the model seems to be connected.

The colored areas and waypoints in the picture belong to official pathfinding examples. Your wall is simpler than Goal, but checked in the same way: there's enough room to go around it.

  1. Enable the navigation grid display and observe the pathways on both sides of the wall.
  2. Narrow the channel and calculate it again to see how the result changes.
  3. After completing the inspection, the grid display can be turned off to restore clarity to the scene.

Step by step

05 · Give a clear answer to the dead end

A reliable robot does not need to succeed every time, but it must let you know why it stopped. No available paths, movement timeouts, and blocked paths are all normal situations that need to be handled.

This example is suitable for fixed small scenes. Continuously tracking a moving player requires throttling recalculations, canceling old tasks, and handling streaming loads, and you cannot simply call ComputeAsync every frame.

  1. Open side: The detour should be successful.
  2. Fully enclosed target: No usable path should be reported.
  3. Restoring gaps: Playing again should succeed.
  4. The Blocked connection has been disconnected after confirmation.

Homework

It's your turn to change and make your own version.

1 Two bypass roads

Leave passages from the left and right sides of the wall respectively, and observe the path selection.

2 Failure is also a result

Record a roadless test, a screenshot and an indication of how you repair the map.

3 Choosing: A small step

Add small steps that can be jumped and observe the Jump waypoint; keep the safe ground below.

Completion criteria:First verify the work in Playtest, then click Stop and save it as "Initials_Lesson24" or a copy of the corresponding Homework; do not overwrite the runnable version of the previous lesson.

Exit questions

Answer it yourself first, and then expand on it.

If pcall returns true, will it definitely find a way?

No, you also need to check whether path.Status is Success.

Why look at waypoint.Action?

Some waypoints require jumping, and only MoveTo may not necessarily complete the action.

Will this example automatically re-plan after the wall is moved?

No, it will stop and report. Dynamic recalculation is a further feature.

Why disconnect Blocked?

After the task is completed, monitoring is no longer required to avoid retaining expired callbacks.

Explore further

This is where we start when we need to check the data.

The text code can be copied directly. First confirm the script type, placement, and whether this code is a complete version or a small experiment; do not run multiple versions of the same script superimposed. The source of the interface reference picture has been marked. The object names in the picture may be different from those in this lesson. Please refer to the operation steps.

Download the code and placement instructions for this lesson