Beginners Scripting Guide (BETA)
  • Getting Started
  • Basic Data Types
  • Variables
  • Basic global functions
  • Number Arithmetic & Compounds Assignment
  • If statement
  • Function
  • Table & Dictionary
  • Loops
  • Creating An Object
  • Understanding Client & Server
  • RemoteEvent
  • Unfinished & In-progress
    • Methods
    • RemoteFunction
    • Basic libraries
    • Basic Services
    • Basic of Vector3 and CFrame
    • Basic built-in events
Powered by GitBook
On this page
  • CONDITION OPERATIONS
  • First example:
  • Second example:

If statement

✏ Beginners Scripting Guide ✏

In this section, we will discuss how if statement works. An if statement is a code of block that will be executed whenever the if statement's condition is met.

NOTE: The parentheses can be used to group multiple conditions and the "not" term will be used to flip the current value to the opposite of the current value.

CONDITION OPERATIONS

  • >= (Condition 1 greater and equal to condition 2)

  • <= (Condition 1 less and equal to condition 2)

  • ~= (Condition 1 is not condition 2)

  • == (Condition 1 is equal to condition 2)

First example:

if 1 > 0 then -- This will check if 1 > 0 or not.
    -- Code block in here will be executed when condition(s) are/is met  
    print('1 is greater than 0')
end

Second example:

if not 1 > 3 then -- This will check if 1 > 3.
    -- The statement is false but we use the "not" to flip that false to the opposite value which is true.
    print('1 is greater than 0')
end

and can be used to combine multiple statements together

if 3 > 1 and 3 > 2 then -- Code block here will be executed when both of the conditions are met.
    -- The engine will check if 3 > 1 and if it's true then the engine will move on to the next statement.
    -- Now the engine will check if 3 > 2 and if it's true then the engine will execute the code block.
    -- From this we can see that the engine will execute the code block if both of the statements are true.
    print('3 is greater than 1 and 3 is greater than 2') 
end

or evaluates to true if either one of the statement is true

if 3 > 1 or 3 > 5 then -- Code block here will be executed if one of the condition is met.
    -- The engine will check if 3 > 1 and if it's true or not, The engine will move on to the next statement which is 3 > 5.
    -- The engine will check 3 > 5 and if it's true or not.
    -- If one of the statements is true then the engine will execute the code block.
    print("3 > 1 or 3 > 5")
end

elseif can be used to extend the if statement.

local a = 2

if a > 1 then -- Trigger block code below if "a" is greater than 1.
    print('3 is greater than 1.')
elseif a < 5 then -- Trigger block code below if "a" is less than 5.
    print('3 is less than 5.')
end

else can be used to check if the condition is not true or opposite.

local a = 2

if a > 2 then
    print('A value is greater than 2') -- This will be print if "a" is greater to 2.
else
    print('A value is less or equal to 2') -- This will be print if "a" is less or equal to 2.
end
PreviousNumber Arithmetic & Compounds AssignmentNextFunction

Last updated 11 months ago