FindPathAsyncYieldFunction of PathfindingService
Description: The FindPathAsync function is used to find a path between the two provided points. This path uses the navigation grid created by the 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 Status will be set to NoPath.
In this example PathfindingService is used to find a path between two parts. The path is then drawn using LineHandleAdornments. The path will be colored in yellow for the segments where a character would need to jump.
To see this example in action, please visit this place. The place is not copy-locked so it can be opened in Studio by anyone.
-- Make variables for Services local PathfindingService = game:GetService("PathfindingService") -- Variables for the parts representing the start and end of the path local start = game.Workspace.Start local finish = game.Workspace.Finish -- Calculate the path and get the waypoints local path = PathfindingService:FindPathAsync(start.Position, finish.Position)local waypoints = path:GetWaypoints() -- Cycle through all of the waypoints and draw a segment between each for i = 2, #waypoints do -- Get the current and last waypoint in the path local lastWaypoint = waypoints[i - 1] local currentWaypoint = waypoints[i] -- Create a LineHandleAdornment. This adornment will be attached to the starting -- point part, so the CFrame of the adornment has to be calculated relative to -- the starting part. local segment = Instance.new("LineHandleAdornment") local lastPosition = lastWaypoint.Position + Vector3.new(0, 0.5, 0) - start.Position local currentPosition = currentWaypoint.Position + Vector3.new(0, 0.5, 0) - start.Position local toCurrent = currentPosition - lastPosition local distance = toCurrent.Magnitude -- Style and attach the adornment segment.CFrame = CFrame.new(lastPosition, currentPosition) segment.Parent = start segment.Adornee = start segment.Thickness = 10 segment.Length = distance -- Color the adornment based on the action of the waypoints. Green indicates walking, -- yellow indicates a jump. local waypointType = currentWaypoint.Action if waypointType == Enum.PathWaypointAction.Jump then segment.Color3 = Color3.new(1, 1, 0) else segment.Color3 = Color3.new(0, 1, 0) end end