Getting Started
This tutorial shows you how you can set up SimplePath and teaches you a basic implementation of the module.
​

Setting everything up

Get the module from the Roblox website using the link below.
SimplePath - Pathfinding Module - Roblox
For the purposes of this tutorial, the script assumes your module is in game.ServerStorage.
After you insert the module to your place, add a new script to ServerScriptService and paste the following code to start using the module:
1
--Import the module so you can start using it
2
local ServerStorage = game:GetService("ServerStorage")
3
local SimplePath = require(ServerStorage.SimplePath)
Copied!
The next part of the code defines all of the different variables that will be used in the script:
1
--Define npc
2
local Dummy = workspace.Dummy
3
​
4
--Define a part called "Goal"
5
local Goal = workspace.Goal
6
​
7
--Create a new Path using the Dummy
8
local Path = SimplePath.new(Dummy)
Copied!
SimplePath.new() is a constructor that creates a new Path and it should only be created once per agent. You can call Path:Run() on the same Path object multiple times without having to create a new Path every time you need to do pathfinding.
​

Method 1: Using events

The following part of the tutorial shows you how you can make a pathfinding script using only events.
To make the Dummy move towards the goal, you only need one line of code:
1
Path:Run(Goal)
Copied!
Even though this single line of code seems sufficient, there are a few important things to keep in mind. Firstly, if some object comes in-between the path of the Dummy, the Dummy will just stop pathfinding before reaching the goal because Path:Run() is not called a second time to compute the path again. To fix this, you can use the Path.Blocked event and call Path:Run() whenever something blocks the path:
1
--Dummy knows to compute path again if something blocks the path
2
Path.Blocked:Connect(function()
3
Path:Run(Goal)
4
end)
Copied!
The next thing to keep in mind is the position of the goal part. In the case where the goal part is constantly moving, how can you alter the current path of Dummy to make sure that it reaches the exact position of the goal part? You can do this by adding in 2 more events.
The Path.WaypointReached event will compute a new Path everytime the Dummy reaches the next waypoint and accounts for a new position of the goal part if it changed.
1
--If the position of Goal changes at the next waypoint, compute path again
2
Path.WaypointReached:Connect(function()
3
Path:Run(Goal)
4
end)
Copied!
The second event is Path.Error. You can compute a new path every time the target becomes unreachable or the path to the goal is not traversable. For example, if the part is floating in the sky, the Dummy would not be able to reach it and Path.Error fires.
1
--Dummmy knows to compute path again if an error occurs
2
Path.Error:Connect(function(errorType)
3
Path:Run(Goal)
4
end)
Copied!

Final code using the first method:

Your code should look something like this after adding everything in:
1
--Import the module so you can start using it
2
local ServerStorage = game:GetService("ServerStorage")
3
local SimplePath = require(ServerStorage.SimplePath)
4
​
5
--Define npc
6
local Dummy = workspace.Dummy
7
​
8
-- Define a part called "Goal"
9
local Goal = workspace.Goal
10
​
11
--Create a new Path using the Dummy
12
local Path = SimplePath.new(Dummy)
13
​
14
--Helps to visualize the path
15
Path.Visualize = true
16
​
17
--Dummy knows to compute path again if something blocks the path
18
Path.Blocked:Connect(function()
19
Path:Run(Goal)
20
end)
21
​
22
--If the position of Goal changes at the next waypoint, compute path again
23
Path.WaypointReached:Connect(function()
24
Path:Run(Goal)
25
end)
26
​
27
--Dummmy knows to compute path again if an error occurs
28
Path.Error:Connect(function(errorType)
29
Path:Run(Goal)
30
end)
31
​
32
Path:Run(Goal)
33
​
Copied!
Set Path.Visualize to true before the very first Path:Run() call to see the waypoints.
As you can see, the Dummy stops pathfinding after reaching the goal. If you want the Dummy to always follow the goal part even after reaching it, you can simply use the Path.Reached event:
1
--Compute a new path every time the Dummy reaches the goal part
2
Path.Reached:Connect(function()
3
Path:Run(Goal)
4
end)
Copied!
​

Method 2: Using loops

In the following tutorial, you will learn how to use SimplePath using loops instead of events. Using SimplePath in a loop is way simpler than using events. All you need is 3 lines of code:
1
while true do
2
Path:Run(Goal)
3
end
Copied!
Path:Run() does not require a wait because it automatically yields if the maximum time elapsed between consecutive calls are less than Settings.TIME_VARIANCE.

Final code using the second method

If you are using loops, your code should look something like this:
1
--Import the module so you can start using it
2
local ServerStorage = game:GetService("ServerStorage")
3
local SimplePath = require(ServerStorage.SimplePath)
4
​
5
--Define npc
6
local Dummy = workspace.Dummy
7
​
8
-- Define a part called "Goal"
9
local Goal = workspace.Goal
10
​
11
--Create a new Path using the Dummy
12
local Path = SimplePath.new(Dummy)
13
​
14
--Helps to visualize the path
15
Path.Visualize = true
16
​
17
while true do
18
Path:Run(Goal)
19
end
20
​
Copied!
​

Choosing a method

SimplePath gives you the freedom to code in any method you prefer. You are not limited to the two methods mentioned in this tutorial as they are simply meant to be examples. You can even combine both methods and implement them together at once. It all depends on how you decide to structure your code based on the performance, compatibility, etc. and personal preference.
Last modified 1mo ago