Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Here is a something you will definitely use alot: "if then" statements
- "if then" statements are script lines with conditions, if the conditions pass, it does X
- if not, it will either do nothing or do Y
- Here is a basic example of one:
- ]]
- if 3 > 2 then
- print("Complete!")
- end
- --[[
- As you can see, this is an example of an "if then" statement, What this does is check if 2 is
- higher than 3
- You will notice how the above uses a "greater than" symbol, These are a list of operators that
- an 'if then' statement will use:
- > | Greater than
- < | Lesser than
- == | Equal to
- ~= | not equal to
- >= | Greater than or is equal to
- <= | Lesser than or is equal to
- These operators are commonly used for integer or float values, But, You can use them for object values
- or other values as well (mainly == and ~=)
- There are also another thing called "elseif"
- This is a secondary conditonal if the first one fails
- Meaning that if the first conditional fails, it checks if the second conditional true, and then does
- something else
- Anyways, here is a somewhat more 'complex' example of an "if then" statement
- ]]
- if 125 > 500 then --Checks if 125 is higher than 500
- print("Success") --If yes, then print "Success"
- elseif 325 < 200 then --If the conditions above isnt met, then check if this conditions work
- print("Success 2") --If the above succeeds, then print "Success 2"
- else --If all the conditions above fail, do this instead
- print("Failure") --prints "Failure"
- end --This ends the statement
- --[[
- Lets translate the above into sudo-code (aka somewhat english)
- Translated:
- If 125 is higher than 500, then make a print with the value "Success"
- But if the above is false, then check if 325 is lower than 200, if yes, then
- make a print with the value "Success 2", If both of these conditions arent met,
- then make a print with the value of "Failure"
- ^^^^^Make sure you speak your code as sudo-code when your experiencing a bug or issue
- it helps alot
- ----------------------------
- These statements can have multiple conditionals as well
- Example:
- ]]
- if 5 > 2 and 5 < 10 then
- print("Success")
- end
- --[[
- By adding "and" to a conditional, it will add an another condition for the statement
- which makes the conditional require both conditions to be true for it to pass
- However, if you want the code to have two seperate conditions and only one of them to pass to get
- the same result, you can add an "or"
- ]]
- if 4 > 5 or 4 > 2 then
- print("Success")
- end
- --[[
- Adding an "or" will add a secondary condition, if the first conditon is a fail, then it checks
- if the second condition is met, if yes, then print "Success"
- And if you are wondering, You can have both of these in the same conditional like this:
- ]]
- if 3 < 8 and 3 > 2 or 5 > 2 then
- print("Success")
- end
- --And you can also have an another "if then" statement as well
- if 2 > 1 then
- print("Part 1")
- if 5 > 2 then
- print("Part 2")
- if 13 > 9 then
- print("Part 3")
- end
- end
- end
- --[[
- And assuming you have this question, this is how an "if then" statement would work:
- if (condition 1) or (condition 2) then
- Lets move into something more exciting, Variables.
- Variables can be added into "if then" statements
- So you can do stuff like this:
- ]]
- val1 = 12
- val2 = 9
- if val1 > val2 then --12 is larger than 9, so it passes
- val1 -= 5 --Reduces the value in val1 by 5, so now val1's new value is 7
- if val1 > val2 then --Checks if val1 is above val2, But since val1's new value is 5, it doesnt pass
- print("End")
- end
- end
- --[[
- now that should be "if then" statements completed i think.
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement