Pathfinding (AI, Dynamic Paths, Modifiers) - Roblox Advanced Scripting #35 (2025) — Transcript
Full transcript
- 0:00okay braw Dev fans if you want to get my
- 0:02autograph then you have to go through
- 0:04this obstacle course for today's
- 0:05learning objective it should be a pretty
- 0:07simple one and if you can make it across
- 0:09this obstacle course without touching
- 0:11the lava parts then you get my autograph
- 0:13and I'm really excited to see what's
- 0:15going to happen here if you touch the
- 0:16lava parts then you die and I do not
- 0:19want to kill my fans over this cuz that
- 0:21would be really bad uh but yeah with
- 0:23that being said let's go ahead and get
- 0:25started in three two one all right oh
- 0:31let's go they're making it oh no a few
- 0:32of them are dying already okay but it
- 0:34looks like some of them are about to
- 0:35make it oh no they did not make it
- 0:37through no wait they're all dying oh my
- 0:41god oh nobody made it across oh this
- 0:45this is so
- 0:47unfortunate oh what have I done all
- 0:50right let's think about this for a
- 0:51minute what you just saw was called
- 0:54pathf finding which is the process of
- 0:57moving a character whether that's an NPC
- 1:00or a player towards a path to reach a
- 1:03certain destination while also avoiding
- 1:06obstacles and that's the basic
- 1:08definition of pathf finding and from
- 1:10this example that you just saw here all
- 1:12of these NPCs had their starting point
- 1:15over on that side and their goal was to
- 1:18find a path to reach the destination
- 1:21which is right over here while also
- 1:24avoiding these red parts and this is
- 1:26calculated using roblox's pathfinding
- 1:29service which is what I'm I'm going to
- 1:30be teaching you how to implement inside
- 1:31of this episode so get ready for this
- 1:33one I think this is going to be a really
- 1:34fun episode and with that being said
- 1:36let's go ahead and go straight into the
- 1:38episode now that we understand it let's
- 1:40Implement pathf finding inside Roblox
- 1:42studio and there's three items that we
- 1:45need for this pathf finding to work the
- 1:47first thing is the NPC or the character
- 1:50that we want to move the second thing is
- 1:52the goal that we're trying to reach and
- 1:54the third thing is the obstacle or the
- 1:56obstacles that are going to be in the
- 1:58way of this character to reach its goal
- 2:01so what we're going to do first is
- 2:03import a character and we're going to do
- 2:05that inside of Avatar and we're going to
- 2:07click rig Builder now it doesn't matter
- 2:09which Avatar you pick I'm just going to
- 2:11click on block Avatar and what I'm going
- 2:14to do is rotate this this way and I'm
- 2:18just going to quickly add a part by
- 2:21going to model and then hitting part I'm
- 2:23just going to scale this down I'm going
- 2:25to change the color of this to some sort
- 2:27of blue I'm going to turn can cide off
- 2:31and anchored on and I'm just going to
- 2:32move this alongside here and finally we
- 2:35want an obstacle so I'm just going to
- 2:37make another part right in front of the
- 2:40goal and I'm going to Anchor this and
- 2:43this is basically our initial setup so
- 2:47what we have going on here is an
- 2:49obstacle in the way of this character to
- 2:51reach its goal which is over here with
- 2:53this button and if we used normal
- 2:56methods of moving the character like
- 2:58let's say the humanoid move to then it's
- 3:00just going to run into the wall like
- 3:03this without ever having to reach the
- 3:05goal because this obstacle is in the way
- 3:07so this is where pathfinding service
- 3:09comes in where we want pathf finding
- 3:11service to calculate a path that is
- 3:14going to make it from where it is to its
- 3:17ending point now how is this path
- 3:21calculated well it's calculated by
- 3:23finding the shortest path possible which
- 3:25is the default way of pathfinding
- 3:27service to find the best path for this
- 3:29character
- 3:30and the way that I can show this to you
- 3:32is through scripting which is what we're
- 3:34going to do right now so what we're
- 3:35going to do is go to rig we're going to
- 3:37hit the plus sign and we're going to
- 3:39insert a script so the first thing we're
- 3:41going to do is locate pathf finding
- 3:43service by saying local path finding
- 3:46service equals game colon get service
- 3:49pathfinding service and then we're going
- 3:50to make reference to our NPC and also
- 3:53our goal part so we're going to say
- 3:56local NPC equals script. parent and then
- 3:58we're going to say local humanoid equals
- 4:01NPC colon wait for Child open to close
- 4:05parentheses humanoid and then finally
- 4:07our goal part which we're actually going
- 4:09to rename our goal part here so I'm just
- 4:11going to select this rename this to goal
- 4:14part and then we're going to go back to
- 4:16our script and say local goart equals
- 4:19game. workpace colon wait for child
- 4:23goart just like this so the next thing
- 4:25we need to do is create our path by
- 4:27saying local path equals
- 4:30path finding service colon create path
- 4:35open and close parentheses just like
- 4:36this now this takes in one parameter
- 4:38which is a table full of information
- 4:41that we can use to change our table to
- 4:43our liking but just for Simplicity sake
- 4:46we're just going to leave this blank for
- 4:47now so this creates a path for us and a
- 4:51way that I can help you visualize this
- 4:53is if I go into the game I'm going to
- 4:55show you this model that has these
- 4:57Waypoint parts now don't worry about
- 4:59adding this inside of your game this is
- 5:01just to help us visualize it so when we
- 5:03create a path it's going to create what
- 5:05are called way points so each of these
- 5:07balls here represent way points and
- 5:09we're going to get into this a little
- 5:11bit later but this is basically our path
- 5:13right now if you want to visualize it
- 5:14like that so what we need to do with
- 5:17this path is we need to now calculate
- 5:20the path that goes from our starting
- 5:23position which is our character to our
- 5:25ending position which is over here and
- 5:28so the way we're going to do this is
- 5:29we're going to go to our script we're
- 5:31going to drop two lines and we're going
- 5:32to say path colon compute async open and
- 5:37close parentheses and this takes in two
- 5:39parameters the first one is the starting
- 5:41position and the second one is the
- 5:43ending position so the starting position
- 5:45is going to be our character which we
- 5:48can say is going to be our humanoid root
- 5:50Parts position because that's where our
- 5:51character is so what we're going to do
- 5:53is say NPC do primary part. position
- 5:57which is the human node root part and
- 5:59then our ending position is going to be
- 6:02our goal part over here so we're going
- 6:04to say goal part. position just like
- 6:06this so once we tell Roblox to compute a
- 6:10path it's going to take this path that
- 6:12we created and it's going to try and
- 6:14find the best path for us so if I were
- 6:16to give you a visualizer this is some
- 6:20this is like a path that Roblox might
- 6:22create for us so it takes our original
- 6:24path and it's going to scatter it along
- 6:26this obstacle to try and find the most
- 6:28optimal path now again this is just to
- 6:30help us visualize it you don't have to
- 6:31add this inside of your game and we did
- 6:33not add this uh through our script or
- 6:35anything like that so it's going to look
- 6:37something like that once we use compute
- 6:39async now here's the thing roblox's
- 6:42pathfinding service is known to not find
- 6:46paths when it seems pretty obvious uh
- 6:49it's definitely not the best framework
- 6:51when it comes to creating path finding
- 6:53modules or Frameworks and things like
- 6:55that so at times it can fail a lot which
- 6:58is why we need to wrap this compute
- 7:00async inside of a PE call so what we're
- 7:03going to do here is say local success
- 7:06comma error message equals P call open
- 7:11close parenthesis function open close
- 7:13parentheses enter and we're going to
- 7:15throw this inside of here now if you're
- 7:18iffy on PE calls I encourage you to
- 7:19watch my data stores video because I go
- 7:21into more detail about that but this PE
- 7:23call is pretty important when we're
- 7:25using compute async okay so now what
- 7:27we're going to do is check if this was
- 7:29successful so we're going to say if
- 7:31success then we can use this path to our
- 7:33advantage but if this was not successful
- 7:36then what we're going to do is write a
- 7:38warning message to the console um with
- 7:41our error message that we got from this
- 7:44PE call up here okay so now that we have
- 7:47this what we want to do is we want to
- 7:49take each of the way points which is
- 7:51what I mentioned earlier uh these balls
- 7:54being which I mentioned earlier these
- 7:56points are all called way points so we
- 7:59can do this inside of a for Loop if we
- 8:01want we're just going to say
- 8:034core comma um Waypoint in pairs open
- 8:08close parentheses and we're going to say
- 8:10path colon get way points just like this
- 8:14and we're going to say do and what we
- 8:16can do here to help us visualize our
- 8:19path is create a part that represents
- 8:21each Waypoint so what we're going to do
- 8:23is say local part equals instance. new
- 8:27uh we can say part and we can put this
- 8:30Parts parent so part. parent equals
- 8:34game. workpace now we're going to make
- 8:36this look like a visual Waypoint just
- 8:38like I showed you earlier and the way
- 8:40we're going to do that is by changing a
- 8:41few properties so we're going to say
- 8:43part. material equals neon we're going
- 8:47to say part. anchored equals true and
- 8:51then we're going to say part. Can
- 8:53Collide equals false and then we're
- 8:55going to change the shape of it too so
- 8:58part. shape equals fall just like this
- 9:02and finally we need to change the
- 9:04position of this part to reflect the
- 9:06position of each Waypoint so what we're
- 9:08going to do is say part. position equals
- 9:12Waypoint
- 9:13doos and this should pretty much give us
- 9:17a visual representation of the path that
- 9:20has been created for us so if we go to
- 9:21the script and if we hit test and hit
- 9:24run then what we should see is that this
- 9:27is something our script did this script
- 9:30first created a path then it computed
- 9:32the path from its starting position and
- 9:34then it tried to find the best path
- 9:36towards its ending position so we've
- 9:38been able to successfully find a path
- 9:40that works for this NPC and I hope that
- 9:42so far that this has been making sense
- 9:44to you so that we can now move on to
- 9:45more complicated things so we've now
- 9:47been able to calculate our path but
- 9:49let's make it so that the NPC actually
- 9:52moves to each way point to reach the end
- 9:54goal and the way we're going to do this
- 9:56is we're going to drop two lines and
- 9:58what we're going to do is say
- 9:59humanoid colon move to open and close
- 10:03parenthesis and what we're going to do
- 10:04is pass in the Waypoint do position just
- 10:07like this and make sure this is the
- 10:09humanoid move to method and not the
- 10:11character move to method and once the
- 10:13humanoid is moving we want to have an
- 10:15event so that when the humanoid moves to
- 10:18this Waypoint position then it's going
- 10:20to go to the next Waypoint it's going to
- 10:22create the Waypoint and then the
- 10:23humanoid is going to move to that one
- 10:25and it's going to keep on doing that
- 10:26until all the way points have been
- 10:28traversed
- 10:29so we can do this by saying humanoid
- 10:32Dove to finished colon weight open and
- 10:37close penes just like this so it's going
- 10:38to move to the Waypoint position and
- 10:40then it's going to wait until it's
- 10:41finished and then it's going to go to
- 10:42the next one just like this so if we go
- 10:44into the game and hit run then what we
- 10:46should see is that for every single
- 10:48Waypoint it looks like it pretty much is
- 10:51working the rig moves from one way point
- 10:53to the next and then it's going to move
- 10:55on to the next one and then the next one
- 10:56and it's going to keep on going until it
- 10:58reaches the end and we basically have a
- 11:00moving NPC script now let's say we have
- 11:02a situation where we want the NPC to
- 11:04jump over obstacles but fails to do so
- 11:07because we haven't scripted it let me
- 11:09show you what I mean so I'm just going
- 11:10to take this wall and I'm going to move
- 11:11it over here I'm going to scale it just
- 11:15like this and I'm going to duplicate
- 11:18this again scale it like so move it up
- 11:22here and I'm just going to create two
- 11:24hurdles just like this and the intention
- 11:27behind this is we want the NPC to start
- 11:31here and we want the NPC to move to this
- 11:34goal on the other side here and it needs
- 11:36to do that by being able to jump over
- 11:38these hurdles right over here if it
- 11:40can't jump over these hurdles then it's
- 11:42going to get stuck so let me show you
- 11:44what happens if we run the script as
- 11:49is okay that was a that was a bad
- 11:52demonstration let me increase the height
- 11:55of these parts now it should be
- 11:58difficult for this NPC to get across
- 12:00there we go now this NPC is stuck it is
- 12:02trying to get to the end goal but it
- 12:05fails to do so because it can't jump so
- 12:06we need to add this ourselves inside of
- 12:08our script so I'm going to hit stop I'm
- 12:10going to go to our script and there's
- 12:11two things that we need to do um the
- 12:14first thing is we need to specify in our
- 12:16path that the character is able to jump
- 12:19when it's following along the path and
- 12:21if you remember we do that inside of
- 12:23here with our agent parameters table so
- 12:26what I'm going to do is put in an open
- 12:29and close curly braces and I'm going to
- 12:31hit enter just like this so these
- 12:32parameters are going to specify some
- 12:34things that the NPC is allowed to do
- 12:37such as being able to jump and the way
- 12:39we're going to do that is by saying
- 12:41agent can jump equals true just like
- 12:46this I'm going to add a commum at the
- 12:47end now by default agent can jump is
- 12:50already set to true so we don't really
- 12:51have to do this but just to introduce to
- 12:54you this configuration table we're going
- 12:56to add this inside of here okay so once
- 13:00the humanoid is attempting to move to
- 13:02this Waypoint position what we want to
- 13:04do is is let the humanoid jump if we
- 13:07need to make the humanoid jump and the
- 13:10way we do this is through a property
- 13:12inside of the Waypoint called action so
- 13:16this is our Waypoint action that's going
- 13:17to tell us if the player if the
- 13:20character needs to jump and this is what
- 13:22it's going to look like so right after
- 13:23the move to Method we're going to say if
- 13:27Waypoint do action equals enum do
- 13:32pathway Point
- 13:35action. jump then we're going to make
- 13:39the humanoid jump so we're going to say
- 13:41humanoid do jump equals true just like
- 13:46that so now if we go into the game and
- 13:49hit run then what we should see is that
- 13:51for a specific Waypoint that requires
- 13:54the NPC to jump the NPC actually does
- 13:57jump so for this Waypoint right here the
- 14:01NPC needed to jump for this Waypoint
- 14:03right here the NPC needed to jump in
- 14:05order to reach across to the end so
- 14:07that's how we make the humanoid jump now
- 14:09let's say we have another situation
- 14:11where we need the player to climb an
- 14:14object to be able to reach its goal now
- 14:16what I'm going to do is I'm just going
- 14:17to move this um this now what I'm going
- 14:19to do is I'm just going to move this
- 14:21goal part here and I'm going to move
- 14:24this part over here and I'm going to
- 14:26scale it up just like this and what also
- 14:29going to do is insert a trust part which
- 14:32is basically a ladder so I'm just going
- 14:34to scale this up just like this and have
- 14:36the goal part up here so is this NPC
- 14:40going to be able to climb up this part
- 14:41and reach its goal well it looks like
- 14:44it's not going to be able to do that it
- 14:46has unsuccessfully been able to find a
- 14:48path so what we need to do is specify in
- 14:51our parameters up here by saying agent
- 14:54can climb equals true and that that is
- 14:59the only thing we need to do to
- 15:00determine if the if the NPC is allowed
- 15:03to climb parts or not so if we now hit
- 15:05run then the NPC should be able to climb
- 15:09this truss and reach the end goal just
- 15:11like this so these are a few properties
- 15:14we can change with the agent parameters
- 15:16table here to modify our path to make it
- 15:18fit our needs now uh in the Roblox
- 15:21documentation there are more properties
- 15:22that you can look at which I'll leave a
- 15:23link to in the description if you want
- 15:25to experiment with that but those are
- 15:27some things that we can add to our
- 15:29pathfinding script to make this more
- 15:31robust okay so before we move okay so
- 15:34before we move on there's one thing I
- 15:35forgot to do with our PE call check so
- 15:38with this line right here line 16 if
- 15:41success then it's not going to be enough
- 15:44to tell us if the PE call was successful
- 15:46because we also need to check if the
- 15:48path that was created was also
- 15:50successful as well so we need to add
- 15:52this to our if statement by saying if
- 15:55success and path. status
- 15:59equals en. paath status. suuccess so it
- 16:04has to require both of these criterias
- 16:07in order to uh make it a successful path
- 16:10but if not then we're going to have a
- 16:12warning message in which we can pretty
- 16:14much specify here by saying the path has
- 16:20unsuccessfully been created and we can
- 16:23just throw in the error message right
- 16:24after that so if I were to test this out
- 16:27let's comment out the agent K CL climb
- 16:29parameter and we're going to hit run
- 16:30then what it should display in the
- 16:32output is the path has unsuccessfully
- 16:35been created and our error message
- 16:36should be displayed to the right of that
- 16:38so that was a little um so that's a
- 16:40little thing I wanted to add there
- 16:41before we move on to the next part so
- 16:43right now our pathfinding script
- 16:45calculates the shortest path from a
- 16:47start position to an end position and
- 16:50that is all what the script is doing but
- 16:52what if we were given a situation where
- 16:54instead of finding the shortest path we
- 16:57wanted so that this path finding script
- 16:59will make this NPC find the smartest
- 17:03path or a path that's more reasonable
- 17:05than whatever the shortest path is going
- 17:07to be and I'm going to be showing you
- 17:08how to do this by adding what are called
- 17:11pathf finding modifiers and I'm going to
- 17:13be showing it through these examples
- 17:16that I have over here starting with this
- 17:18first one over here so what I'm going to
- 17:20do is take this rig I'm going to move it
- 17:22over here and I'm going to take this
- 17:23goal part and move it somewhere along
- 17:25here at the end okay so we're presented
- 17:28a situation here where there are two
- 17:31paths that this NPC can take it can
- 17:33either take the wood path or it can
- 17:35either take the Neon Path and I want you
- 17:38to think of this Neon Path as a lava
- 17:41part obviously if this rig were to try
- 17:44and go to this lava part then it's going
- 17:47to die and we don't want that to happen
- 17:49we want this NPC to go towards the wood
- 17:51path so that it can reach its end goal
- 17:53and we can do this by setting what's
- 17:56called material costs and the the basic
- 17:59idea behind this is that we want to make
- 18:01it so that this wood path is more
- 18:04traversible than this Neon Path by
- 18:07setting a cost of how easy this material
- 18:11is to walk on versus a different
- 18:13material and this can be done through
- 18:15our script so if we go to our script we
- 18:17are going to go inside of our parameters
- 18:19table and we're going to add another
- 18:21parameter called costs and we're going
- 18:23to set this equal to a table so we're
- 18:26going to say open and close curly
- 18:27brackets and we're going to hit enter
- 18:28and I'm also going to add a comma here
- 18:30so the idea behind this costs table is
- 18:33that our keys are going to be our
- 18:35materials and then our values are going
- 18:38to be the cost of that specific material
- 18:41so our two materials that we have here
- 18:43are wood and neon so what I'm going to
- 18:46do here is say wood equals and then we
- 18:49can set this to any number we want so by
- 18:52default the number is one so if we set
- 18:54this to one then this is going to have a
- 18:57high priority or this is going to be a
- 19:00material that's going to be easy to walk
- 19:02on um so what we're going to do is also
- 19:04add another material here called neon
- 19:07and we're going to set this to a number
- 19:09that's higher than one so something
- 19:11bigger like 100 so if we do this then
- 19:14essentially what this is saying is that
- 19:15this wood path is going to be easier to
- 19:18travel across than this Neon Path that's
- 19:21over here and this is for any part that
- 19:23has the wood material or the neon
- 19:25material so now if we go into test and
- 19:28hit r run we should see that this NPC is
- 19:31going to Traverse along this wood path
- 19:34over here to reach its end goal and we
- 19:36have successfully avoided the Neon Path
- 19:39and so that's how we set material costs
- 19:41to make smarter decisions uh for our
- 19:44pathfinding script okay so you might
- 19:46have noticed something if I hit run uh
- 19:48so that we can watch this again you can
- 19:50see that this NPC as it was walking
- 19:53along this path it sort of touched this
- 19:55neon part uh even though we don't want
- 19:57it to touch the neon part at all the
- 19:59reason that it's doing this is because
- 20:01our minimum horizontal space for
- 20:04calculating the shortest path possible
- 20:06is a really small number by default
- 20:09which is why the path was really close
- 20:13to this neon part and so what we need to
- 20:15do is increase that distance
- 20:19horizontally and we can do that by
- 20:20adding a parameter inside of our
- 20:22parameter table so what I'm going to do
- 20:23is drop a line and I'm going to say
- 20:26agent radius equals and as I mentioned
- 20:30before the default is two so we can make
- 20:32the size of this bigger by let's say
- 20:34four and this will give us a different
- 20:36result so if we go into the game and hit
- 20:38run then what we should see is that
- 20:40there's now more horizontal space for
- 20:43this NPC to try and calculate the
- 20:45shortest path while also maintaining
- 20:47some space and so that's how we were
- 20:49able to change that here and that's how
- 20:50we update this path to make it better
- 20:53but I want you to be careful because if
- 20:54you make it too big like let's say 20
- 20:56then it's going to have two too much
- 20:59space to be able to find the path that
- 21:01it needs with how big this radius is so
- 21:03I want you to be careful with doing
- 21:05these configurations and you can also
- 21:07add what's called Agent height which is
- 21:10the exact same thing as agent radius
- 21:12except it determines vertical space and
- 21:14not horizontal space so I want you to
- 21:16consider these two parameters as you add
- 21:18them inside of your um pathfinding
- 21:20script and so I hope you found this part
- 21:22to be helpful so that was the first type
- 21:24of pathfinding modification it was
- 21:26through changing material costs but now
- 21:29the next thing I want to show you are
- 21:31unwalkable regions or regions that
- 21:34shouldn't be taken into consideration
- 21:37when we're calculating a path to reach
- 21:39the end goal so what I'm going to do is
- 21:41take this NPC move it over here move the
- 21:43goal part over here and let's imagine
- 21:46that this is like a danger zone and we
- 21:47don't want the NPC to go through this
- 21:50danger zone but instead we want it to
- 21:52walk around it um the way we can do that
- 21:55is we can add what's called a path
- 21:58finding modifier object inside of this
- 22:00part over here so what I'm going to do
- 22:02is hit the plus sign inside of this part
- 22:04and I'm going to insert a path finding
- 22:07modifier just like this and we can do
- 22:10the same thing for what we did here with
- 22:12setting material costs but instead set a
- 22:15material cost for this danger zone
- 22:17itself and the way we do that is by
- 22:19clicking on this pathfinding modifier
- 22:21and there's this label property so we
- 22:23can call this whatever we want we can
- 22:25just say danger zone just like this and
- 22:30what we're going to do is go to our
- 22:31script and for our costs table we're
- 22:33just going to drop another line and
- 22:35we're going to say danger zone which is
- 22:37the name of our label and we're going to
- 22:39set this equal to a very big number like
- 22:41let's say 1,000 in this case so anything
- 22:45that has the label danger zone it's
- 22:47going to look at this cost table and
- 22:50it's going to see that we don't want to
- 22:52Traverse through this because it's a
- 22:53really large number and the reason that
- 22:55this is useful to us is because any
- 22:57object that that's now contained within
- 23:00this danger zone is now considered
- 23:02non-traversable which is why having this
- 23:05pathfinding modifier object inside of
- 23:07here is pretty useful so if we were to
- 23:10test the game now so if I were to hit
- 23:12run then what we should see is that it's
- 23:14now going to avoid this danger zone and
- 23:17try to calculate the best path um from
- 23:20its starting position to its ending
- 23:21position and so that's how we add danger
- 23:23zones or zones that are not possible to
- 23:26walk through when we're modifying our
- 23:28pathfinding script okay so I want to
- 23:29show you this last modifier example in
- 23:32which we have a situation where we have
- 23:34two doors this is door one and this is
- 23:36door two so what I'm going to do is move
- 23:38this goal part inside of here and I'm
- 23:41going to move the NPC inside of here so
- 23:44with these two doors we want to add
- 23:46pathf finding modifiers to each of these
- 23:49doors and set different costs for them
- 23:51so that we can determine which door this
- 23:53NPC should take and so here's what we're
- 23:55going to do I'm just going to select
- 23:57this door right here and I'm going to to
- 23:58insert a pathfinding modifier and I'm
- 24:01going to do the same thing with door 2
- 24:03as well so with each of these labels
- 24:05we're going to give them unique labels
- 24:07so for door one I'm just going to say
- 24:09door one and then for door two I'm just
- 24:11going to say door two just like this and
- 24:13now if we go inside of our script we can
- 24:15add this to our costs material table so
- 24:18we can say door 1 equals let's say 10
- 24:22and then door 2 is equal to 10 as well
- 24:25so these are our costs for these two
- 24:28doors but if we want it so that the NPC
- 24:31wants to go through a specific door
- 24:33rather than simply picking the shortest
- 24:34door then we will specify door two to be
- 24:39the door that it wants to go to so we
- 24:40want to set that to a lower material
- 24:43cost so if we go back to our script
- 24:45we're going to say door 2 is equal to
- 24:47let's say five so this one has a smaller
- 24:50material cost than door one so the NPC
- 24:54will not pick the shortest path which is
- 24:57going to be door one but instead it's
- 24:59going to go inside of door 2 because it
- 25:01has an easier traversal cost so if we
- 25:04hit run then what it's going to do is
- 25:06it's going to avoid door one and it's
- 25:08going to go into door two and it's going
- 25:10to reach its goal so that's another
- 25:11example of using pathfinding modifiers
- 25:13and I hope that this has given you
- 25:15insight to try and use these to make
- 25:17smarter pathfinding decisions inside of
- 25:20your Roblox game okay so I utilized
- 25:22pathfinding service to create this NPC
- 25:25following player script as you can see
- 25:28right here here and essentially what
- 25:29it's doing here is with this NPC it's
- 25:32using pathf finding service to calculate
- 25:34the shortest path to any player that's
- 25:37within the range of this NPC so if I
- 25:41were to walk away far enough from the
- 25:44NPC then it's no longer able to detect
- 25:46me and it's not going to chase anyone
- 25:48but if I go back into the NPC's range
- 25:51which is about 50 studs then it's going
- 25:53to try and follow me until it reaches uh
- 25:56to where I am another interesting thing
- 25:58about about the script is this can work
- 26:00with any player that's within its range
- 26:02because it's going to detect the closest
- 26:04player and I'm going to show you right
- 26:06here that if I walk away from here then
- 26:08it's going to chase player two instead
- 26:10of me because now player two is closer
- 26:13to the NPC so if I were to show you with
- 26:15player two if I walk away from the NPC
- 26:18then it's now going to follow player one
- 26:20and if both of us are far enough away
- 26:22from the NPC then it's just not going to
- 26:24chase anyone and it's just going to
- 26:25stand there so this is a really cool
- 26:27implementation with pathf finding
- 26:28service and I want to show you the
- 26:30script that made this possible okay so
- 26:33let's break down our NPC following
- 26:35player script now my goal here for this
- 26:37chapter is to help you understand how we
- 26:40use pathfinding service to create uh to
- 26:42create systems like this so that you can
- 26:44hopefully be inspired to use pathfinding
- 26:46service inside of your own scripts to
- 26:48create really cool stuff like this so
- 26:50what we're going to do is open up the
- 26:53rig and there should be one script
- 26:55called follow player so if we open this
- 26:57up then this should pretty much be the
- 26:59entire script for our NPC following
- 27:02player script so let's start at the top
- 27:04here there is a variable here called
- 27:07range which is a constant variable and
- 27:09I'm going to explain what this is in
- 27:10just a little bit but we basically have
- 27:13a function here called create player
- 27:14path that takes in a Target parameter
- 27:17Target referring to the closest player
- 27:20that this NPC has detected so we want to
- 27:22get this player as a Target so what this
- 27:25does is it computes a path uh from the
- 27:28NPC to the Target and if it wasn't
- 27:31successful then it's going to return nil
- 27:33otherwise it's going to return the
- 27:34player path that has computed I think
- 27:36this function is pretty self-explanatory
- 27:38so now if we move on to the next
- 27:39function this one is going to be a
- 27:41little tricky so I want to take some
- 27:43time to explain this so we have a table
- 27:46here called magnitudes now I haven't
- 27:48talked about magnitudes on this channel
- 27:51really so I think this deserves a small
- 27:54introduction okay so let's answer the
- 27:56question of what is a magnet magude so
- 27:59in the context of pathfinding service we
- 28:01want to use magnitude as a way to
- 28:04determine what the distance between the
- 28:07NPC and the player is so if we were to
- 28:11try and figure out the distance between
- 28:13the player and the NPC then all we need
- 28:16to do is calculate the magnitude also
- 28:18known as the distance from the player to
- 28:22the NPC and this will give us a result
- 28:25in studs so right now uh with the this
- 28:28player uh if we were to calculate the
- 28:30distance it's going to be maybe about
- 28:32like 75 studs let's just say for this
- 28:34example but for this range right here
- 28:37that this NPC is a part of this is let's
- 28:40say a range of 50 so right now this NPC
- 28:43cannot find my player because I am
- 28:46currently outside of this NPC's range
- 28:48but if I were to let's say get closer so
- 28:51I would be 25 studs in magnitude between
- 28:54uh our distances then I would be within
- 28:57the range of the NPC to come and detect
- 28:59me so that's something that's really
- 29:00interesting about this and that's what
- 29:02we use magnitudes for it's to calculate
- 29:04the distance from the NPC to the player
- 29:06but this can be used outside of
- 29:08pathfinding as well but this is just a
- 29:10really good way of using magnitudes so
- 29:12if you go back to our script this is
- 29:14essentially a table of all the players
- 29:18with their distances from this NPC right
- 29:21here and so the idea here is we want to
- 29:24find the player that has the smallest
- 29:27distance so so if I were to trace
- 29:28through this code with you we're going
- 29:30to take every player and we're going to
- 29:32calculate the distance from that player
- 29:34to the NPC and if the distance of that
- 29:37specific player is within range with
- 29:40this variable up here which is 50 studs
- 29:42then we're going to add to our
- 29:44magnitudes table using this value right
- 29:47here which is another table so the first
- 29:48entry is the distance and the second
- 29:50entry is the player so if I were to help
- 29:52you visualize it like this this table of
- 29:55magnitudes is basically going to be a
- 29:57table with a distance as the first value
- 30:00and the player object as the second
- 30:02value so if I were to add another table
- 30:04here this would be let's say brev this
- 30:06is the magnitudes table it's full of
- 30:09tables where the first entry is the
- 30:11distance and the second entry is the
- 30:13player now the next thing we do is use
- 30:16table. sort which basically takes all
- 30:18the entries of magnitudes and it's going
- 30:20to compare the distance which is what
- 30:23this hard bracket one signifies is that
- 30:25if we have one value in the table and
- 30:26another value in the table we want to
- 30:28compare its distances and we want to put
- 30:30the entries with the smallest distances
- 30:32first and put the ones with the larger
- 30:35distances later inside of the list and
- 30:37so that's what this does and we also
- 30:38want to check if the magnitudes table is
- 30:42at least one because if it was Zero then
- 30:44there would be no target for this NPC to
- 30:46try and uh follow so that's what this is
- 30:49for and the target is set to magnitudes
- 30:52which gets the first entry because we
- 30:54now have the smallest entry first and we
- 30:57want want to get the player which is
- 30:59what this hard bracket 2 signifies
- 31:01because player is right over here so
- 31:04that's how we get the Target and once we
- 31:06do that then we're going to return that
- 31:07Target so that's what this function does
- 31:10and if it didn't make sense to you then
- 31:11I encourage you to rewatch this part but
- 31:13uh moving forward we have this while
- 31:15true do Loop that first gets the closest
- 31:18player with the function that I just
- 31:19explained right here and if it finds the
- 31:21target then it's going to wait until the
- 31:23target uh has loaded into the game with
- 31:25their character and then it's going to
- 31:27create a player path with that Target
- 31:30and it's going to create the way points
- 31:31then it's going to create a visualizer
- 31:33and then it's going to make the NPC move
- 31:35to each Waypoint and if it hasn't
- 31:36detected a Target initially then this
- 31:39NPC will not Chase anyone because there
- 31:42is no target within its vicinity so it's
- 31:44just going to stay in place and that's
- 31:46basically what this does and this while
- 31:48true do statement is going to fire every
- 31:51single heartbeat using run service and
- 31:53that is basically how we create our NPC
- 31:56following player script it's definitely
- 31:58isn't a perfect script I would actually
- 32:00consider this to be a more simple
- 32:02implementation because there are some
- 32:04things that I left out in the script
- 32:06like adding a feature where it can
- 32:08detect blocked paths so that it can
- 32:11reroute itself to look for my character
- 32:14uh and also other things like being able
- 32:15to jump on obstacles without having like
- 32:17a weird system where it would just I
- 32:19don't know run into the wall just like
- 32:21you're seeing right here so that it
- 32:22would actually be able to jump when it
- 32:24needs to but sometimes it wouldn't do
- 32:26that and it would just lead to some
- 32:28other problems so you would have to
- 32:31figure out how to create a more advanced
- 32:33system by adding onto the script but
- 32:35what I've shown you here is a more
- 32:37simple implementation and I hope that
- 32:39this has given you inspiration to expand
- 32:41your script to add more to it and so
- 32:45that is basically what I wanted to show
- 32:46you inside of this episode and one thing
- 32:48that I want to answer is whether
- 32:50roblox's pathfinding service actually is
- 32:52good and my answer to that would be is
- 32:56it's definitely not like an amazing
- 32:57system system but it gets the job done
- 32:59when you need it to if you were to try
- 33:01and make this NPC go through like a a
- 33:04difficult Obby then it's not going to
- 33:06compute a path for you because you kind
- 33:07of have to treat it like it's fragile it
- 33:09has a lot of limitations when it comes
- 33:11to being able to calculate pads that
- 33:13should be obvious but aren't and so you
- 33:17have to take it with a grain of salt
- 33:18that roblox's path finding service alone
- 33:21probably isn't going to reach your goals
- 33:23that you wanted to achieve like the the
- 33:26top Roblox developers would be using
- 33:27their own pathfinding service for this
- 33:29kind of thing and so if you want to dive
- 33:32deeper into pathfinding then I encourage
- 33:34you to do so because you might because
- 33:35you might make something really cool out
- 33:37of this oh anyways I'm done yapping
- 33:39about pathfinding but it's definitely a
- 33:40really interesting concept to dive
- 33:42deeper into if this is something you're
- 33:44interested in so if you enjoyed this
- 33:45video then I encourage you to leave a
- 33:47like and also subscribe if you haven't
- 33:49already and also leave a comment down
- 33:51below uh if there's something that
- 33:53you've been able to implement with
- 33:54pathfinding service that you found to be
- 33:57pretty useful and that you're proud of
- 33:58creating uh I would definitely like to
- 34:00hear your thoughts about pathfinding and
- 34:02I would also like to give a shout out to
- 34:03my patreon members for their continued
- 34:05support and I definitely appreciate
- 34:07their support because it definitely
- 34:08motivates me to continue working on
- 34:09these videos for you guys um so if you
- 34:12want to become a patreon member the link
- 34:13will be in the description you'll get
- 34:15access to my scripts and also Early
- 34:17Access to these videos that come out and
- 34:19other benefits like that the link will
- 34:20be in the description so with that being
- 34:23said I hope you enjoyed this video and I
- 34:24will see you in the next episode
- 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.