The Beginning.
Hello, I am Lakodex! Today I will be teaching all of you how to script a basic Pathfinding system. Pay attention to the text on your screen, and I recommend that you don’t skip over anything as this is a very detailed lesson. Let’s begin, shall we?
What is Pathfinding (If you already know, you can skip.) Pathfinding is a Service built in to Roblox itself. Pathfinding basically allows you to create virtualized paths for NPC’s to use. NPC’s will no longer run in to walls, but instead walk around them to get to the wanted position.
How does it work?
It works by using software to calculate the distance between the NPC and walls and decide to Jump, or walk around it. Pathfinding is a very useful tool on Roblox to create virtualized characters. Some may make a minimap, NPC’s, etc.
How can I start doing Pathfinding.
It’s very easy! Firstly you need to get the Pathfinding Service, and put it in your script like this. (Put the script inside of the NPC)
Step 1
local PathfindingService = game:GetService("PathfindingService")
Wow! You’ve already did half of the code already! Your already getting good at this! Let’s continue. Now, you need to create parameters or the NPC will walk close, or in to the wall. And then, create 1 point for the NPC to walk to, and create the path.
Step 2
local PathfindingService = game:GetService("PathfindingService")
local NPCChar = script.Parent --Gets the entire character
local NPCHum = NPCChar.Humanoid --Gets the humanoid, the start of Pathfinding.
local End = game.Workspace.End --Create a part if Workspace named "End". This will be the point the NPC will walk to.
local params = {
AgentRadius = 2, --Helps to avoid the character from clipping the walls, but cleanly go around. Raise this value if the character is clipping corners.
AgentHeight = 5, --The height of the character, if this is to big the character may try jumping over large obstacles. Lower this value if the character is not going over a block your wanting it to go over.
AgentCanJump = true --Set if the character is allowed to jump over blocks or not. (Bool Value)
}
local NPCPath = PathfindingService:CreatePath(params)
Good Job! You’re actually almost done with Pathfinding! Now, we need to Compute the Navigation Mesh. And get the waypoints, so the humanoid knows where to go.
Step 3
local PathfindingService = game:GetService("PathfindingService")
local NPCChar = script.Parent --Gets the entire character
local NPCHum = NPCChar.Humanoid --Gets the humanoid, the start of Pathfinding.
local End = game.Workspace.End --Create a part if Workspace named "End". This will be the point the NPC will walk to.
local params = {
AgentRadius = 2, --Helps to avoid the character from clipping the walls, but cleanly go around. Raise this value if the character is clipping corners.
AgentHeight = 5, --The height of the character, if this is to big the character may try jumping over large obstacles. Lower this value if the character is not going over a block your wanting it to go over.
AgentCanJump = true --Set if the character is allowed to jump over blocks or not. (Bool Value)
}
local NPCPath = PathfindingService:CreatePath(params)
NPCPath:ComputeAsync(NPCChar.HumanoidRootPart.Position, End.Position) --Turns our navigation mesh to a table, so we can grab the values.
local waypoints = NPCPath:GetWaypoints() --Getting the data from the table, turning it to Waypoints.
Actually! You’re finished. Now, all you have to do is loop through every Waypoint and you’re done!
Finished Result
local PathfindingService = game:GetService("PathfindingService")
local NPCChar = script.Parent --Gets the entire character
local NPCHum = NPCChar.Humanoid --Gets the humanoid, the start of Pathfinding.
local End = game.Workspace.End --Create a part if Workspace named "End". This will be the point the NPC will walk to.
local params = {
AgentRadius = 2, --Helps to avoid the character from clipping the walls, but cleanly go around. Raise this value if the character is clipping corners.
AgentHeight = 5, --The height of the character, if this is to big the character may try jumping over large obstacles. Lower this value if the character is not going over a block your wanting it to go over.
AgentCanJump = true --Set if the character is allowed to jump over blocks or not. (Bool Value)
}
local NPCPath = PathfindingService:CreatePath(params)
NPCPath:ComputeAsync(NPCChar.HumanoidRootPart.Position, End.Position) --Turns our navigation mesh to a table, so we can grab the values.
local waypoints = NPCPath:GetWaypoints() --Getting the data from the table, turning it to Waypoints.
for _, waypoint in pairs(waypoints) do
NPCHum:MoveTo(waypoint.Position)
NPCHum.MoveToFinished:Wait()
end
Tutorial Published, and created 4/10/2021. Scripting may change.