Med Written By LoveTheBears101

Pathfinding: Creating a Path Created on: 29-12-2019

Learn how to create a path using PathFindingService.

Hi! This tutorial is about creating a path and showing the path's WayPoints using PathFinding Service.

Setting up the Workspace

In the workspace, we need a part named Start, a part named Finish, some obsticles (you can name these what ever you want), and a folder named Points. Then, create and open a script. You can call the script what ever you want > Note: spelling and capatilization DOES matter. Make sure you spell the names correctly!

Your Explorer should look something like this now:





Step 1:

Since PathFindingService is a Service, we need to call GetService.

local PathService = game:GetService("PathfindingService")

Step 2:

Now, lets create an array named parameters for the path. This uses AgentParameters to figure out what the best path is.

local parameters = {
	AgentRadius = 2, --The radius of the person/object
	Agent Height = 5, --The Height of the person/object
	AgentCanJump = true --Wether the person/object can jump
}

Step 3:

We want an infinite loop so that if we move a part, it recalculates a path. We will do this with a while true do loop

while true do
	--Code Here
end

Step 4:

Lets create the path! we do this with the :Create Path and ComputeAsync functions of PathService. Put this inside of the while true do loop

while true do
NewPath = game:GetService("PathfindingService"):CreatePath(parameters)
	path = NewPath:ComputeAsync(game.Workspace.Start.Position, game.Workspace.Finish.Position, 200)
end

What this does is it creates a path using the Agent Parameters we created, then it computes a path using Compute Async using the positions of the two parts we created earlier. The third property of Compute Async is Max Distance. We set it to 200, most games shouldn't need to go over that.

Step 5:

Now, lets get all the Points between the start and the finish. We do this by calling GetWaypoints(), a function of Path. It returns an array of all the points.

while true do
	NewPath = game:GetService("PathfindingService"):CreatePath(parameters)
	path = NewPath:ComputeAsync(game.Workspace.Start.Position, game.Workspace.Finish.Position, 200)
	points = NewPath:GetWaypoints()
end

Step 6:

Now, we need the clear all the children of the folder Points since that is where we store the parts on the path. Add the line into the loop:

game.Workspace.Points:ClearAllChildren()

Step 7:

Almost done! Now, we need a loop to place parts at all the waypoints, we do this with a for loop. Put the for loop into the while true do loop so it looks like this:

while true do
	NewPath = game:GetService("PathfindingService"):CreatePath(parameters)
	path = NewPath:ComputeAsync(game.Workspace.Start.Position, game.Workspace.Finish.Position, 200)
	points = NewPath:GetWaypoints()
	game.Workspace.Points:ClearAllChildren()
	for point = 1, #points do

	end
end

This runs the loop for each item in the array points and changes the value of points by +1 each time. We created the array points in the last step to get all the waypoints.

Step 8:

Now, lets make it add a part for each waypoint.

while true do
	NewPath = game:GetService("PathfindingService"):CreatePath(parameters)
	path = NewPath:ComputeAsync(game.Workspace.Start.Position, game.Workspace.Finish.Position, 200)
	points = NewPath:GetWaypoints()
	game.Workspace.Points:ClearAllChildren()
	for point = 1, #points do
		part = Instance.new("Part")
		part.CanCollide = false
		part.Name = "Point"
		part.Position = points[point].Position
		part.Anchored = true
		part.BrickColor = BrickColor.White()
		part.Size = Vector3.new(1,1,1)
		part.Parent = game.Workspace.Points
	end
	
end

What this does it it creates a new Instance of the class part. It sets can collide to false, names it Point, puts it at the position of the WayPoint, Anchors it, sets it's BrickColor to white, and makes it's Parent the folder we created named Points

Step 9: The most important step

This step may not seem important, but it is. we need to add a wait(0.1) at the end of the infinite loop to stop Roblox Studio from crashing. Add this right after the for loop ends:

wait(0.1)

The Final Product:

The final code should look like this:

local pathFindingService = game:GetService("PathfindingService")
local parameters = {
	AgentRadius = 2,
	AgentHeight = 5,
	AgentCanJump = true
	}

while true do
	NewPath = game:GetService("PathfindingService"):CreatePath(parameters)
	path = NewPath:ComputeAsync(game.Workspace.Start.Position, game.Workspace.Finish.Position, 200)
	points = NewPath:GetWaypoints()
	
	game.Workspace.Points:ClearAllChildren()
	
	for point = 1, #points do
		part = Instance.new("Part")
		part.CanCollide = false
		part.Name = "Point"
		part.Position = points[point].Position
		part.Anchored = true
		part.BrickColor = BrickColor.White()
		part.Size = Vector3.new(1,1,1)
		part.Parent = game.Workspace.Points
	end
	wait(.1)
end

Now click Run



Your screen should look something like this (except the obsticles because you made your own):



Notice the white dots in between the start and the finish - that's each WayPoint! Now try moving the parts around without stopping the program. The path should automaticly adjust!

Doesn't Work? Get the free model I made here to see where you went wrong: https://web.roblox.com/library/4555307812/Path-Finding-Tutorial

Thanks for reading! Hope you enjoyed this tutorial and it helped you alot! Cheers,

LoveTheBears101

Med Written By LoveTheBears101

See LoveTheBears101's profile on Roblox

Discussion