Kahibaro
Discord Login Register

2.2.1 If statements

Why If Statements Matter

If statements let your game make decisions. With them, your scripts can react differently when a condition is true or false. Without if statements, every script would always do the same thing, no matter what the player does.

In Roblox games, you use if statements when you want to check things like a player’s health, the number of coins they have, which button they clicked, or whether an object has been touched. The script then chooses what to do next based on the result of that check.

If statements are the foundation for more complex logic later, such as control of game flow, conditions in loops, and many gameplay systems. For now, focus on understanding how to write them and how they behave.

Basic If Statement Structure

An if statement in Lua checks a condition. If the condition is true, the code inside the block runs. If it is false, that code is skipped.

The simplest form in Lua is:

if condition then
    -- code that runs only if condition is true
end

The keyword if starts the condition, then marks the beginning of the block that should run when the condition is true, and end marks the end of the if statement.

Here is a concrete example:

local coins = 15
if coins >= 10 then
    print("You have enough coins!")
end

If coins is 10 or more, the message is printed. If coins is less than 10, nothing inside the if block runs.

Important rule: In Lua, every if block that you start with if or elseif must be closed with a matching end.

Conditions and Comparison Operators

The condition in an if statement must result in a boolean value, either true or false. Lua uses comparison operators to compare values.

Some common comparison operators are:

== means "equal to"
~= means "not equal to"
> means "greater than"
< means "less than"
>= means "greater than or equal to"
<= means "less than or equal to"

For example:

local health = 50
if health == 0 then
    print("Player is dead")
end
if health > 0 then
    print("Player is alive")
end
if health ~= 100 then
    print("Player is not at full health")
end

Each of these conditions will be checked separately. Only the code inside a condition that evaluates to true will run.

Important rule: Use == for comparison, not =. A single = assigns a value. Double == compares values.

Boolean Values and Logical Operators

Lua has two boolean values, true and false. If statements often involve these directly or combine several conditions with logical operators.

The main logical operators are:

and is true only if both conditions are true.
or is true if at least one condition is true.
not reverses a boolean value, turning true into false and false into true.

Here is an example that checks several things at once:

local health = 80
local hasShield = true
if health > 50 and hasShield == true then
    print("Player is very safe")
end
if health < 20 or hasShield == false then
    print("Player is in danger")
end
if not hasShield then
    print("Shield is missing")
end

You can think of and as multiplication of booleans, where true is 1 and false is 0. So true and true behaves like $1 \times 1 = 1$, which is true, but true and false behaves like $1 \times 0 = 0$, which is false. You can think of or as addition that is limited to 1, where true or true and true or false both count as 1, which is true.

If, Else, and Elseif

Often you want one thing to happen if a condition is true, and a different thing if it is false. Lua uses else for this situation.

Here is the pattern:

if condition then
    -- runs if condition is true
else
    -- runs if condition is false
end

For example:

local coins = 5
if coins >= 10 then
    print("You can buy the item")
else
    print("You need more coins")
end

Sometimes you need to check more than one condition in order. Lua provides elseif for that. It lets you add extra conditions after the first one.

Here is the structure:

if condition1 then
    -- runs if condition1 is true
elseif condition2 then
    -- runs if condition2 is true and condition1 was false
else
    -- runs if none of the previous conditions were true
end

Only one of the blocks runs. Lua checks them from top to bottom.

Here is a practical example:

local health = 40
if health <= 0 then
    print("Player is dead")
elseif health < 30 then
    print("Player is low on health")
elseif health < 70 then
    print("Player is hurt")
else
    print("Player is healthy")
end

In this example, only one message prints. Lua checks the conditions in order. As soon as it finds one that is true, it runs that block and skips the rest.

Important rule: In an if ... elseif ... else ... end chain, only the first true condition runs. Every block after that is skipped.

Nesting If Statements

Sometimes you want more specific checks inside a situation that is already limited by a previous condition. You can place an if statement inside another if statement. This is called nesting.

For example:

local coins = 20
local hasVIP = true
if coins >= 10 then
    print("You can buy the basic item")
    if hasVIP == true then
        print("You also get a discount")
    end
end

The inner if statement only runs if the outer condition is true. In this example, the script will never check for hasVIP unless the player has at least 10 coins.

Nesting lets you create more detailed logic step by step. Be careful not to make the nesting too deep, or your code becomes hard to read.

Truthiness and Common Pitfalls

Lua treats some values as true and some as false when used in conditions. This is called truthiness.

In Lua, only false and nil count as false in conditions. Everything else counts as true, including:

0
Empty strings ""
Empty tables {}

For example:

if 0 then
    print("This will run, because 0 is treated as true in Lua")
end
if "" then
    print("This will also run, because an empty string is true")
end

This can surprise beginners who come from other languages where 0 or empty values are treated as false.

Another common mistake is forgetting end or placing it in the wrong place. If you see errors related to expected 'end' or similar messages, carefully match each if, elseif, and else with a single end.

You might also accidentally write a single = where you need ==. This will either cause an error or change a variable value instead of checking it, which can break your game logic in subtle ways.

Using If Statements in Simple Game Logic

Even with very basic scripts, if statements let you start adding real gameplay decisions. Here is a small example similar to something you might use in a Roblox game.

Suppose you want to check whether a player has enough points to enter a special area:

local requiredPoints = 100
local playerPoints = 75
if playerPoints >= requiredPoints then
    print("You can enter the special area")
else
    print("You need more points to enter")
end

You can imagine replacing the print statements with real actions later, such as opening a door or teleporting the player. For now, understanding the decision structure is the important part.

As you move further, you will connect if statements to events and other control structures. The core idea will remain the same. The script checks a condition and then chooses a path based on whether the condition is true or false.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!