How you can use AI Pathfinding

Hello. This is my first post on this forum, I’m not a new user, (didn’t even know this existed) and today I am gonna show you how you can use AI pathfinding to go from point 1 to 2 etc. or chase a player, this requires a significant knowledge of scripting, without further ado, let’s jump right into it.

So first you are gonna need an npc, it doesn’t matter about the animate script but you can use it if you want, for this tutorial, I will be using a noob NPC and also an end point (or if you are not using parts and trying to make an enemy, you probably don’t need that) and I am gonna be making a small obstacle course to demonstrate this. because npcs without pathfinding can’t jump

image

with all the boring stuff out of the way, it’s time to get scripting, insert a script into the NPC character and call it whatever you want, so we are gonna be using pathfindingservice, if you don’t know what it is, it’s like a brain for an npc, as npc’s are not players and actually can’t go behind objects

local pathfindingService = game:GetService("PathfindingService")
local NPC = script.Parent
local humanoid = NPC.Humanoid -- The Humanoid is an essential part as you need the function MoveTo()
local HumanoidRootPart = NPC.HumanoidRootPart
local NPCpath = pathfindingService:CreatePath() -- Creating a path

So we are gonna use the path:ComputeAsync() function which basically yields(stops) the scripts until it is ready to return or start, sorry if you don’t understand, i’m bad at explaining, the two arguments in the function is the HumanoidRootPart Position (or torso Position) and the endpos so it should be

NPCpath:ComputeAsync(HumanoidRootPart.Position,game.Workspace.EndingPart.Position)

now that this is done, you have to get wayPoints for your path, which just makes like the fastest way to the endingPart, that also means it doesn’t get stuck in walls, also the GetWaypoints() function returns a table, not an object, so it will be:

local wayPoints = path:GetWaypoints() -- Make sure it's correctly spelled like I did,

for i,wayPoint in pairs(wayPoints) do
    humanoid:MoveTo(wayPoint.Position) -- That's why I said the MoveTo function is important, also don't forget the .Position after the wayPoint
end

But if we were to run this, the npc is not gonna do anything and go in a wall again (if you have a wall) that’s because it’s necessary to use The MoveToFinished event but instead of connecting it, we wait it
by doing:

local wayPoints = path:GetWaypoints() -- Make sure it's correctly spelled like I did,

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

so now we have finished it but the character will not jump, that is because it is not programmed to jump and just stays there, so to fix it, we have to change the state of the character using the Jump bool in the humanoid so it will be:

local wayPoints = path:GetWaypoints() -- Make sure it's correctly spelled like I did,

for i,wayPoint in pairs(wayPoints) do
   if wayPoint.Action == Enum.PathWaypointAction.Jump then
      humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
   end
   if wayPoint.Action == Enum.PathWaypointAction.Walk then
      humanoid:ChangeState(Enum.HumanoidStateType.Walk)
   end
    humanoid:MoveTo(wayPoint.Position)
    humanoid:MoveToFinished:Wait()
end

and we are done, so now you can try checking him out, let’s test it

https://gyazo.com/629d382665d964073ca10040545c80aa
(sorry I don’t really know how to upload gifs yet)

so now he will go to the part and stop there and that’s the end of the tutorial :smiley:

Happy Scripting!

36 Likes

Thanks for the tutorial! Sorry if I ask, but do you know some tutorials about advanced PathFinding? For example, if we have a dynamic path the NPC behaviour will be very strange.
Thanks in advance.

1 Like

I don’t really know, I just learnt from random tutorials

2 Likes

You should check this out: https://www.youtube.com/channel/UCEaBFdb9xUQjIB05HRG0cTw
He’s mainly focused on creating NPCs

2 Likes

You’ve attempted to call MoveToFinished as a method where you wrote humanoid:MoveToFinished:Wait(). It should be humanoid.MoveToFinished:Wait() instead.

3 Likes