PathfindingService

Show Deprecated
Not Creatable
Service
Not Replicated

PathfindingService is used to find logical paths between two points, ensuring that characters can move between the points without running into walls or other obstacles. By default, the shortest path is calculated, but you can implement pathfinding modifiers to compute smarter paths across various materials, around defined regions, or through obstacles.

See Character Pathfinding for usage details.

Summary

Methods

Properties

Methods

CreatePath

Creates a Path object based on various agent parameters. Valid keys and values in the agentParameters table are as follows:

KeyTypeDefaultDescription
AgentRadiusinteger2Determines the minimum amount of horizontal space required for empty space to be considered traversable.
AgentHeightinteger5Determines the minimum amount of vertical space required for empty space to be considered traversable.
AgentCanJumpbooleantrueDetermines whether jumping during pathfinding is allowed.
AgentCanClimbbooleanfalseDetermines whether climbing TrussParts during pathfinding is allowed.
WaypointSpacingnumber4Determines the spacing between intermediate waypoints in path.
Coststable{}Table of materials or defined PathfindingModifiers and their "cost" for traversal. Useful for making the agent prefer certain materials/regions over others. See here for details.

Parameters

agentParameters: Dictionary

Lua table which lets you fine-tune the path for the size of the agent (the humanoid that will move along the path). See above for valid keys, types, and descriptions.

Default Value: "nil"

Returns

A Path object.

Code Samples

Creating a Path with Pathfinding Service

local Workspace = game:GetService("Workspace")
local PathfindingService = game:GetService("PathfindingService")
-- This model contains a start, end and three paths between the player can walk on: Snow, Metal and LeafyGrass
local PathOptionsModel = script.Parent.PathOptions
local startPosition = PathOptionsModel.Start.Position
local finishPosition = PathOptionsModel.End.Position
-- Create a path that avoids Snow and Metal materials
-- This will ensure the path created avoids the Snow and Metal paths and guides
-- the user towards the LeafyGrass path
local path = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 6,
AgentCanJump = false,
Costs = {
Snow = math.huge,
Metal = math.huge,
},
})
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(startPosition, finishPosition)
end)
-- Confirm the computation was successful
if success and path.Status == Enum.PathStatus.Success then
-- For each waypoint, create a part to visualize the path
for _, waypoint in path:GetWaypoints() do
local part = Instance.new("Part")
part.Position = waypoint.Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.new(1, 0, 1)
part.Anchored = true
part.CanCollide = false
part.Parent = Workspace
end
else
print(`Path unable to be computed, error: {errorMessage}`)
end

FindPathAsync

Yields

This function is used to find a Path between two provided points. This path uses the navigation grid created by PathfindingService and makes sure that the path can be followed by a regular-sized Roblox character.

This function returns a Path object which contains the coordinates of the path. If no path is found between the two points, this function will still return a Path object, but that object's Path.Status will be Enum.PathStatus.NoPath.

To get the waypoints of a Path object, you can use the Path:GetWaypoints() function.

Parameters

start: Vector3

Path start coordinates.

finish: Vector3

Path finish coordinates.


Returns

A Path object.

Events