welcome back to my Roblox beginner scripting tutorial series my name is balev and in this episode we'll be discussing about if statements but we're also going to be talking about else if statements and also else statements since they all tie to the same concept so the first thing we're going to do is go on the right side and we're going to disable um any previous script we've made in the past couple episodes so the one that's currently active is my function script so I'm just going to disable this and I'm going to insert a new
script inside of the workspace by hitting the plus sign SE searching up a script and inserting a script inside of the workspace I'm going to select this right click and I'm going to rename it to if statements just like this hit enter and I'm also going to uh X Out These tabs for these previous scripts just to make things more organized and I'm also going to zoom in by holding control and moving uh inward just like this so we can see the text better and now we have our proper setup for uh what we're going
to be talking about for this episode okay now if statements it's a fundamental concept that we need to know uh when it comes to um adding logic to our scripts if we want to have a structure of executing specific codes if conditions are met so what I'm talking about here with conditions is we're going to start with a if keyword in all lowercase so it's going to look like this and when we're making a conditional statement um it's tied to a Boolean value uh to determine if a condition uh either returns true or either returns
false so an example I can give you is if we were to see if 2 + 2 is equal to 4 which obviously it is true but if you were to type this out so if you were to say 2 + 2 equals 4 uh just like this then we would have a true Boolean be returned with this conditional now you might see some errors here and you might be wondering what this is all about um we can't use a single equal sign if we trying to compare uh two numbers together so 2 + 2
that's 4 uh equals 4 um because this single equal sign is supposed to be an assignment operator so we're trying to uh essentially set whatever's on the left side uh to whatever's on the right side but if we're just doing a comparison then what we need to do is add another uh equal sign just like this to make it a comparison operator and not an assignment operator and so once we do this then what we're going to do on the right side is say then just like this so if 2 + 2 is equal to
4 then what we're going to do is hit enter and then roadblock is going to add an an end um keyword at the end here so basically if 2 plus 2 is equal to 4 then we can um run whatever's inside of this Branch so we're going to write a print statement uh saying that 2 + 2 does equal to 4 just like this so now if we go into the game and hit test and hit play then what we we should see in the output is that 2 + 2 does indeed equal to 4
now let me tell you what happens if this does not equal to 4 so if I hit stop and I go back into our script and then if I were to say if 2 + 3 is equal to 4 which in this case it's not true because 2 + 3 is equal to 5 if we go into the game and hit play then what we should see in the output is nothing being printed because 2 + 3 does not equal to 4 that's why it basically skipped this line so it's not going to print out
this statement right here so I'm going to hit stop again and now that's basically how we have our first Branch with if statements but there's more we can add on top of this and the next uh Branch I can show you is an else statement so basically what we're going to do is inside of our block of code we have over here we're going to drop two lines like this it could be one or two lines it doesn't really matter and I'm going to hit backspace so that we have proper indentation right here uh to
line up with the if um keyword so we're going to say l just like this and then we're going to hit enter so then what we're going to do is add another print statement like this saying the if statement failed um just like this so what's going to happen here is it's going to check if 2+ 3 is equal to 4 and if it's not true then it's going to go down to this else statement and it's going to print out whatever is inside of this block of code right here so if we go into
the game and hit play then what we should see is that the if statement failed because we tried checking if 2+ 3 is equal to 4 which we know is not true so that is something you can do with the else statement and once again some things to keep in mind is you need to have your else keyword line up with the if statement uh it can't be over here uh and it most certainly cannot be I don't know over here or something it has to line up with the if statement and you need to
have each branch um be indented like this uh for each part of the if statement so I'm going to hit stop and that is basically how we add an else statement to it but now I'm going to show you what an else if statement is so an else if statement is basically another if statement that gets checked if the first if statement fails so basically what it's going to look like is we're going to drop um two lines again here and I'm going to hit backspace so that we line up with the if and the
else um accurately so we're going to say else if just like this so this is one word else if uh we're going to say if 2 plus 2 equals to 4 and then we're going to hit enter and now what we can do here is say print open and close parentheses uh 2 + 2 does actually equal uh 2 4 here uh just to like differentiate it a little bit so um we first start with our if statement so 2 + 3 does not equal to 4 so it's going to skip this and it's going
to go down here to check if 2 + 2 is equal to 4 this is true so it's going to print this statement and it's going to skip the else because we know that this is true so if we go into the game and hit play then we should see in the output that 2 + 2 does actually equal to four here so that is our um if statement structure that we have going on here that I think you'll find to be um extremely extremely useful now we can add more elsif statements if we want
to here so if we drop a line and then go down here we can write another Els if here uh by saying if 1 + 1 = to 2 then we can print um 1 + 1 does equal to 2 just like this and we're also going to change this to something different so that um we can make sure that this one passes through correctly so if we hit play then what we should see is 1+ 1 does equal to two so if we hit stop and go back to our script we can see that
how this logic applies because we start here it doesn't work we we then go over here it doesn't work and then we go over here it does work so then it prints the message and it basically skips the else if nothing works the only time it would go into the else is if all of these statements did not work so you can kind of understand the structure going on here and it's definitely very important to understand if a certain uh criteria or condition is met then we then we're going to execute a specific part of
the code uh and not execute the other parts of the code if we don't need uh to have that now this is going to be useful for uh our functions that we've created in previous episodes so that's actually what we're going to be doing so I'm going to select everything here so I'm going to hit contr a and I'm going to hit backspace so now what we're going to do is create our addition function just like we did in previous episodes by saying local function addition uh open close parentheses and then we're going to pass
in two arguments so the first argument is going to be number one uh and then we're going to separate this with a comma and then we're going to have number two as our second um parameter so then we're going to hit enter and then what we're going to do is have um our if statement structure inside of this um addition function so that we can print stuff depending on what the result is from this so what we're going to do is write a result variable by saying local result equals number one + number two just
like this and then we're going to drop a uh drop two lines down here and then we're going to make an if statement so we're going to say if result equals four so once again make sure you're using two equal signs not one equal sign because we're comparing values together rather than assigning uh variables so we're going to say then we're going to make a print statement by saying um the result is equal to 4 and then we're going to make an else if statement by saying else if result uh equals to let's say six
then we're going to make a print statement by by saying the result is equal to six just like this and if both of these fail then we're just going to write an else statement once again make sure that the alignment is correct uh over here so we're going to say else and then we're going to say print um none of these results add up just like that and so now we're going to call this um addition function down here by saying addition uh open and close parentheses so the first number we're going to add is
2 comma 2 so it's going to fall under this branch and then we're going to make another addition function call right here and then we're going to put three and three just like this and then we're going to make another addition uh function call by saying uh four and two so both of these are going to fall under this one and then we're going to make an addition function call for I don't know some big number like 10 and 10 so now what will happen is if we go into the game and hit play then
what we should see in the output is uh the result is equal to four so it checked two and two which in this case it is true then it's going to check for 3 and three which uh does equate to six and then finally four and two which also equates to six so it printed this twice um and then obviously for 10 and 10 that equals to 20 which didn't fall into any of these conditions so it went into the else statement saying that none of these results added up so this is how we can
add our if statements to our functions and you can probably think of other ways you can can add this inside of your scripts now that we know that we can use conditions to um execute code when it meets certain requirements so I'm just going to hit stop and that is essentially the end of uh understanding if statements one final thing I want to mention here is um understanding what happens if we have two if statements so I'm actually going to um delete this else and else if statement over here so I'm just going to select
these two and then hit backspace and then backspace again so now let's see what happens if we have another if statement so if we say if result equals equals to four then uh we're going to make a print statement by saying um basically the same thing over here so I'm just going to copy uh what was in here so I'm going to hit contrl C and then I'm going to select this and then hit contrl V just like this and I'm also going to uh get rid of these three addition function calls right here just
to have this one so now if we go into the game hit play then what we should see in the output is the result is equal to four being printed out twice because if we go inside of our script then it's first going to check if the results is equal to four then it does print this but then it's going to go down here to check again if the results is equal to four then the result is equal to uh to four now obviously it doesn't have to be this condition specifically like it doesn't have
to be the same condition but it has to be um like two different if statements that meet the requirement of whatever this condition is to then execute both of these um code blocks so that is something important to know about when you're using IF statements is that if you have multiple if statements then it's going to check uh for each of these if statement blocks but if you have an if else if an else structure then it's only going to fall under one of those code blocks and I hope that makes sense to you so
that's basically going to be it for this episode for today's learning objective you basically have a lot of power with what you want to do with these if statements and also functions and these other sorts of things so I want you to definitely experiment with this uh before you move on to the next episode and see what you can come up with and once you do that then I want you to go down to the comment section and paste your code uh that you feel comfortable sharing to everybody who's watching these tutorial guides as well
and with that being said that's going to be it for this episode I will see you in the next one take care