PathfindingService

How to use PathfindingService

by CXylet

Author Avatar

To use "PathfindingService" you will need to put a script inside of the character, otherwise it won't work.

So first of all you need to define a character.

local character = script.Parent

We are accessing a character and now let's make it simple.

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local part = workspace.Part

humanoid:MoveTo(part.Position)

As you can see we defined the humanoid to access something that is called MoveTo,

humanoid:MoveTo() will allow the character to move.

image|200x200

And we defined a part to move the character to.

but the sad thing is if we add a wall and cover the path this will happen.

image|100x100

Unfortunately it will get stuck because it doesn't find the closest path it just goes to the path in sight.

That's where "PathfindingService" come along!

You might be asking, what does "PathfindingService" do?

Well fortunately it's really easy to use but if you are a beginner you might not understand, so I recommend you to be intermediate to read the documentation about "PathfindingService".

Okay the first thing to use "Pathfindingservice" is to define it.

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()

So now we defined "PathfindingService", and we defined a variable that's called "path"

so path = PathfindingService:CreatePath() actually creates a path but it's imaginary so it's stored but it's not quite correct yet.

To make it correct you'll have to compute the path!

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()

path:ComputeAsync(character.HumanoidRootPart.Position, workspace.Part.Position) -- Start Pos, End Pos

So this looks confusing, but don't worry it's quite simple.

First of all we computed the path and we said the start position of our path and the end position.

So you might think that we are done, but we are close.

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()

path:ComputeAsync(character.HumanoidRootPart.Position, workspace.Part.Position) -- Start Pos, End Pos

local waypoints = path:GetWaypoints()

You might get confused but we defined waypoints, so waypoints = path:GetWaypoints()

So path:GetWaypoints() just get's the imaginary parts and it's basically a table with all the imaginary parts.

local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()

path:ComputeAsync(character.HumanoidRootPart.Position, workspace.Part.Position) -- Start Pos, End Pos

local waypoints = path:GetWaypoints()

for i, waypoint in pairs(waypoints) do
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
end

So we made a for loop to loop to all the imaginary parts, so instead of saying waypoints[1] we just loop all of this.

And humanoid:MoveTo(waypointPosition) just moves the character to the waypoints so it follows the right path instead of ignoring everything in sight and that's basically "PathfindingService"

It just makes a path and ignore obstacles.

View in-game to comment, award, and more!