What is the pathfindng service, what would it be used for and how would I use it?
What is the Pathfinding service?
2 answers
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
