Advanced Roblox Scripting Tutorial #21 - Pathfinding (Beginner to Pro 2019)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right yo it is a defconn gear back with a brand new video and guys in today's roblox scripting tutorial i will be teaching you about path finding service now what is path finding service you may ask well let me just go out and show you alright watch my character when I hit a run okay so he's you could probably guess what he's gonna do but look he goes along this path and it's actually a calculated path from the computer and he goes right to this part right here now it may seem a little confusing you may be wondering how he does it but it's with a service called path finding service and that's what we're learning at today's tutorial so let's go ahead and get started okay guys so first things first well so we haven't get a bank Blake baseplate out here and we need to make sure we add our script and also we need to have a character to move right we need to have something to move around okay so let's go ahead and insert a quick little character now um actually before we get started I want to say make sure you leave a like okay cuz it's gonna be insane video and also you know if this does help you maybe subscribe you can see more awesome script in tutorials like this and yeah okay anyways so how do you insert your character that's the first thing we're gonna need we're gonna need like a little you know a little character to actually move around now how do we get this little guy well you can go ahead and go into your tool box and insert like an MPC I guess you can find like a free one there and as you can see right here you have this guy and he even has animation scripts it seems like so that's kind of nice I've never used one before but I'm sure it works fine I think so I don't know but anyways how I do this or how you can get your own avatar is there's a common plugin used in roblox called load character it's a pretty popular one it's safe you don't even actually use it in your like in a script or I guess oh you okay you know what I mean but anyways basically what the plug-in does is it kind of just you clicked a little button and the type and username mine is use code tap by the way guests use code tap and when my girl bucks in premium and there we go that's my name and now I can go ahead and either spawn the are six version which is just the version with like you see like just an arm like that like oh crap like just an arm like this or I get super on the ar-15 version which has you know how's the hands right here and then it has the lower arm and it has the upper arm okay so that doesn't look that right because I just yeah anyways you get the point I think right but we're gonna using the ar-15 just because it's pretty simple and yeah why not okay now let me delete this guy right here there we go and I'd like to let's talk on be the description a link of this plug-in will be in the description below if you want to enter your own character well you're gonna need a character so I would suggest getting this anyways but if you can find your own you can do that too okay now the important thing is that this character has a humanoid and actually I just move the arm so let me just answer than anyone but yeah the important thing is this character has a humanoid we want to make sure the character has a humanoid because it needs to be able to use this function called move tool but that we do this is just an irregular character as you can see I have a humanoid okay let's go and start it let's go ahead and get started scripting finally okay so I'm just gonna insert a script into the thick you the the the model or the character because it's pretty simple there and then I'm gonna go ahead and just name this you know pathfinding script or path finding script or you don't even if you don't want to but I like to name my scripts anyways um first things first we need to get our path finding service right obviously so that's pretty simple you just make a variable called path finding service and then you set it equal to game get service path finding service and now this is how you use all the events and functions of path finding service right okay so now also we need to actually get our human right we need to define our humanoid so our humanoid is equal to local human is equal to script pearance wait for child humanoid and then we want to get our torso - like little middle part that way we know where to start because the humanoid isn't actually like a part is not a position but the torso does so we'll do we'll do local torso is equal to script dot P Hrant wait for child torso there we go okay and that will be that's okay yeah all right next what do we want to do we want to go ahead and in some other stuff like sorry other stuff like you know actually computes a path and stuff like that so how do we do that you may ask well that's pretty simple you know you just kind of do it okay I'm sorry I'm fooling someone it's my right here okay okay so yeah we need to make the path alright so how do we do this so all you can make a path is you do local path is equal to path finding service and then create path there we go okay so basically you could just do this right here and type that in but we're not gonna do that because it you know it's kind of I don't know look weird to me but you can do that if you want to anyways that's not important okay so now we made the path now basically what this path is is right now it's just a memory right now it is just a piece of memory and it doesn't actually do anything yet this isn't actually a path because we don't first of all we didn't tell it where we wanted to go and we didn't tell it what we want to start okay so what you need to do for that is actually compute your path so now this is like a necessary function you need to do this if you want to have a path at all you do path or the name of your path : and then it's a function commute 8 or compute sorry not come you compute async okay and there you go now you can do now the two parameters for this function are the starting point in the ending point so where do you want it to start and where do you want it to answer obviously you wanted to start my character and for here this demonstration I'll put a part right here and I'll make it look a little bit nicer I'll make it black then I'll change the scale and make it like this okay there's a nice little part there okay next we want to actually name it real quick I'm gonna name its sorry and ending parts and there we go okay so we need to put the position values and though so we're gonna do torso dot position that's the starting value and then the ending value is the gamed workspace dot ending part dot position there we go and now we have the path okay now obviously this is a pretty simple path it's just a straight line unless there's a little bit of curve right here but the other is anyways but it's gonna calculate the path now we need to make it actually move we need to make our humanoid actually because right now we just made the path but nothing's happening to the path yet so how we do that is basically whatever paths are made right basically yeah yeah whatever paths are made there's a bunch of little like dots made okay because it needs to follow like a straight line right or if it was if it was over here okay and there was a part in the way right here okay it would obviously to go around it right and it if if you were to just do like human move to and then you did game that work space dot ending part dot position right you could do that right and it would it would try to move to it but the thing is it's it's gonna get stuck in the wall cuz it has no brain okay computers have no brain and they don't know where to go you just he just got stuck in the wall stuff and was the wall obviously to go back but you see he now he just in there we get the point right it got stuck in the wall so what path finding service does it calculates a path so it would go like this and then it would turn and then go like that and then it would go all over here okay so it kind of just goes like that and then that and then you and I'm saying right but it creates a bunch of little points imaginary points and will actually make that into parched to demonstrate it but it created a bunch of matter matter your point super black point here point their point their point their foot there and then it goes around and they go to point they put interpreter put there and just in the shortest possible length in the fastest time right it already transmitted in the shortest possible distance okay so we need to actually use these points and to tell our character to move to each point right that's what we're gonna do that's how you make it character follow a path make it move to each point and these points are called waypoints okay so what we're gonna do is we're gonna make a table of the waypoints because basically the the path was computed right the path whenever wrong we run the script now the path will be computed and it will exist but we need to actually find out what the path looks like so we get the points of it by doing local waypoints is equal to path get get waypoints there we go and then after we have that we want to make the character follow each part on our each Waypoint or go to each Waypoint on the path okay so we're gonna do for i through he actually won it will do for AI comma waypoints and you guys should know how to use these if you can't wait point you should know how to use these if you don't don't go watch my for in pairs video it's in the beginners series I think it's an advanced series I don't know but I have a bunch of trippin tutorials on this channel if you can learn you can learn from absolutely nothing ok you can learn about being a basic and you having nothing learned anyways so wait in pairs waypoints do ok so what we just did we just made the we made you know like well we're looping through the Waypoint so guess we're doing we're looping through each Waypoint in the in the table right here ok so what we're gonna do now is we're gonna make the character move to the Waypoint right pretty simple we're gonna do human move to move to Waypoint dot position okay because we're looping through so each Waypoint it has its gonna go okay so for IV through in Waypoint in pairs okay right here so and then it's gonna see right here it says Waypoint assessment the first one and it'll do human move to Waypoint opposition okay that's it right so let's go and try it on so he works it should right that's that's that's what it is so let's see and oh okay issue what's the issue here it still went to the wall now the reason why is you need to actually wait until when you got a weight function until it goes into it right so what what you do how you do that or you need a weight function to make sure you don't start moving again until it's finished moving to the first Waypoint okay so how do you do that as I do human dot move to finish now this is actually an event you can like connect a function to it if you want but it's pretty simple to just to : wait and then you can just put it again you know seconds in there so yeah just do that and then it'll basically wait until yeah it'll wait until the yes it'll wait until the I don't know how I can say this it'll wait until the player is finished moving okay and then we're gonna hand it run and see that works and oh look look at that okay so it went around okay now it obviously moved because it hit it wasn't anchored but you see it went around that way in personally like when I balled it I thought that it was shorted to go this way and I guess it's faster this way cuz I or maybe maybe this is wrong I don't know maybe he's been feeding a bad path I have no clue but you know you go to get I'm saying right it computes it tries to compute the shortest path I don't know if that's actually the fastest path but it might be I guess I mean you could measure it if you want but I can't I don't can't do that right now no the story will you wait too long anyways um okay now obviously that works right but this is first of all way too basic if it had to jump it couldn't jump okay like for example if we boxed in right now if we put in this guy in a box and actually I want to show you something real quick if we put him like this you see what he would do what he would do right now is he would go all the way around because he's smart he's a brain now you see he would go all the way around and go to there okay but if we put him in a box what's gonna happen now we didn't tell him how to jump or well you know you think you know how to jump already cuz we just kind of told him where to move to but he actually doesn't know how to jump he doesn't know how and basically what happens is whenever there's a waypoint every single point that is made right it actually has an action ok so there's actually variables of waypoints and it's there's Waypoint dot action ok I think that's it actually make sure that's it yeah wait point action ok and basically these actions are important because you want to make sure it can it can jump and stuff like that you know what I'm saying so yeah ok so there's an action for each thing so Waypoint action we can print the action if you want oh well actually let's see if that prints I don't know if it will actually print but we need to do it in here because it's looping through here but basically if there's an action for walking and as an action for jumping and each Waypoint has an action it tells you how to jump or walk now that didn't work obviously but what we need to do is actually see if the Waypoint action is jumping well then we want to make the human a jump right so we'll do if Waypoint dot action because it's looping through right and way point down action is equal to enum dots path Waypoint action that's a little weird let's how'd you get it so you do enum halfway point action because it saves it all there and then dots you see house jumping or walking little dot jump then we want to do human change states enum dots humanoid humanoid state type dot jumping ok University you may have nursing you may have never seen this before either this is basically how you change the state of a humanoid making it jump right so that'll make a jump now we could put it to like do beaky we also do if they played out action is equal to e num or opposite or enum dots path Waypoint action dot walk but there's really no point because if it's not jumping if it's not jumping then it's gonna be moving okay so there's no point in putting that there to because it open and put on the walk so yeah okay anyways now let's gonna test it out see if it works now I don't think it'll still I still think it won't work because okay yeah alright so it didn't work again because obviously it can't jump this high that was kind of the reason why wouldn't work but yeah I mean I'll take it out and show you improve it but it'll work now cuz it can jump this high so now let's see and see it jump it over like a freaking hop see those so high but you see a jump right over now if I were to move this or if I were to comment it out comments it out you can see here that it still won't work because it needs to know you need to tell it that it needs to jump he doesn't know he's a little stupid okay I know your little stupid buddy it's okay one day you'll learn just just okay alright I'm gonna stop talking to a robot now okay anyways um number the control Z that and give her that but uh there you go now this is the basics of pathfinding now we're not done just yet I want to do one more quick thing but this is the basics of Hathorne right this is how pathfinding works if i were to make that same map I just made or actually I'll just insert it right now if I were to just copy this over okay let me copy it real quick I'm sorry I'm copying it if I were to copy this over copy/paste then it would still work now let me delete this real quick because that's this is our ending part it would still work okay I can copy right there put it right there and it would still work this script is that it's that good okay and I'm not gonna make a little thing I'm sick as no point but it looks eat like he doesn't go from there it goes from the shortest possible way it can calculate and some time might be some times it might be wrong I'm not sure but you see what I'm saying it goes to like the shortest possible way and then it's so cool like how it does it I think it's that's honestly awesome now okay before you go ahead and make a zombie script okay be a little careful because it's assistant all of pathfinding and there's a few more things like like okay for example like right now if I run this okay and then I do this it gets stuck okay now it actually moved it because that part wasn't anchored but let me do that again an anchor this part real quick alright so like watch okay look watch it calculates the path right now okay it jumps over and now I put that right there he's stuck now why is he stuck well because the original path that was calculated is it says go here but now there's something new it didn't calculate it again it doesn't know this is here the path that was calculated originally doesn't have it has no clue that this part was gonna be here so it oh it worked eventually anyways I don't know why it worked dimensionally well actually I do know why because it waited but anyways beYOU can get the point right it can get blocked so there's an event for that too it's it's called blocked now that it's path not blocked and there's also one yeah yeah paths are blocked so you want to check if that's blocked sometimes or also one more thing - okay oh yeah yeah alright so yeah there's path blocked okay and that's how you check if it's blocked okay that if that word is happening that would be fine right now and then also one more thing - there's you there's a way to check if the path is successful so you can you if path dot status is equal to enum dots path status dot no path or success so if it's successful then that that will no - so yeah but anyways you get that point right there right so yeah so it's not gonna be perfect there's still something to figure out and also you may want to use Ray casting for other things like that too but we'll learn about that in the future don't worry and yeah all right so before I end this tutorial I do want to add that little script that made that whole looking like that glowing path thing looked kind of cool so I'm gonna copy that over because I'm not gonna save it again but you should know how to do that basically what I'm gonna do is just create the I'll create a part right here okay yeah for each Waypoint I'm gonna create a part okay and then I'm going to make the shape a ball and then I'm going to make the material I'm gonna do partner material cuz neon and then I make the size just like that it looks kind of nice and then I'm going to make the position the part though the Waypoint position but then I'm gonna add like two studs higher so it's not like in the ground or anything like that so yeah well shouldn't be in the ground but you know so still a little bit in sky and then I'm gonna anchor it obviously and then I'm gonna make it can collide off so that way our character doesn't get stuck with it and then I'm gonna do part dot parent finally is equal to game down workspace because I need to make sure you the part nowhere it knows where to go so yeah alright I'll go ahead run that script right there and there we go it should make a little path for us and look at that it looks so beautiful it makes the path it doesn't so fast and it doesn't in the shortest route possible like the thing is too like if I were to do this right now I would probably think okay I'm gonna go over here and then we're jump up here and then I'm gonna go over here and then I'm gonna go around like I want to kind of race it a real quick action let me try that okay I wanna try to race race my guy I'm gonna do weights too and then I'm gonna sorry I can spawn in I'm out of time it's morning all right I'm gonna go and do this and then here we go go go go go go oh no no no I don't know maybe he's gonna be be you get the boy really look he goes along the edges see I I go along look at middle because I like to be safe but do you see how it was along the edges it's kind of lying to be some reason but actually maybe it's not like I don't know but anyways BJ goes along the edges I normally go in the middle because like you know I just kind of think okay I'll go in the middle because you know it's safer but he goes along the straight edges because the fastest way like this is faster than doing along the middle right yeah I'm seeing so listen buddy you beat me alright you beat me this time but but not next time okay I can push you off stupid but you all right I would lead to basically I want to watch him fall room quick wait a minute bye-bye oh no don't know I was it oh my god okay I forgot thank you that anyways you don't I'm saying though this is pathfinding servers hope you guys enjoy this tutorial it's really cool I think so and you know there's one purpose right there you can race BOTS that's kind of cool honestly but yeah anyways guys this is the basic so if you wanna learn more if you want to learn more about pathfinding don't worry I will have tutorials on that in the future but for now um youyou can just look it up I guess it being worried for information on it but anyways make sure to subscribe for you news is that way you don't miss out on any more awesome stripping tutorials guys I'm serious I'm gonna have some sex criminals sick scripting his words coming out soon I have a lot of ideas and also join my discord server guys I have a discord server for scripting basically it related and it's awesome guys the community's been saying lately and I appreciate all of you links in description links in the bio or not laying it in my own baby like in the comments - maybe but yeah that's pretty cool anyways guys I'll see you guys very soon in the next one which should be every other day so yeah I feel like I'm forgetting something but I don't know um oh yes okay one more thing actually it's kind of useless but if I change the wait I make it's like nothing right and let me know go ahead and comment out my character moving but if I change the weight of this right to nothing or if I just delete that and actually delete this moving to right here it'll basically make the path for me like instantly look at this oh well that didn't work oh there it is Susie it made the pattern just and made the path from the starting point obviously and that's still moving because I put that down there I don't know why that was down there anyways that's still moving but okay but anyways you see what I'm saying that right like the path most instantly it's kind of cool and you can make it out it's like I guess you can put that in your game I guess if you wanted to but I'd put it on the client because it's probably a lot better but yeah so that's kind of cool I liked a lot and liked it a lot and it made it instantly you know I think it's so cool and actually I'm probably gonna wait point zero point like zero five I want to see how fast runs like that they recall and oh that didn't work it have an error oh there you go Francie okay sorry it's toriel's leaving you can you can go now if you'll I just okay but look at that look how cool that is okay I'm gonna stop rambling now all right I'll see you guys next one peace out
Info
Channel: TheDevKing
Views: 121,210
Rating: undefined out of 5
Keywords: TheDevKing, Roblox Scripting Tutorial, roblox, studio, coding, tutorial, how to script on roblox, roblox studio, pathfinding roblox, pathfinding roblox studio, roblox studio npc pathfinding, roblox studio ai pathfinding, npc pathfinding roblox, how to make a bot in roblox studio, how to make an AI in roblox studio, how to make a zombie that follows you in roblox, how to use pathfinding roblox, roblox pathfinding service, pathfinding service roblox, pathfinding tutorial roblox
Id: VWKNtqjPKn0
Channel Id: undefined
Length: 22min 22sec (1342 seconds)
Published: Tue Dec 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.