Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the Pathfinding service?

Asked by 5 years ago

What is the pathfindng service, what would it be used for and how would I use it?

2 answers

Log in to vote
0
Answered by 5 years ago

Well, the pathfinding service is basically a better way to help NPC's move towards points. Now you might be thinking, that's not hard at all, just use :MoveTo(). But if there is an obstacle on the way, it actually still tries to move in a straight line to the point, meaning the NPC gets stuck. Now the pathfinding service will allow to find the best path between any two points. Using it will create a table/array of points that you can cycle through and move your NPC to each. You could use MoveToFinished:Wait(), or check the distance to make sure your NPC gets to the next point before moving on.

TL:DR: NPC's don't get stuck on things.

To implement a very basic version would be like so:

local PathfindingService = game:GetService("PathfindingService")

local pointA = game.Workspace.PointA
local pointB = game.Workspace.PointB

local path = PathfindingService:FindPathAsync(pointA.Position, pointB.Position)
local points = path:GetWaypoints()

local humanoid = game.referenceToHumanoidHere.humanoid

for _, waypoint in pairs(waypoints) do
    humanoid.MoveTo(waypoint.position)
    humanoid.MoveToFinished:Wait()
end
Ad
Log in to vote
0
Answered by
Audiimo 105
5 years ago
Edited 5 years ago

Pathfinding service is good for NPCs when you want them to walk to a point while detecting if a part is in the way. There is a wiki page about it and it is quite helpful. Here it is: wiki.roblox.com/index.php?title=Pathfinding

Answer this question