If statements

In this tutorial you will learn how to use If statements! (This is my first tutorial)

by MaTh071512

Author Avatar

First, we have to know what booleans are booleans are basically "true" or "false" for example you know when you anchor a part, it has a little checkmark? thats a boolean. now how can we use them?

IF STATEMENTS allow you to use the booleans you just learned about, for example:

if 1==2 then
	print("1 is apparently equal to 2")
end

in our case, when ran it wont print anything because obviously 1 isnt equal to 2 also, always use == instead of = when comparing numbers, if you use = it will think you are declaring a variable. however, we can still make the if statement do something

if 1==2 then
	print("1 is apparently equal to 2")
else
	print("1 isnt equal to 2")
end

with this, it will now print "1 isnt equal to 2" and that is because when using an else, it will run if all the questions above failed. now introducing "elseif" elseif is used to shorten the amount of things you have to write, basically if a question above it failed, it will go down and run the elseif

if 1==2 then
	print("1 is apparently equal to 2")
elseif 1==1 then
	print("1 is equal to 1")
else
	print("1 isnt equal to 2")
end

now, it will print "1 is equal to 1" because it went down, determined it is true and ran. but comparing numbers is boring, isnt it? lets do it with properties of parts!

if workspace.Part.Anchored == true then
	print("the part is anchored")
else
	print("the part isnt anchored")
end

now depending on whether the part is anchored or not, it will print "the part is anchored" or "the part isnt anchored" now lets move on to something cooler "and" "not" "or" using "and" in an if statement will ONLY run if the condintions before and after it were true, for example

if 3==3 and 2==2 then
	print("true")
end

if you run it, nothing special happens, but if you change the 2==2 to 2==1 then it wont run, because 1 condition isnt satisfied and therefore it wont run now, "or"

if 2==1 or 4==4 then
	print("true")
end

in this case, it will run because when using an or, it kind of constructs an entire new check, and even if one is satisfied it will run now, "not" the not allows you to reverse what it checks for, without it it checks for true, but with it checks for false

if not 2==3 then
	print("true")
end

here it will still run because it is checking for false.

and this concludes how to use if statements, have fun!

View in-game to comment, award, and more!