### Lesson 4: "If" Statements

An 'if' statement can control whether something happens based on certain conditions that you specify.

Here is the format of an 'if' statement:

if (YourExpression) then
-- actions here
end

Be sure to capitalize correctly. In the script editor, the words 'if', 'then', and 'end' should turn blue.

'Your Expression' is the condition of the if statement. It can be in parenthesis, () but that is not always necessary. Here are some of the operators you can use in your 'if' statements:

The double equals sign (==) checks to see if the value on the left equals the value on the right. (2 == 1 + 1) Be sure not to confuse this with the single equals sign (=), which sets the value on the left to the one on the right.

The less than sign (<) checks to see if the statement on the left is less than the statement on the right. (2*2 < 20)

The greater than sign (>) checks to see if the statement on the left is greater than the statement on the right. (10*3 > 20)

The not equal sign (~=) checks to see if the value on the left does not equal the value on the right. (2 ~= 3)

The 'and' keyword checks to see if both equations are true. (2*2 < 20 and 10*3 > 20)

The 'or' keyword checks to see if either equation is true. (2*2 < 20 or 10*3 == 5)

'+' adds, '-' subtracts, '*' multiplies, and '/' divides.

You can also use variables and built-in variables in your conditions. For example:

if (game.Workspace.Part.Transparency == 0.5) then -- If a part's transparency is set at 50%
-- actions here
end

Updated on Jan 28, 2012 by (Version 8)