ComputeAsync
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts.
This function computes a Path
from a start position to an end position. This function is not automatically called when a path is created and must be invoked each time the path needs to be updated.
Once the Path is computed, it will have a series of waypoints that, when followed, can lead a character along the path. These points are gathered with the Path/GetWaypoints
function.
See also
articles/Pathfinding
, provides an in-depth pathfinding walkthrough
Parameters
Name | Type | Default | Description |
---|---|---|---|
|
The world position where the computed path begins |
||
|
The world position where the computed path finishes |
Returns
Return Type | Summary |
---|---|
No return |
Code Samples
Using the Pathfinding Service
The code sample below explores how to move an NPC along a more complex path or around obstacles. This is known as pathfinding.
-- 1. First, create a path with the parameter set local agentParams = { AgentRadius = 2.0 AgentHeight = 5.0 AgentWalkableClimb = 2.0 AgentCollisionGroupName = "RedPlayers" CollectionWeights = { Bridge = 2.5, Minefield = math.huge } MaterialWeights = { Water = 1.5 } AgentCanJump = false } local path = PathfindingService:CreatePath(agentParams) -- 2. Listen to path blocked events path.Blocked:Connect(OnPathBlocked) -- 3. Compute the path path:ComputeAsync(humanoidPos, targetPosition) -- 4. When the path is blocked... function OnPathBlocked(blockedWaypointIdx) -- Check if the obstacle is further down the path if blockedWaypointIdx > currentWaypointIdx then -- Recompute the path path:ComputeAsync(updatedHumanoidPos, targetPosition) if path.Status == Enum.PathStatus.Succeess then -- Retrieve update waypoint list with path:GetWaypoints() -- and Continue walking towards target else -- Error, path not found end end end