PathWaypoint is a datatype that contains information about the points that make up a Path. This includes both the Position of the point as well as the Action.
The action shows what action a character would need to do in order to reach the next waypoint in the path. For most waypoints the action will be Walk, meaning the character can simply walk to the next waypoint. In some cases however, the action can be Jump, which means that the character has reached a ledge or a gap and will need to jump to reach the next point.
PathWaypoints cannot be constructed. Instead, they are created in the array returned by the GetWaypoints function of Paths.
The action to perform in order to reach this waypoint.
For most waypoints the action will be Walk, meaning a character can simply walk to this waypoint. In some cases, however, the action can be Jump, which means that the character has reached a ledge or a gap and will need to Jump to reach the next point.
The position of the waypoint.
When FindPathAsync creates Paths, it will space the waypoints out. In general, waypoints will be about 3 studs apart from one another. If the waypoints are separated by a jump they may be further apart, or if the waypoints are going around a corner they may be closer together.
This example creates a path between two points and creates parts at every waypoint along that path. The code uses the Position property of the PathWaypoints to position the parts.
-- Make variables for Roblox services local PathfindingService = game:GetService("PathfindingService") -- Make variables for the start and end of the path local start = Vector3.new(0, 1, 0) local finish = Vector3.new(25, 1, -60) -- Find the path and get the waypoints local path = PathfindingService:FindPathAsync(start, finish) local waypoints = path:GetWaypoints() -- Loop through all of the waypoints in the path for waypointIndex, waypoint in pairs(waypoints) do -- Create a part for each waypoint and position the part -- at the waypoint's position. local part = Instance.new("Part") part.Size = Vector3.new(1, 1, 1) part.Position = waypoint.Position part.CanCollide = false part.Anchored = true part.Parent = game.Workspace end
In this example, an NPC travels between two points, following paths created by PathfindingService. When following the path, the NPC looks at the Action of the waypoints it is trying to reach. If it encounters a waypoint it cannot get to by walking, the NPC’s Humanoid will be instructed to jump.
This code can be seen in action here. The place is not copy locked.
-- Make variables for Roblox services local PathfindingService = game:GetService("PathfindingService") -- Make variables for the NPC and some of its components local wanderBot = game.Workspace.WanderBot local rootPart = wanderBot.HumanoidRootPart local humanoid = wanderBot.Humanoid -- Make variables for the places the NPC will walk to local goalA = game.Workspace.GoalA.Position local goalB = game.Workspace.GoalB.Position -- Function that moves the NPC to the passed in point local function moveToPoint(point) -- Find a path to the destination and get the waypoints for the path local path = PathfindingService:FindPathAsync(rootPart.Position, point) local waypoints = path:GetWaypoints() -- Loop through all of the waypoints in the path for currentPoint = 1, #waypoints do -- Get the next waypoint to move to and move the humanoid towards it local waypoint = waypoints[currentPoint] humanoid:MoveTo(waypoint.Position) -- Check the action of the next waypoint. If it is a jump waypoint -- then make the NPC jump if waypoint.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end -- Wait until the NPC has reached the next waypoint local reachedTarget = humanoid.MoveToFinished:Wait() end end -- Move the NPC between the two points in a loop while true do moveToPoint(goalA) wait(2) moveToPoint(goalB) wait(2) end