welcome back to my Roblox beginner scripting tutorial series my name is balev and in this episode we'll be discussing about find first child and wait for child which are two functions that Roblox has provided to us that's going to be very useful in locating objects within our game and it's also useful to help us understand the structure of how a game is structured so to recap a game is basically comprised of all of these different folders that have its own specific use cases like let's say this workspace it shows all of the parts that we can see within this 3D world that basically make up the workspace of the game now when we're understanding this hierarchy like if we open up this workspace this workspace has all of these different objects that are within this workspace folder the workspace in this case is called a parent and everything that's inside of this workspace is a child that belongs to the workspace parent and we can continue going down this list by finding more objects that have stuff contained within them like this base plate so this base plate has a texture child and this base plate is the parent of this texture child and this base plate is the child of this workspace parent and so you can kind of understand the structure from here on out with how the parent and child structure works inside of the Explorer so with that understanding now let me show you some things that can help us organize these parts before we start using these functions so there's two ways we can organize stuff within our game uh one of them is being models and the second one is being folders and I'm going to show you what both of them do so if we go inside of the Explorer and we click on the plus sign next to workspace and we insert a model like this so I'm going to click on model uh it's going to add a model inside of the workspace now basically what a model does is it groups Parts together to make up one big part as a whole that's basically a model so if we go into the model Tab and we click on parts so we part here I'm going to go to the right side and I'm going to rename this part to part one and what I'm going to do is drag this into the model that we just created and now I'm going to duplicate this part so I'm going to hit control D and I'm going to select the move tool like so and I'm going to move it this way and I'm going to rename this part to part two and I'm going to duplicate it one more time and I'm going to move it this way and call this one part three now since all of these are contained within a model if we hover over one of these parts then it's going to select all three of the parts because it's all contained inside of one model so if I select one of these parts then it's going to select the model rather than the individual part itself and we can drag this model around and it's going to drag all three parts along with it because it's all contained inside of one model now let me show you what a folder does so if we click on the plus sign and insert a folder like this and let's say we take all three of these parts I'm going to select the first part I'm going to hit control while selecting the other two parts like this and I'm going to drag it inside of the folder So within this folder we can still select the folder and drag it around like so but if we go into the game and we select one of these parts then it's going to individually select one of these parts or at least when we hover over it to then move it around if we want to do so just like this so that's like a slight difference between a folder and a model but generally what I would say is you should use a model if you want to group Parts together to make up one big part but a folder can be more generalized and can be used to organize Parts scripts and other sorts of assets you can use inside of your game so that's like a short introduction to models and folders but why is this important well this is going to be important to understand the parent and child relationship when we're using these two functions inside of our game so what I'm going to do is Select these parts again and I'm going to move it back into our model and I'm going to delete this folder so the first function I'm going to show you is find first child and what we're actually going to do is insert a script not inside of the workspace but let's insert a script inside of the model that we just created so I'm going to hit the plus sign next to model and I'm going to insert a script okay and I'm going to delete this code that's inside of here and now what we're going to do is first locate our model but what's interesting about this is because we have our script inside of the model we actually don't need to locate the model from the game data model and then into the workspace and then into the model itself so what we would have done is we would have said uh local and then the name of the model which we can just call model like this we're going to set this equal to game. workpace do model we don't need to do it like this but instead we can do it from the script itself so if we were to actually delete this we start at where the script is located so in all lower case we're just going to say script which basically tells us that we are currently located inside of the script right now and we want to get the parent of this script which is going to be the model so we can just say script. parent and this is going to locate the model rather than doing game. workpace dood just like this both of these basically do the exact same thing because we put the script inside of the model as a child so now that we've come to that understanding let's locate let's say the first part here so what we're going to do is drop a line and then we're going to say local part 1 equals model do part one so this is how we've been able to locate parts or other objects within the game it's basically by using a DOT and then whatever the name of the object is but now this is where the first function comes in which is find first child and let me show you what this looks like so instead of saying dot part one we're actually going to delete this and we're going to replace this with colon find First Child open and close parentheses and inside of these parentheses we're going to put the name of the part that we're trying to to look for so in quotations we're going to say part one now why are we doing it like this instead of doing Dot part one like we've been doing in the past well let's say in this case we have a script that's trying to look for a part that's contained within this model let's say this part is not here so if we were to just delete this part and it's just gone from the model then the script is going to try to look for a part called part one that's in the model but part one is not here so the script is is going to throw in an error saying that there is no such thing called part one if we were to just locate part one inside of the model but what this function does with find first child is it's going to check the model for a part one and if there's no part one then it's just going to tell us that and we can handle the error if that ever comes to be the case so what I'm going to do is hit control Z as in zero so we can bring the part back and I'm going to go back to the script and what we're going to do down here is check if part one actually does exist so we're going to say if part one then we're going to let's say do something with this part like change to the brick color of it so we're just going to say part 1. brick color equals brick color. new open and close parenthesis and inside of these quotations we can pick a random color here I'm just going to say um Coco and then I'm going to hit enter like that so if we go into the game and hit test and hit play then what we should see inside of the game is part one has changed its color because the script was able to find part one within the model now once again this is just a safety precaution to let us know that if the script decides to not find the first part then it's just going to skip this if statement and it's just going to continue with the script as is without throwing an error inside of the output I hope that part has made sense to you so if we hit stop and we basically delete the first part and if we go into the game now then it's not going to throw in an error but it's also not going to change the part of part one because obviously there's no part one inside of the model so it's a safety precaution to be able to look for something if it doesn't exist because there's a chance that it won't exist and that is the purpose of fine first child and I hope that this has made sense to you so now what I'm going to do is hit stop and I'm going to hit control Z again to bring the part back and now let me introduce to you the second function that's going to be useful and that's called wait for child now basically what this does is it lets Roblox wait to look for a specific object within a game for a specific period of time and if it doesn't look for it then it's just going to skip it and it's going to continue the rest of the code if it did not look for it and we can handle the script accordingly to whether Roblox was able to find it or not so what this is going to look like is let's say we drop two lines and we were to look for part two so we're going to say local part two equals model colon weight for child just like this open and close parenthesis and we're going to specify part two inside of here now basically what's going to happen is Roblox is going to run the script and it's going to look for If part two exists within this model which in this case it actually does exist Once We join into the game so once we do that then we're just going to let's say make a print statement saying part two has been detected just like this so if we go into the game and hit play then what we should see in the output is immediately says part two has been detected because it's contained within the model that we created but let's hit stop and let me show you what happens if we delete the part and then bring it in after a certain period of time so what I'm going to do is take this part and I'm going to right click and hit cut so that it's contained within our clipboard and what I'm actually going to do is go to test and click this dropdown and hit on run so that all we're going to see is our camera so once we see this then it's going to say in the output after a certain period of time that it says that there's a warning that says infinite yield possible on waiting for part two because Roblox took a while to try and see if part two existed but in this case it actually didn't so if we go into the workspace and we go into the model so I'm going to rightclick the model and I'm going to hit paste into just like this and now it's going to say that part two has been detected because we used the wait for child function to see whether the part was actually going to be detected and since it was added to this model that's when we decided to continue executing the script because of it and it's very useful for situations like this if we're trying to wait for something to be added into a model or into the game itself so that is basically how we use wait for child and also how we use fine for child inside of our game if something in this video didn't make sense to you then I encourage you to rewatch some of the bits before we move on to the final part of this video which is going to be a practical example of using fine for child so now what I'm going to do is hit stop and I'm also going to hit control Z to bring part two back so for this last part of the video we're going to be making what's called a kill brick and basically what this does is if let's say our player was in the game and we decided to step on this part the part would kill us and it would basically reset our character um kind of like if we were to play an obstacle course and if we touch a part that we shouldn't be touching then it basically kills our character and that's what's called a kill brick and we can use what we learned in the last episode with with the touched event and also combining it with fine first child to make a kill Breck so that's what I'm going to be showing you how to do for the rest of this video so what I'm going to do is go to the model and we're going to insert a part like this and we can change the brick color of this to Red so that we can uh say that this is a kill brick and we're going to go to the right side and we're going to rename this part to kill brick and then we're going to hit enter so what I'm going to do is insert a script inside of the kill briak so I'm going to hit the plus sign and I'm going to insert a script just like this I'm going to delete this code and we're first going to make reference to the kill brick which once again we can do from the script that's inside of this kill brick so we can say local kill brick equals script so we're located inside of the script and then we're going to say dot parent just like this and now we're going to use a touched event for this kill brick so we're going to say kill brick. touched colon connect function open close parenthesis and then we're going to specify the other part that's inside of here and then we're going to hit enter just like this so what we need to do in order to detect if this is a player that's touching this kill briak is we need to find what's called a humanoid so I'm just going to demonstrate to you what this humanoid is really quickly by going to test and we're going to hit the drop down and then we're going to hit play so if we go into the game and go to the workspace and go to our character which is our brde character or whatever character your name is inside of the game we're going to open this up and we're going to see that there is an object here called humanoid and this humanoid is basically used to tell us that this is a player that's stepping on this part rather than just another random part stepping on this so we're going to be detecting this using fine first child so what we're going to do is hit stop and we're going to go back to our script that we created so we're going to locate the humanoid of this part so we're going to say local other part equals oh and there's one more thing I forgot to mention too is that so if we're we're inside of the game and we're stepping on this part let's say most likely we're using our right foot or our left foot to be stepping on this part and as you can see with the part that's touching this kill brick part we can locate the humanoid that is basically contained within our brawl battle um character so the way we can do this is we can actually go to our script if we hit stop and we go to our script like this we can find the humanoid using the other part uh by saying local humanoid equals other part. parent colon find First Child open close parenthesis and we're going to say humanoid just like this so basically when we have our other part which is going to be like our right foot or left foot we're going to get the parent of that which is going to be our character data model and then from there we're going to find um a humanoid inside of that player data model so then once we make this check we're going to say if humanoid then we're going to set the health of the humanoid equal to zero to essentially kill the player so this is a property of humanoid which we're basically just going to say humanoid do Health equals z so health is a property of humanoid for the character and if we set it equal to zero then that's going to kill the player so now if we go into the game and hit play so our character should be inside of the game and if we touch this part then it's going to kill our player like so so that is basically how we make a kill brick and we can also make sure that a character is touching this kill brick and not any other random part inside of the game so that is basically how we use fine first child and wait for child inside of our game and I encourage you to experiment with fine for child and wait for child for any other parts or any other functionality that you may think of for this challenge so for today's learning objective I want you to continue experimenting with that and once you do do that I want you to go down to the comment section and paste in your code for other people to to see that you are comfortable sharing and that is basically going to be it for this episode I hope you enjoyed it and I will see you in the next one take care