If, then, else

From GMod Wiki

Jump to: navigation, search

This section of the tutorial will show you how to make your code react differently based on conditions of your choosing.

If statements

The first thing I want to teach you is the "if" statement. This statement is a very basic, direct order to lua. This can be really useful in creating your own scripts. "If" tells the code to do something if a condition is true or not. This is a simple if statement:

local age = 18
if age == 20 then
    Msg("You're 20!")
end

The if statement can be a little confusing.

The local age = 18 part of this script sets a "Local" variable named age as 18. Local just means that the variable will only exist within the control structure. In this case the if statement.

The above example is saying that; "if age is equal to 20 then say 'You're 20!' and stop."

The if statement looks like this to lua:

if true then
    -- Do something
end

Condition has to be true to run the code block after it. A code block is code that only runs under certain conditions. In this case, the code block runs if the condition is true.

Else Statements

Else is used to define the opposite of the condition, so if a condition is true, then else would tell it to execute the following code, a good usage of else is shown below.

local age = 40
if age == 20 then
    Msg("You're 20!\n")
else 
    Msg("You're NOT 20!\n")
end

The above example checks if the variable "age" is 20, if this is true, then it prints to console "You're 20!", but if the variable "age" is NOT 20, then it prints "You're NOT 20!" to console.

Conditions Available For Use

Equal To

The equal to condition is true if the thing on the left and right of == are equal.

local age = 1
if age == 20 then
    Msg("You are 20!\n")
end

Not Equal To

The "not equal to" condition is true if the thing on the left and right of ~= aren't equal. You can also use != instead of ~=

local age = 15
if age ~= 20 then
    Msg("You are NOT 20!\n")
end

The code below is the same as the code above.

local age = 15
if age != 20 then
    Msg("You are NOT 20!\n")
end
Notepad-48.png Exclamation mark means not in both the C Programming Language, and Lua


Less Than

The "less than" condition is true if the thing on the left is less than the thing on the right.

local age = 15
if age < 20 then
    Msg("You are under 20!\n")
end

Greater Than

The "greater than" condition is true if the thing on the left is more than the thing on the right.

local age = 15
if age > 20 then
    Msg("You are over 20!\n")
end

Less Than or Equal To

The "less than or equal" condition is true if the thing on the left is less or equal to the thing on the right.

local age = 15
if age <= 20 then
    Msg("You are 20 or below!\n")
end

Greater Than or Equal To

The "greater than or equal" condition is true if the thing on the left is more or equal to the thing on the right.

local age = 15
if age >= 20 then
    Msg("You are 20 or over!\n")
end

This is a lot for first time Lua scripters, however, if you practice enough with using Conditionals, you will get the hang of it eventually.


Else If Statement

Last, I want to show you how to do else if. Else if is used to say, basically, "If it's not that, then if..." in lua. This is more precise than using else, but else if is NOT effective all the time

local age = 15
if age < 15 then
    Msg("Sorry, you need to be at least 15.\n")
elseif age > 18 then
    Msg("Sorry, nobody over 18 is allowed in here.\n")
else
    Msg("Welcome to High School!")
end

What this script is doing is trying to figure out if you can go to high school. If you're not under 15, and you're not over 18, then you can come in.

Give it a try yourself. Try writing a simple if statement in notepad. If you need help just look at the examples in here. Start with the simple ones and work up.

Yarin Kaul Icon ArrowSquare32 left.png Variables

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Functions Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox