YouTube2Text

Pathfinding (AI, Dynamic Paths, Modifiers) - Roblox Advanced Scripting #35 (2025) — Transcript

by BrawlDev · 6,830 words · 891 segments · language en · Watch on YouTube

Full transcript

  1. 0:00okay braw Dev fans if you want to get my
  2. 0:02autograph then you have to go through
  3. 0:04this obstacle course for today's
  4. 0:05learning objective it should be a pretty
  5. 0:07simple one and if you can make it across
  6. 0:09this obstacle course without touching
  7. 0:11the lava parts then you get my autograph
  8. 0:13and I'm really excited to see what's
  9. 0:15going to happen here if you touch the
  10. 0:16lava parts then you die and I do not
  11. 0:19want to kill my fans over this cuz that
  12. 0:21would be really bad uh but yeah with
  13. 0:23that being said let's go ahead and get
  14. 0:25started in three two one all right oh
  15. 0:31let's go they're making it oh no a few
  16. 0:32of them are dying already okay but it
  17. 0:34looks like some of them are about to
  18. 0:35make it oh no they did not make it
  19. 0:37through no wait they're all dying oh my
  20. 0:41god oh nobody made it across oh this
  21. 0:45this is so
  22. 0:47unfortunate oh what have I done all
  23. 0:50right let's think about this for a
  24. 0:51minute what you just saw was called
  25. 0:54pathf finding which is the process of
  26. 0:57moving a character whether that's an NPC
  27. 1:00or a player towards a path to reach a
  28. 1:03certain destination while also avoiding
  29. 1:06obstacles and that's the basic
  30. 1:08definition of pathf finding and from
  31. 1:10this example that you just saw here all
  32. 1:12of these NPCs had their starting point
  33. 1:15over on that side and their goal was to
  34. 1:18find a path to reach the destination
  35. 1:21which is right over here while also
  36. 1:24avoiding these red parts and this is
  37. 1:26calculated using roblox's pathfinding
  38. 1:29service which is what I'm I'm going to
  39. 1:30be teaching you how to implement inside
  40. 1:31of this episode so get ready for this
  41. 1:33one I think this is going to be a really
  42. 1:34fun episode and with that being said
  43. 1:36let's go ahead and go straight into the
  44. 1:38episode now that we understand it let's
  45. 1:40Implement pathf finding inside Roblox
  46. 1:42studio and there's three items that we
  47. 1:45need for this pathf finding to work the
  48. 1:47first thing is the NPC or the character
  49. 1:50that we want to move the second thing is
  50. 1:52the goal that we're trying to reach and
  51. 1:54the third thing is the obstacle or the
  52. 1:56obstacles that are going to be in the
  53. 1:58way of this character to reach its goal
  54. 2:01so what we're going to do first is
  55. 2:03import a character and we're going to do
  56. 2:05that inside of Avatar and we're going to
  57. 2:07click rig Builder now it doesn't matter
  58. 2:09which Avatar you pick I'm just going to
  59. 2:11click on block Avatar and what I'm going
  60. 2:14to do is rotate this this way and I'm
  61. 2:18just going to quickly add a part by
  62. 2:21going to model and then hitting part I'm
  63. 2:23just going to scale this down I'm going
  64. 2:25to change the color of this to some sort
  65. 2:27of blue I'm going to turn can cide off
  66. 2:31and anchored on and I'm just going to
  67. 2:32move this alongside here and finally we
  68. 2:35want an obstacle so I'm just going to
  69. 2:37make another part right in front of the
  70. 2:40goal and I'm going to Anchor this and
  71. 2:43this is basically our initial setup so
  72. 2:47what we have going on here is an
  73. 2:49obstacle in the way of this character to
  74. 2:51reach its goal which is over here with
  75. 2:53this button and if we used normal
  76. 2:56methods of moving the character like
  77. 2:58let's say the humanoid move to then it's
  78. 3:00just going to run into the wall like
  79. 3:03this without ever having to reach the
  80. 3:05goal because this obstacle is in the way
  81. 3:07so this is where pathfinding service
  82. 3:09comes in where we want pathf finding
  83. 3:11service to calculate a path that is
  84. 3:14going to make it from where it is to its
  85. 3:17ending point now how is this path
  86. 3:21calculated well it's calculated by
  87. 3:23finding the shortest path possible which
  88. 3:25is the default way of pathfinding
  89. 3:27service to find the best path for this
  90. 3:29character
  91. 3:30and the way that I can show this to you
  92. 3:32is through scripting which is what we're
  93. 3:34going to do right now so what we're
  94. 3:35going to do is go to rig we're going to
  95. 3:37hit the plus sign and we're going to
  96. 3:39insert a script so the first thing we're
  97. 3:41going to do is locate pathf finding
  98. 3:43service by saying local path finding
  99. 3:46service equals game colon get service
  100. 3:49pathfinding service and then we're going
  101. 3:50to make reference to our NPC and also
  102. 3:53our goal part so we're going to say
  103. 3:56local NPC equals script. parent and then
  104. 3:58we're going to say local humanoid equals
  105. 4:01NPC colon wait for Child open to close
  106. 4:05parentheses humanoid and then finally
  107. 4:07our goal part which we're actually going
  108. 4:09to rename our goal part here so I'm just
  109. 4:11going to select this rename this to goal
  110. 4:14part and then we're going to go back to
  111. 4:16our script and say local goart equals
  112. 4:19game. workpace colon wait for child
  113. 4:23goart just like this so the next thing
  114. 4:25we need to do is create our path by
  115. 4:27saying local path equals
  116. 4:30path finding service colon create path
  117. 4:35open and close parentheses just like
  118. 4:36this now this takes in one parameter
  119. 4:38which is a table full of information
  120. 4:41that we can use to change our table to
  121. 4:43our liking but just for Simplicity sake
  122. 4:46we're just going to leave this blank for
  123. 4:47now so this creates a path for us and a
  124. 4:51way that I can help you visualize this
  125. 4:53is if I go into the game I'm going to
  126. 4:55show you this model that has these
  127. 4:57Waypoint parts now don't worry about
  128. 4:59adding this inside of your game this is
  129. 5:01just to help us visualize it so when we
  130. 5:03create a path it's going to create what
  131. 5:05are called way points so each of these
  132. 5:07balls here represent way points and
  133. 5:09we're going to get into this a little
  134. 5:11bit later but this is basically our path
  135. 5:13right now if you want to visualize it
  136. 5:14like that so what we need to do with
  137. 5:17this path is we need to now calculate
  138. 5:20the path that goes from our starting
  139. 5:23position which is our character to our
  140. 5:25ending position which is over here and
  141. 5:28so the way we're going to do this is
  142. 5:29we're going to go to our script we're
  143. 5:31going to drop two lines and we're going
  144. 5:32to say path colon compute async open and
  145. 5:37close parentheses and this takes in two
  146. 5:39parameters the first one is the starting
  147. 5:41position and the second one is the
  148. 5:43ending position so the starting position
  149. 5:45is going to be our character which we
  150. 5:48can say is going to be our humanoid root
  151. 5:50Parts position because that's where our
  152. 5:51character is so what we're going to do
  153. 5:53is say NPC do primary part. position
  154. 5:57which is the human node root part and
  155. 5:59then our ending position is going to be
  156. 6:02our goal part over here so we're going
  157. 6:04to say goal part. position just like
  158. 6:06this so once we tell Roblox to compute a
  159. 6:10path it's going to take this path that
  160. 6:12we created and it's going to try and
  161. 6:14find the best path for us so if I were
  162. 6:16to give you a visualizer this is some
  163. 6:20this is like a path that Roblox might
  164. 6:22create for us so it takes our original
  165. 6:24path and it's going to scatter it along
  166. 6:26this obstacle to try and find the most
  167. 6:28optimal path now again this is just to
  168. 6:30help us visualize it you don't have to
  169. 6:31add this inside of your game and we did
  170. 6:33not add this uh through our script or
  171. 6:35anything like that so it's going to look
  172. 6:37something like that once we use compute
  173. 6:39async now here's the thing roblox's
  174. 6:42pathfinding service is known to not find
  175. 6:46paths when it seems pretty obvious uh
  176. 6:49it's definitely not the best framework
  177. 6:51when it comes to creating path finding
  178. 6:53modules or Frameworks and things like
  179. 6:55that so at times it can fail a lot which
  180. 6:58is why we need to wrap this compute
  181. 7:00async inside of a PE call so what we're
  182. 7:03going to do here is say local success
  183. 7:06comma error message equals P call open
  184. 7:11close parenthesis function open close
  185. 7:13parentheses enter and we're going to
  186. 7:15throw this inside of here now if you're
  187. 7:18iffy on PE calls I encourage you to
  188. 7:19watch my data stores video because I go
  189. 7:21into more detail about that but this PE
  190. 7:23call is pretty important when we're
  191. 7:25using compute async okay so now what
  192. 7:27we're going to do is check if this was
  193. 7:29successful so we're going to say if
  194. 7:31success then we can use this path to our
  195. 7:33advantage but if this was not successful
  196. 7:36then what we're going to do is write a
  197. 7:38warning message to the console um with
  198. 7:41our error message that we got from this
  199. 7:44PE call up here okay so now that we have
  200. 7:47this what we want to do is we want to
  201. 7:49take each of the way points which is
  202. 7:51what I mentioned earlier uh these balls
  203. 7:54being which I mentioned earlier these
  204. 7:56points are all called way points so we
  205. 7:59can do this inside of a for Loop if we
  206. 8:01want we're just going to say
  207. 8:034core comma um Waypoint in pairs open
  208. 8:08close parentheses and we're going to say
  209. 8:10path colon get way points just like this
  210. 8:14and we're going to say do and what we
  211. 8:16can do here to help us visualize our
  212. 8:19path is create a part that represents
  213. 8:21each Waypoint so what we're going to do
  214. 8:23is say local part equals instance. new
  215. 8:27uh we can say part and we can put this
  216. 8:30Parts parent so part. parent equals
  217. 8:34game. workpace now we're going to make
  218. 8:36this look like a visual Waypoint just
  219. 8:38like I showed you earlier and the way
  220. 8:40we're going to do that is by changing a
  221. 8:41few properties so we're going to say
  222. 8:43part. material equals neon we're going
  223. 8:47to say part. anchored equals true and
  224. 8:51then we're going to say part. Can
  225. 8:53Collide equals false and then we're
  226. 8:55going to change the shape of it too so
  227. 8:58part. shape equals fall just like this
  228. 9:02and finally we need to change the
  229. 9:04position of this part to reflect the
  230. 9:06position of each Waypoint so what we're
  231. 9:08going to do is say part. position equals
  232. 9:12Waypoint
  233. 9:13doos and this should pretty much give us
  234. 9:17a visual representation of the path that
  235. 9:20has been created for us so if we go to
  236. 9:21the script and if we hit test and hit
  237. 9:24run then what we should see is that this
  238. 9:27is something our script did this script
  239. 9:30first created a path then it computed
  240. 9:32the path from its starting position and
  241. 9:34then it tried to find the best path
  242. 9:36towards its ending position so we've
  243. 9:38been able to successfully find a path
  244. 9:40that works for this NPC and I hope that
  245. 9:42so far that this has been making sense
  246. 9:44to you so that we can now move on to
  247. 9:45more complicated things so we've now
  248. 9:47been able to calculate our path but
  249. 9:49let's make it so that the NPC actually
  250. 9:52moves to each way point to reach the end
  251. 9:54goal and the way we're going to do this
  252. 9:56is we're going to drop two lines and
  253. 9:58what we're going to do is say
  254. 9:59humanoid colon move to open and close
  255. 10:03parenthesis and what we're going to do
  256. 10:04is pass in the Waypoint do position just
  257. 10:07like this and make sure this is the
  258. 10:09humanoid move to method and not the
  259. 10:11character move to method and once the
  260. 10:13humanoid is moving we want to have an
  261. 10:15event so that when the humanoid moves to
  262. 10:18this Waypoint position then it's going
  263. 10:20to go to the next Waypoint it's going to
  264. 10:22create the Waypoint and then the
  265. 10:23humanoid is going to move to that one
  266. 10:25and it's going to keep on doing that
  267. 10:26until all the way points have been
  268. 10:28traversed
  269. 10:29so we can do this by saying humanoid
  270. 10:32Dove to finished colon weight open and
  271. 10:37close penes just like this so it's going
  272. 10:38to move to the Waypoint position and
  273. 10:40then it's going to wait until it's
  274. 10:41finished and then it's going to go to
  275. 10:42the next one just like this so if we go
  276. 10:44into the game and hit run then what we
  277. 10:46should see is that for every single
  278. 10:48Waypoint it looks like it pretty much is
  279. 10:51working the rig moves from one way point
  280. 10:53to the next and then it's going to move
  281. 10:55on to the next one and then the next one
  282. 10:56and it's going to keep on going until it
  283. 10:58reaches the end and we basically have a
  284. 11:00moving NPC script now let's say we have
  285. 11:02a situation where we want the NPC to
  286. 11:04jump over obstacles but fails to do so
  287. 11:07because we haven't scripted it let me
  288. 11:09show you what I mean so I'm just going
  289. 11:10to take this wall and I'm going to move
  290. 11:11it over here I'm going to scale it just
  291. 11:15like this and I'm going to duplicate
  292. 11:18this again scale it like so move it up
  293. 11:22here and I'm just going to create two
  294. 11:24hurdles just like this and the intention
  295. 11:27behind this is we want the NPC to start
  296. 11:31here and we want the NPC to move to this
  297. 11:34goal on the other side here and it needs
  298. 11:36to do that by being able to jump over
  299. 11:38these hurdles right over here if it
  300. 11:40can't jump over these hurdles then it's
  301. 11:42going to get stuck so let me show you
  302. 11:44what happens if we run the script as
  303. 11:49is okay that was a that was a bad
  304. 11:52demonstration let me increase the height
  305. 11:55of these parts now it should be
  306. 11:58difficult for this NPC to get across
  307. 12:00there we go now this NPC is stuck it is
  308. 12:02trying to get to the end goal but it
  309. 12:05fails to do so because it can't jump so
  310. 12:06we need to add this ourselves inside of
  311. 12:08our script so I'm going to hit stop I'm
  312. 12:10going to go to our script and there's
  313. 12:11two things that we need to do um the
  314. 12:14first thing is we need to specify in our
  315. 12:16path that the character is able to jump
  316. 12:19when it's following along the path and
  317. 12:21if you remember we do that inside of
  318. 12:23here with our agent parameters table so
  319. 12:26what I'm going to do is put in an open
  320. 12:29and close curly braces and I'm going to
  321. 12:31hit enter just like this so these
  322. 12:32parameters are going to specify some
  323. 12:34things that the NPC is allowed to do
  324. 12:37such as being able to jump and the way
  325. 12:39we're going to do that is by saying
  326. 12:41agent can jump equals true just like
  327. 12:46this I'm going to add a commum at the
  328. 12:47end now by default agent can jump is
  329. 12:50already set to true so we don't really
  330. 12:51have to do this but just to introduce to
  331. 12:54you this configuration table we're going
  332. 12:56to add this inside of here okay so once
  333. 13:00the humanoid is attempting to move to
  334. 13:02this Waypoint position what we want to
  335. 13:04do is is let the humanoid jump if we
  336. 13:07need to make the humanoid jump and the
  337. 13:10way we do this is through a property
  338. 13:12inside of the Waypoint called action so
  339. 13:16this is our Waypoint action that's going
  340. 13:17to tell us if the player if the
  341. 13:20character needs to jump and this is what
  342. 13:22it's going to look like so right after
  343. 13:23the move to Method we're going to say if
  344. 13:27Waypoint do action equals enum do
  345. 13:32pathway Point
  346. 13:35action. jump then we're going to make
  347. 13:39the humanoid jump so we're going to say
  348. 13:41humanoid do jump equals true just like
  349. 13:46that so now if we go into the game and
  350. 13:49hit run then what we should see is that
  351. 13:51for a specific Waypoint that requires
  352. 13:54the NPC to jump the NPC actually does
  353. 13:57jump so for this Waypoint right here the
  354. 14:01NPC needed to jump for this Waypoint
  355. 14:03right here the NPC needed to jump in
  356. 14:05order to reach across to the end so
  357. 14:07that's how we make the humanoid jump now
  358. 14:09let's say we have another situation
  359. 14:11where we need the player to climb an
  360. 14:14object to be able to reach its goal now
  361. 14:16what I'm going to do is I'm just going
  362. 14:17to move this um this now what I'm going
  363. 14:19to do is I'm just going to move this
  364. 14:21goal part here and I'm going to move
  365. 14:24this part over here and I'm going to
  366. 14:26scale it up just like this and what also
  367. 14:29going to do is insert a trust part which
  368. 14:32is basically a ladder so I'm just going
  369. 14:34to scale this up just like this and have
  370. 14:36the goal part up here so is this NPC
  371. 14:40going to be able to climb up this part
  372. 14:41and reach its goal well it looks like
  373. 14:44it's not going to be able to do that it
  374. 14:46has unsuccessfully been able to find a
  375. 14:48path so what we need to do is specify in
  376. 14:51our parameters up here by saying agent
  377. 14:54can climb equals true and that that is
  378. 14:59the only thing we need to do to
  379. 15:00determine if the if the NPC is allowed
  380. 15:03to climb parts or not so if we now hit
  381. 15:05run then the NPC should be able to climb
  382. 15:09this truss and reach the end goal just
  383. 15:11like this so these are a few properties
  384. 15:14we can change with the agent parameters
  385. 15:16table here to modify our path to make it
  386. 15:18fit our needs now uh in the Roblox
  387. 15:21documentation there are more properties
  388. 15:22that you can look at which I'll leave a
  389. 15:23link to in the description if you want
  390. 15:25to experiment with that but those are
  391. 15:27some things that we can add to our
  392. 15:29pathfinding script to make this more
  393. 15:31robust okay so before we move okay so
  394. 15:34before we move on there's one thing I
  395. 15:35forgot to do with our PE call check so
  396. 15:38with this line right here line 16 if
  397. 15:41success then it's not going to be enough
  398. 15:44to tell us if the PE call was successful
  399. 15:46because we also need to check if the
  400. 15:48path that was created was also
  401. 15:50successful as well so we need to add
  402. 15:52this to our if statement by saying if
  403. 15:55success and path. status
  404. 15:59equals en. paath status. suuccess so it
  405. 16:04has to require both of these criterias
  406. 16:07in order to uh make it a successful path
  407. 16:10but if not then we're going to have a
  408. 16:12warning message in which we can pretty
  409. 16:14much specify here by saying the path has
  410. 16:20unsuccessfully been created and we can
  411. 16:23just throw in the error message right
  412. 16:24after that so if I were to test this out
  413. 16:27let's comment out the agent K CL climb
  414. 16:29parameter and we're going to hit run
  415. 16:30then what it should display in the
  416. 16:32output is the path has unsuccessfully
  417. 16:35been created and our error message
  418. 16:36should be displayed to the right of that
  419. 16:38so that was a little um so that's a
  420. 16:40little thing I wanted to add there
  421. 16:41before we move on to the next part so
  422. 16:43right now our pathfinding script
  423. 16:45calculates the shortest path from a
  424. 16:47start position to an end position and
  425. 16:50that is all what the script is doing but
  426. 16:52what if we were given a situation where
  427. 16:54instead of finding the shortest path we
  428. 16:57wanted so that this path finding script
  429. 16:59will make this NPC find the smartest
  430. 17:03path or a path that's more reasonable
  431. 17:05than whatever the shortest path is going
  432. 17:07to be and I'm going to be showing you
  433. 17:08how to do this by adding what are called
  434. 17:11pathf finding modifiers and I'm going to
  435. 17:13be showing it through these examples
  436. 17:16that I have over here starting with this
  437. 17:18first one over here so what I'm going to
  438. 17:20do is take this rig I'm going to move it
  439. 17:22over here and I'm going to take this
  440. 17:23goal part and move it somewhere along
  441. 17:25here at the end okay so we're presented
  442. 17:28a situation here where there are two
  443. 17:31paths that this NPC can take it can
  444. 17:33either take the wood path or it can
  445. 17:35either take the Neon Path and I want you
  446. 17:38to think of this Neon Path as a lava
  447. 17:41part obviously if this rig were to try
  448. 17:44and go to this lava part then it's going
  449. 17:47to die and we don't want that to happen
  450. 17:49we want this NPC to go towards the wood
  451. 17:51path so that it can reach its end goal
  452. 17:53and we can do this by setting what's
  453. 17:56called material costs and the the basic
  454. 17:59idea behind this is that we want to make
  455. 18:01it so that this wood path is more
  456. 18:04traversible than this Neon Path by
  457. 18:07setting a cost of how easy this material
  458. 18:11is to walk on versus a different
  459. 18:13material and this can be done through
  460. 18:15our script so if we go to our script we
  461. 18:17are going to go inside of our parameters
  462. 18:19table and we're going to add another
  463. 18:21parameter called costs and we're going
  464. 18:23to set this equal to a table so we're
  465. 18:26going to say open and close curly
  466. 18:27brackets and we're going to hit enter
  467. 18:28and I'm also going to add a comma here
  468. 18:30so the idea behind this costs table is
  469. 18:33that our keys are going to be our
  470. 18:35materials and then our values are going
  471. 18:38to be the cost of that specific material
  472. 18:41so our two materials that we have here
  473. 18:43are wood and neon so what I'm going to
  474. 18:46do here is say wood equals and then we
  475. 18:49can set this to any number we want so by
  476. 18:52default the number is one so if we set
  477. 18:54this to one then this is going to have a
  478. 18:57high priority or this is going to be a
  479. 19:00material that's going to be easy to walk
  480. 19:02on um so what we're going to do is also
  481. 19:04add another material here called neon
  482. 19:07and we're going to set this to a number
  483. 19:09that's higher than one so something
  484. 19:11bigger like 100 so if we do this then
  485. 19:14essentially what this is saying is that
  486. 19:15this wood path is going to be easier to
  487. 19:18travel across than this Neon Path that's
  488. 19:21over here and this is for any part that
  489. 19:23has the wood material or the neon
  490. 19:25material so now if we go into test and
  491. 19:28hit r run we should see that this NPC is
  492. 19:31going to Traverse along this wood path
  493. 19:34over here to reach its end goal and we
  494. 19:36have successfully avoided the Neon Path
  495. 19:39and so that's how we set material costs
  496. 19:41to make smarter decisions uh for our
  497. 19:44pathfinding script okay so you might
  498. 19:46have noticed something if I hit run uh
  499. 19:48so that we can watch this again you can
  500. 19:50see that this NPC as it was walking
  501. 19:53along this path it sort of touched this
  502. 19:55neon part uh even though we don't want
  503. 19:57it to touch the neon part at all the
  504. 19:59reason that it's doing this is because
  505. 20:01our minimum horizontal space for
  506. 20:04calculating the shortest path possible
  507. 20:06is a really small number by default
  508. 20:09which is why the path was really close
  509. 20:13to this neon part and so what we need to
  510. 20:15do is increase that distance
  511. 20:19horizontally and we can do that by
  512. 20:20adding a parameter inside of our
  513. 20:22parameter table so what I'm going to do
  514. 20:23is drop a line and I'm going to say
  515. 20:26agent radius equals and as I mentioned
  516. 20:30before the default is two so we can make
  517. 20:32the size of this bigger by let's say
  518. 20:34four and this will give us a different
  519. 20:36result so if we go into the game and hit
  520. 20:38run then what we should see is that
  521. 20:40there's now more horizontal space for
  522. 20:43this NPC to try and calculate the
  523. 20:45shortest path while also maintaining
  524. 20:47some space and so that's how we were
  525. 20:49able to change that here and that's how
  526. 20:50we update this path to make it better
  527. 20:53but I want you to be careful because if
  528. 20:54you make it too big like let's say 20
  529. 20:56then it's going to have two too much
  530. 20:59space to be able to find the path that
  531. 21:01it needs with how big this radius is so
  532. 21:03I want you to be careful with doing
  533. 21:05these configurations and you can also
  534. 21:07add what's called Agent height which is
  535. 21:10the exact same thing as agent radius
  536. 21:12except it determines vertical space and
  537. 21:14not horizontal space so I want you to
  538. 21:16consider these two parameters as you add
  539. 21:18them inside of your um pathfinding
  540. 21:20script and so I hope you found this part
  541. 21:22to be helpful so that was the first type
  542. 21:24of pathfinding modification it was
  543. 21:26through changing material costs but now
  544. 21:29the next thing I want to show you are
  545. 21:31unwalkable regions or regions that
  546. 21:34shouldn't be taken into consideration
  547. 21:37when we're calculating a path to reach
  548. 21:39the end goal so what I'm going to do is
  549. 21:41take this NPC move it over here move the
  550. 21:43goal part over here and let's imagine
  551. 21:46that this is like a danger zone and we
  552. 21:47don't want the NPC to go through this
  553. 21:50danger zone but instead we want it to
  554. 21:52walk around it um the way we can do that
  555. 21:55is we can add what's called a path
  556. 21:58finding modifier object inside of this
  557. 22:00part over here so what I'm going to do
  558. 22:02is hit the plus sign inside of this part
  559. 22:04and I'm going to insert a path finding
  560. 22:07modifier just like this and we can do
  561. 22:10the same thing for what we did here with
  562. 22:12setting material costs but instead set a
  563. 22:15material cost for this danger zone
  564. 22:17itself and the way we do that is by
  565. 22:19clicking on this pathfinding modifier
  566. 22:21and there's this label property so we
  567. 22:23can call this whatever we want we can
  568. 22:25just say danger zone just like this and
  569. 22:30what we're going to do is go to our
  570. 22:31script and for our costs table we're
  571. 22:33just going to drop another line and
  572. 22:35we're going to say danger zone which is
  573. 22:37the name of our label and we're going to
  574. 22:39set this equal to a very big number like
  575. 22:41let's say 1,000 in this case so anything
  576. 22:45that has the label danger zone it's
  577. 22:47going to look at this cost table and
  578. 22:50it's going to see that we don't want to
  579. 22:52Traverse through this because it's a
  580. 22:53really large number and the reason that
  581. 22:55this is useful to us is because any
  582. 22:57object that that's now contained within
  583. 23:00this danger zone is now considered
  584. 23:02non-traversable which is why having this
  585. 23:05pathfinding modifier object inside of
  586. 23:07here is pretty useful so if we were to
  587. 23:10test the game now so if I were to hit
  588. 23:12run then what we should see is that it's
  589. 23:14now going to avoid this danger zone and
  590. 23:17try to calculate the best path um from
  591. 23:20its starting position to its ending
  592. 23:21position and so that's how we add danger
  593. 23:23zones or zones that are not possible to
  594. 23:26walk through when we're modifying our
  595. 23:28pathfinding script okay so I want to
  596. 23:29show you this last modifier example in
  597. 23:32which we have a situation where we have
  598. 23:34two doors this is door one and this is
  599. 23:36door two so what I'm going to do is move
  600. 23:38this goal part inside of here and I'm
  601. 23:41going to move the NPC inside of here so
  602. 23:44with these two doors we want to add
  603. 23:46pathf finding modifiers to each of these
  604. 23:49doors and set different costs for them
  605. 23:51so that we can determine which door this
  606. 23:53NPC should take and so here's what we're
  607. 23:55going to do I'm just going to select
  608. 23:57this door right here and I'm going to to
  609. 23:58insert a pathfinding modifier and I'm
  610. 24:01going to do the same thing with door 2
  611. 24:03as well so with each of these labels
  612. 24:05we're going to give them unique labels
  613. 24:07so for door one I'm just going to say
  614. 24:09door one and then for door two I'm just
  615. 24:11going to say door two just like this and
  616. 24:13now if we go inside of our script we can
  617. 24:15add this to our costs material table so
  618. 24:18we can say door 1 equals let's say 10
  619. 24:22and then door 2 is equal to 10 as well
  620. 24:25so these are our costs for these two
  621. 24:28doors but if we want it so that the NPC
  622. 24:31wants to go through a specific door
  623. 24:33rather than simply picking the shortest
  624. 24:34door then we will specify door two to be
  625. 24:39the door that it wants to go to so we
  626. 24:40want to set that to a lower material
  627. 24:43cost so if we go back to our script
  628. 24:45we're going to say door 2 is equal to
  629. 24:47let's say five so this one has a smaller
  630. 24:50material cost than door one so the NPC
  631. 24:54will not pick the shortest path which is
  632. 24:57going to be door one but instead it's
  633. 24:59going to go inside of door 2 because it
  634. 25:01has an easier traversal cost so if we
  635. 25:04hit run then what it's going to do is
  636. 25:06it's going to avoid door one and it's
  637. 25:08going to go into door two and it's going
  638. 25:10to reach its goal so that's another
  639. 25:11example of using pathfinding modifiers
  640. 25:13and I hope that this has given you
  641. 25:15insight to try and use these to make
  642. 25:17smarter pathfinding decisions inside of
  643. 25:20your Roblox game okay so I utilized
  644. 25:22pathfinding service to create this NPC
  645. 25:25following player script as you can see
  646. 25:28right here here and essentially what
  647. 25:29it's doing here is with this NPC it's
  648. 25:32using pathf finding service to calculate
  649. 25:34the shortest path to any player that's
  650. 25:37within the range of this NPC so if I
  651. 25:41were to walk away far enough from the
  652. 25:44NPC then it's no longer able to detect
  653. 25:46me and it's not going to chase anyone
  654. 25:48but if I go back into the NPC's range
  655. 25:51which is about 50 studs then it's going
  656. 25:53to try and follow me until it reaches uh
  657. 25:56to where I am another interesting thing
  658. 25:58about about the script is this can work
  659. 26:00with any player that's within its range
  660. 26:02because it's going to detect the closest
  661. 26:04player and I'm going to show you right
  662. 26:06here that if I walk away from here then
  663. 26:08it's going to chase player two instead
  664. 26:10of me because now player two is closer
  665. 26:13to the NPC so if I were to show you with
  666. 26:15player two if I walk away from the NPC
  667. 26:18then it's now going to follow player one
  668. 26:20and if both of us are far enough away
  669. 26:22from the NPC then it's just not going to
  670. 26:24chase anyone and it's just going to
  671. 26:25stand there so this is a really cool
  672. 26:27implementation with pathf finding
  673. 26:28service and I want to show you the
  674. 26:30script that made this possible okay so
  675. 26:33let's break down our NPC following
  676. 26:35player script now my goal here for this
  677. 26:37chapter is to help you understand how we
  678. 26:40use pathfinding service to create uh to
  679. 26:42create systems like this so that you can
  680. 26:44hopefully be inspired to use pathfinding
  681. 26:46service inside of your own scripts to
  682. 26:48create really cool stuff like this so
  683. 26:50what we're going to do is open up the
  684. 26:53rig and there should be one script
  685. 26:55called follow player so if we open this
  686. 26:57up then this should pretty much be the
  687. 26:59entire script for our NPC following
  688. 27:02player script so let's start at the top
  689. 27:04here there is a variable here called
  690. 27:07range which is a constant variable and
  691. 27:09I'm going to explain what this is in
  692. 27:10just a little bit but we basically have
  693. 27:13a function here called create player
  694. 27:14path that takes in a Target parameter
  695. 27:17Target referring to the closest player
  696. 27:20that this NPC has detected so we want to
  697. 27:22get this player as a Target so what this
  698. 27:25does is it computes a path uh from the
  699. 27:28NPC to the Target and if it wasn't
  700. 27:31successful then it's going to return nil
  701. 27:33otherwise it's going to return the
  702. 27:34player path that has computed I think
  703. 27:36this function is pretty self-explanatory
  704. 27:38so now if we move on to the next
  705. 27:39function this one is going to be a
  706. 27:41little tricky so I want to take some
  707. 27:43time to explain this so we have a table
  708. 27:46here called magnitudes now I haven't
  709. 27:48talked about magnitudes on this channel
  710. 27:51really so I think this deserves a small
  711. 27:54introduction okay so let's answer the
  712. 27:56question of what is a magnet magude so
  713. 27:59in the context of pathfinding service we
  714. 28:01want to use magnitude as a way to
  715. 28:04determine what the distance between the
  716. 28:07NPC and the player is so if we were to
  717. 28:11try and figure out the distance between
  718. 28:13the player and the NPC then all we need
  719. 28:16to do is calculate the magnitude also
  720. 28:18known as the distance from the player to
  721. 28:22the NPC and this will give us a result
  722. 28:25in studs so right now uh with the this
  723. 28:28player uh if we were to calculate the
  724. 28:30distance it's going to be maybe about
  725. 28:32like 75 studs let's just say for this
  726. 28:34example but for this range right here
  727. 28:37that this NPC is a part of this is let's
  728. 28:40say a range of 50 so right now this NPC
  729. 28:43cannot find my player because I am
  730. 28:46currently outside of this NPC's range
  731. 28:48but if I were to let's say get closer so
  732. 28:51I would be 25 studs in magnitude between
  733. 28:54uh our distances then I would be within
  734. 28:57the range of the NPC to come and detect
  735. 28:59me so that's something that's really
  736. 29:00interesting about this and that's what
  737. 29:02we use magnitudes for it's to calculate
  738. 29:04the distance from the NPC to the player
  739. 29:06but this can be used outside of
  740. 29:08pathfinding as well but this is just a
  741. 29:10really good way of using magnitudes so
  742. 29:12if you go back to our script this is
  743. 29:14essentially a table of all the players
  744. 29:18with their distances from this NPC right
  745. 29:21here and so the idea here is we want to
  746. 29:24find the player that has the smallest
  747. 29:27distance so so if I were to trace
  748. 29:28through this code with you we're going
  749. 29:30to take every player and we're going to
  750. 29:32calculate the distance from that player
  751. 29:34to the NPC and if the distance of that
  752. 29:37specific player is within range with
  753. 29:40this variable up here which is 50 studs
  754. 29:42then we're going to add to our
  755. 29:44magnitudes table using this value right
  756. 29:47here which is another table so the first
  757. 29:48entry is the distance and the second
  758. 29:50entry is the player so if I were to help
  759. 29:52you visualize it like this this table of
  760. 29:55magnitudes is basically going to be a
  761. 29:57table with a distance as the first value
  762. 30:00and the player object as the second
  763. 30:02value so if I were to add another table
  764. 30:04here this would be let's say brev this
  765. 30:06is the magnitudes table it's full of
  766. 30:09tables where the first entry is the
  767. 30:11distance and the second entry is the
  768. 30:13player now the next thing we do is use
  769. 30:16table. sort which basically takes all
  770. 30:18the entries of magnitudes and it's going
  771. 30:20to compare the distance which is what
  772. 30:23this hard bracket one signifies is that
  773. 30:25if we have one value in the table and
  774. 30:26another value in the table we want to
  775. 30:28compare its distances and we want to put
  776. 30:30the entries with the smallest distances
  777. 30:32first and put the ones with the larger
  778. 30:35distances later inside of the list and
  779. 30:37so that's what this does and we also
  780. 30:38want to check if the magnitudes table is
  781. 30:42at least one because if it was Zero then
  782. 30:44there would be no target for this NPC to
  783. 30:46try and uh follow so that's what this is
  784. 30:49for and the target is set to magnitudes
  785. 30:52which gets the first entry because we
  786. 30:54now have the smallest entry first and we
  787. 30:57want want to get the player which is
  788. 30:59what this hard bracket 2 signifies
  789. 31:01because player is right over here so
  790. 31:04that's how we get the Target and once we
  791. 31:06do that then we're going to return that
  792. 31:07Target so that's what this function does
  793. 31:10and if it didn't make sense to you then
  794. 31:11I encourage you to rewatch this part but
  795. 31:13uh moving forward we have this while
  796. 31:15true do Loop that first gets the closest
  797. 31:18player with the function that I just
  798. 31:19explained right here and if it finds the
  799. 31:21target then it's going to wait until the
  800. 31:23target uh has loaded into the game with
  801. 31:25their character and then it's going to
  802. 31:27create a player path with that Target
  803. 31:30and it's going to create the way points
  804. 31:31then it's going to create a visualizer
  805. 31:33and then it's going to make the NPC move
  806. 31:35to each Waypoint and if it hasn't
  807. 31:36detected a Target initially then this
  808. 31:39NPC will not Chase anyone because there
  809. 31:42is no target within its vicinity so it's
  810. 31:44just going to stay in place and that's
  811. 31:46basically what this does and this while
  812. 31:48true do statement is going to fire every
  813. 31:51single heartbeat using run service and
  814. 31:53that is basically how we create our NPC
  815. 31:56following player script it's definitely
  816. 31:58isn't a perfect script I would actually
  817. 32:00consider this to be a more simple
  818. 32:02implementation because there are some
  819. 32:04things that I left out in the script
  820. 32:06like adding a feature where it can
  821. 32:08detect blocked paths so that it can
  822. 32:11reroute itself to look for my character
  823. 32:14uh and also other things like being able
  824. 32:15to jump on obstacles without having like
  825. 32:17a weird system where it would just I
  826. 32:19don't know run into the wall just like
  827. 32:21you're seeing right here so that it
  828. 32:22would actually be able to jump when it
  829. 32:24needs to but sometimes it wouldn't do
  830. 32:26that and it would just lead to some
  831. 32:28other problems so you would have to
  832. 32:31figure out how to create a more advanced
  833. 32:33system by adding onto the script but
  834. 32:35what I've shown you here is a more
  835. 32:37simple implementation and I hope that
  836. 32:39this has given you inspiration to expand
  837. 32:41your script to add more to it and so
  838. 32:45that is basically what I wanted to show
  839. 32:46you inside of this episode and one thing
  840. 32:48that I want to answer is whether
  841. 32:50roblox's pathfinding service actually is
  842. 32:52good and my answer to that would be is
  843. 32:56it's definitely not like an amazing
  844. 32:57system system but it gets the job done
  845. 32:59when you need it to if you were to try
  846. 33:01and make this NPC go through like a a
  847. 33:04difficult Obby then it's not going to
  848. 33:06compute a path for you because you kind
  849. 33:07of have to treat it like it's fragile it
  850. 33:09has a lot of limitations when it comes
  851. 33:11to being able to calculate pads that
  852. 33:13should be obvious but aren't and so you
  853. 33:17have to take it with a grain of salt
  854. 33:18that roblox's path finding service alone
  855. 33:21probably isn't going to reach your goals
  856. 33:23that you wanted to achieve like the the
  857. 33:26top Roblox developers would be using
  858. 33:27their own pathfinding service for this
  859. 33:29kind of thing and so if you want to dive
  860. 33:32deeper into pathfinding then I encourage
  861. 33:34you to do so because you might because
  862. 33:35you might make something really cool out
  863. 33:37of this oh anyways I'm done yapping
  864. 33:39about pathfinding but it's definitely a
  865. 33:40really interesting concept to dive
  866. 33:42deeper into if this is something you're
  867. 33:44interested in so if you enjoyed this
  868. 33:45video then I encourage you to leave a
  869. 33:47like and also subscribe if you haven't
  870. 33:49already and also leave a comment down
  871. 33:51below uh if there's something that
  872. 33:53you've been able to implement with
  873. 33:54pathfinding service that you found to be
  874. 33:57pretty useful and that you're proud of
  875. 33:58creating uh I would definitely like to
  876. 34:00hear your thoughts about pathfinding and
  877. 34:02I would also like to give a shout out to
  878. 34:03my patreon members for their continued
  879. 34:05support and I definitely appreciate
  880. 34:07their support because it definitely
  881. 34:08motivates me to continue working on
  882. 34:09these videos for you guys um so if you
  883. 34:12want to become a patreon member the link
  884. 34:13will be in the description you'll get
  885. 34:15access to my scripts and also Early
  886. 34:17Access to these videos that come out and
  887. 34:19other benefits like that the link will
  888. 34:20be in the description so with that being
  889. 34:23said I hope you enjoyed this video and I
  890. 34:24will see you in the next episode
  891. 34:28take care

About this transcript

This page contains the full transcript of Pathfinding (AI, Dynamic Paths, Modifiers) - Roblox Advanced Scripting #35 (2025) by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 6,830 words across 891 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.