welcome back to my Roblox beginner scripting tutorial guide my name is balev and in this episode we'll be discussing about loops once again another very fundamental Concept in scripting but also programming in general that you will need to know not only for game development but also for whatever programming Endeavor you will have anything past that so as usual what we're going to do is go on the right side and we're going to disable our previous scripts that we created in the last episode we're going to disable it and then create a new script inside of the workspace just like this and and we're going to rename this to the name of the episode which is loops and then we're going to hit enter I'm going to delete this and first thing I would like to do is try and paint a scenario for you where we would want to repeatedly execute the same pieces of code over and over and over again if we were to let's say have three print statements by saying print let's say statement one and then we were to make another print statement down here so we're going to write another print statement with statement two and then we can do another print statement by saying statement three so we have these three print statements right here but if we were to copy this and then go down here and then paste this and then if we were to just do this over and over again so I'm just going to hit control V uh to basically just paste everything inside of here repeatedly over and over and over and over and over and over again then this is not a really great approach to print all these statements all at the same time like these three specific lines of code for each block this is not really the best way to go about this because as you can see this already took 43 lines for what is it like eight or nine different blocks of print statements that we're trying to print here um so that's why this isn't the most effective way to do it but there is an alternative that works a lot better than what we're doing right here and that is called using a loop so what I'm going to do is Select everything that we've created here so I'm going to hit control a and then I'm going to pit backspace so let me introduce to you what a loop is a loop is basically what allows us to uh go through a specific block of code for either a specific number of times or an indefinite number of times um until a certain condition is met so that we can stop this Loop and we can stop uh going through the same lines of code over and over again um let me show you what this will look like in a practical example uh we first need to understand the structure of what a loop looks like and the first thing we're going to do is write a for Loop now a for Loop is basically what allows us to uh loop through specific blocks of code over and over again uh until we hit a certain criteria and we know how many times we want to execute this um block of code so what we're going to do is write the four keyword just like this and we need to write a counter variable so this can be whatever name we want it to be for the counter variable we could just call this uh my counter just like this and then what we need to do is set this equal to um a initial value so this is going to taken three numbers the first one is going to be the starting value and this can be whatever starting value you want um I'm just going to say one for example and then we're going to separate this with a comma and then we need to put down an end value so when is this Loop going to stop I'm going to say five for this example and then I'm going to write one more uh comma here and then we're going to put down uh how much is is this counter going to increment by whenever we reach the bottom of the for Loop so this is going to be let's say one for for this case and then what we're going to do is instead of writing then we're going to actually write the keyword do just like this and then we're going to hit enter and then um what we're going to be putting down inside of here is our statements that we had in the previous um example that I just showed you so it was print uh statement 1 and then we just copied this pasted it down here here two times and then we had statement two and then statement three just like this so basically what we're doing is we're starting at whatever um this counter value starts at which is going to be one and then once we go down this Loop for the first iteration it's going to print statement one statement two and statement 3 and then it's going to go back up here uh to increment the value by whatever this incremental number is which is going to be two so now we are on the second iteration it's going to print one print two print three and it's going to go back up again and then increment this um counter variable to three go back down 1 2 3 and then it's going to increment to four 1 2 3 and then finally it's going to go to its fifth iteration 1 two 3 and then it's going to stop so if we go into the game and hit test and hit play then what we should see in the output is statement 1 2 3 uh 1 2 3 1 2 3 and one two3 okay so these statements printed out five times because we started at one and then we reached the last value of of five um and so once we've done that it basically printed these three statements here five times so the equivalence of this would be printing this out once twice three times four times then five times this Loop is the equivalent of doing this and it's a much better way of going about this and it's a very important fundamental concept for you to know about when we want to repeatedly run the same commands uh over and over and over again until a certain conditions met so that is how we use a for Loop but there's actually another loop that is also important to know about and this is called a while loop now a while loop is basically used if we don't know uh how many times exactly we want to run this Loop but we do know that a certain condition needs to be met so that we can stop this while loop so here's what I'm going to do I'm going to drop two lines and what I'm going to do is this structure for a while loop is different so what we need to do is first specify um our count varibles separately so we're going to create a variable uh using the local keyword so we're going to say local let's say my while counter just like this and we're going to set this equal to let's say one so basically the starting value right here and then what we're going to do is write down our while loop by saying while uh and then we have to write a condition here so we can say that this condition is my while counter is let's say less than or equal to 5 just like this and then what we're going to do is say do again at the end of this declaration so we going to hit enter and then what we're going to do is basically write down our print statements that we had in the previous example and then paste it down here now here is something you need to be extremely careful about and that is um incrementing this value every single time that we go through this iteration because as you saw with this with this previous example all we needed to to do was basically write down our print statements and then the for Loop would automatically increment the value by one um because we specified it here in the Declaration but down here if we were to print if we were to execute this while loop then my while counter Still Remains at one because we never updated this value once we reached the bottom so if we started at one and then we went down here to print statement 1 statement two and statement three it's still going to remain as one and it's never going to reach this criteria um so that it's going to result in an indefinite W Loop and that's going to be really bad because it's actually going to crash your game if you if you attempt to do this so I recommend not doing this it's something that people get tripped up about all the time so you just need to remember that if you're making a wall Loop you need to increment your value or decrement it um or decrement it by making it go backwards however you made your conditional to be um but what we need to do is once we finish our statements we're going to drop a line and then we're going to increment the value so we're going to say my while counter equals uh we're going to take the value of my while counter itself so my while counter + one so basically we're taking the the variable of my while counter and we're setting this equal to whatever the value of my while counter currently is which is one and we're going to add one to it so this is going to result in my while counter being equal to two instead of one and then it's going to go up until it reaches the condition where it's going to be equal to five and then that's when it knows that it can stop so what I'm going to do is go into the game hit play and then what we should see in the output well actually um okay so we still have the the previous for Loop so I'm actually going to delete what we had here very quickly so I'm going to delete this go into the game and hit play then what we should see in the output is statement one two and three being printed out five different times in the output and it basically is working as expected so that is how we write a for Loop inside of our script I'm going to quickly hit control Z so that we can have our for loop back inside of a script so that you can see it comparing the while statement now there's a couple things I want to um show like we can actually decrement our um our counter so if we let's say started at five and then we wanted the goal to be uh going back to one and we make the in inrent value by negative 1 then it'll actually go backwards but it's essentially going to do the same thing so if I were to just delete this while statement right here and go into the game and hit play then what we should see in the output is five different print statements again uh addressing this before Loop right over here where we print statement one statement two and statement three except this time it's going backwards so it's starting at five then it's going to go down here and then it's going to decrement it by -1 so then it's going to be four and it's going to go down down here three go down here two go down here and then one go down here and then finally it's going to stop the for Loop so you can make your Loops go backwards as well which is another important thing to know about and you can basically do the same thing with while Loops as well you just need to be careful about like what your starting value is what your ending value is and also what your um increment or decrement values are um what we can also do so I'm just going to hit uh control Z again to just go back and go with our previous Loop that we had before and and this time what we can do is actually have a bigger incremental value so let's say two and we were to let's say have a starting value of two and then have an N value of six so I'm just going to delete this while ruce statement again go down here hit play then what should happen is this is printing out three times so statement 1 2 and three statement 1 2 and 3 and then statement 1 2 and 3 because we start at two and then it's going to go down here and then it's going to increment by two so it's going to be at four and then go down here and then finally it's going to be at six go down here and then you basically get the idea at this point so this is how you can control your counter variables um when you're using for loops and this can also work with wall Loops as well so now I'm going to hit stop and that is basically that now another interesting thing we can do is we can have loops inside of our Loops so if we were to let's say go back to our previous example with uh starting at one ending at five and then incrementing by one uh we can have a loop inside of a loop so I'm going to make this more simple by basically just making one print statement um basically just saying I don't know a like as a simple example and then what I'm going to do is create a for Loop inside of this for Loop so I'm going to say for um my counter 2 equals 1 comma let's say five and then one again and then we're going to say do uh print B just like this now what's going to happen here is we're going to start at one up here so it's going to print a and then it's going to go down here where it's going to print B five times before this counter gets incremented by one to then print our second a and then it's going to print five more B's before it goes back up here so if I were to go into the game and hit play let me show you what this looks like in the output so it prints a one time and then it prints B five times and then it prints the second a and then it prints B five times and it's just going to keep going uh until we reach the end point um so this is another powerful uh way you can use Loops is by using what's called nested Loops having a loop inside of another loop so I definitely want you to be able to experiment with this um once you use Loops inside of your scripts and that is basically how we are using Loops inside of our uh scripts inside of Roblox studio now as for a final example what I'm going to do is let's say we want to use a for Loop to change the color of this base plate then what we can do is first go back to our script I'm actually going to delete everything that's inside of here and what we're going to do first is make reference to our base plate so we're going to say local base plate equals game. workpace dobas plate like this and then what we're going to do down here is make a for Loop by saying for uh my counter equals 1 comma let's do it 10 times and then we're going to have an incremental value of one and then finally do so what we're going to do here is basically change the basee Plate's color to uh a certain color so we're going to say base plate do um brick color equals and what we're going to do is pick out a color uh from the brick color scroll wheel so if we click on this then what we can do is identify which color we want to have and then we can basically just write down the the color itself inside of our script so let's say we want toothpaste um we need to match this this name exactly as it is so what we're going to do is Select out of this go back to our script and then we're going to type in um and then we're going to type in a brick color value so you don't have to understand what this means but just follow exactly what I do uh and then we should be able to change our brick color so we're going to say brick color. new open and close parenthesis and then we're going to put in um whatever color it is we wanted to change this to which is going to be toothpaste just like that and this is how we're going to be changing our color now what we can do down here is we're going to drop a line now here's the thing um when we're writing our for Loops uh or any Loops in general everything is going to be executed simultaneously without any weight to it so if we were to basically make multiple brick color changes then we won't be able to see these changes because it's happening so quickly so what we actually need to do is pause our script for a specific period of time before we execute the next line of code and this is what's called a weight statement that I'm going to show you what it looks like right here so I'm going to drop a line and what we're going to do is say task so this is from roblox's task library and we're going to say dot wait open close parentheses and then inside of here we're going to specify the number of seconds that we're going to wait before we reach the next line of code so in this case I'm going to say um let's say one so that we can see the changes so we wait 1 second uh before we change the brick color again so what I'm going to do is copy this line and I'm going to paste it down here and I'm going to change this color to something different so what I'm going to do is put in the uh double quotations and then we can pick whatever random color we want uh this one says really red so we could just auto complete it by hitting enter and then we can do this one more time by saying task.
wait for about 1 second and then paste in our line here and then specify whatever color we want by putting in the double quotations and then let's say we want to add um gold so I'm going to select that hit enter and now we have our script but one final thing we have to do is add one more task. we here so that we can see the changes from this brick color change and then this brick color change because once we finish the script we're going to immediately go back up and then we're going to change the brick color one more time um for the next iteration so now if we go into the game and hit play then what we should see inside of our base plate is that our brick color or our base Plate's color is changing every single second three times uh for a total of 30 times because it's going to um change the brick color three times per iteration and we're doing it 10 times in total so once this brick color changes 30 times that's when this Loop finally stops and then we now move on to the rest of the script so that's essentially the basics of using Loops inside of your Roblox games and I hope you learned a lot from this episode for today's learning objective what I want you to do is basically go through and make um more for Loops more wall Loops you can even try making nested Loops as well to change um the base Plate's brick color and now that I've showed you how to use task.