START HERE·LUA FROM ZERO·Verified June 2026 · Lua 5.4 · ox_lib 3.x
Learning with an AI assistant?
Copies this whole lesson - every step, code block, and the exact console errors - plus 2026 ground rules (no lua54 'yes', Cfx.re Portal, correct callback signatures) as a ready-to-paste mentor prompt.
Start Here · Lua from zero

if and else: making decisions

Up to now your code has run straight down the page, one line after another, every time. An if statement breaks that. It lets your code look at something, ask a yes-or-no question, and run a block only when the answer is yes. That single idea is how a program reacts: a door opens only if the player has a key, a message shows only if the level is high enough. By the end of this lesson you will write code that decides for itself.

You'll learn
How an if statement runs code only when a condition is true, the comparison operators, else and elseif, and the Lua truthiness rule that surprises every beginner.
Time
~10 minutes
Difficulty
Beginner. No setup, all in the browser.
You need
Nothing to install. You write and run plain Lua right here in the page.
BEFORE YOU START

What an if statement is

An if statement is a fork in the road. You give it a question. If the answer is yes, it runs the code you put inside it. If the answer is no, it skips that code and moves on.

Here is the smallest shape:

lua
if condition then
  -- this code runs only when the condition is true
end

Three words do the work. if starts the question. then marks where the question ends and the code-to-run begins. end marks where that code stops. Everything between then and end is the block: the code that only runs when the answer is yes.

The question itself is the condition. A condition is just an expression that Lua boils down to a single yes-or-no answer. That answer has a name: a boolean. A boolean is a value that is only ever one of two things, true or false. When the condition lands on true, the block runs. When it lands on false, the block is skipped.

How do you get a true or false out of nowhere? You compare two things. level >= 5 is a comparison. Lua reads it as "is level greater than or equal to 5?" and replaces the whole thing with true or false. The >= symbol is a comparison operator: a symbol that compares two values and hands back a boolean.

Vocabulary

if
The keyword that starts a decision. It runs the block that follows only when its condition is true.
condition
The question an if asks. An expression that Lua reduces to a single true or false.
boolean
A value that is only ever true or false. The answer every condition produces.
comparison operator
A symbol like == or >= that compares two values and returns a boolean.

The comparison operators

A condition is usually built from a comparison operator. Lua has six of them. Each one takes a value on the left and a value on the right and gives you back true or false.

lua
a == b   -- true if a is equal to b
a ~= b   -- true if a is NOT equal to b
a > b    -- true if a is greater than b
a < b    -- true if a is less than b
a >= b   -- true if a is greater than or equal to b
a <= b   -- true if a is less than or equal to b

One of these is different from most other languages. To check "not equal", Lua uses ~=, not !=. If you type != you will get an error. The tilde ~ is the "not" symbol in Lua, so ~= reads as "not equal". Burn that in now and you will skip an error every other beginner hits.

See it run

You do not need a server for any of this. The box below is a real Lua runner inside your browser. Read the code, then press RUN. The variable level is set to 7, and the condition asks whether 7 is at least 5. It is, so the first block runs.

sandbox.luaLUA 5.4
OUTPUT
Press RUN to execute.

Now change local level = 7 to local level = 3 and press RUN again. The condition 3 >= 5 is false, so Lua skips the first block and runs the second one instead, printing Level too low. You just watched one variable change the path your code takes. That is the whole point of an if.

else and elseif

else: the fallback

You already met else in the sandbox above. An if on its own only does something when the answer is yes. Add an else and you also handle the no case. The else block runs when, and only when, the condition was false.

lua
if level >= 5 then
  print("You can enter")
else
  print("Level too low")
end

Read it in plain words: if the level is high enough, print the welcome, otherwise print the rejection. Exactly one of the two blocks runs every time, never both, never neither.

elseif: chaining several checks

What if there are more than two outcomes? You could nest if statements, but Lua gives you a cleaner tool: elseif. It lets you line up several questions, and Lua tries them top to bottom, stopping at the first one that is true.

Run this and watch which line prints:

sandbox.luaLUA 5.4
OUTPUT
Press RUN to execute.

Lua checks score >= 90 first. 82 is not at least 90, so it moves on. Next it checks score >= 80. 82 is at least 80, so that block runs and prints Grade: B. The moment one branch matches, Lua runs it and skips every branch below, including the final else. The else at the bottom is the catch-all: it runs only when none of the questions above were true.

Order matters in an elseif chain. Lua stops at the first true branch, so put your tightest condition first. If you wrote score >= 70 before score >= 90, a score of 95 would match the 70 line first and wrongly print a C. Always go from most specific to least specific, top to bottom.

and, or, not, and the nil/false trap

Sometimes one comparison is not enough. You want to run a block only when two things are both true, or when either of them is true. Lua gives you three words for that: and, or, and not. Other languages write these as &&, ||, and !. Lua spells them out as real words.

lua
local level = 7
local hasKey = true
 
if level >= 5 and hasKey then
  print("Door opens")   -- both must be true
end
 
if level >= 5 or hasKey then
  print("At least one is true")
end
 
if not hasKey then
  print("No key")       -- runs only when hasKey is false
end

and is true only when both sides are true. or is true when at least one side is true. not flips a boolean: not true becomes false, and not false becomes true.

Now the trap that catches every beginner. To decide whether to run a block, Lua sorts every value into "truthy" or "falsy". The surprise is how short the falsy list is. In Lua, only false and nil are falsy. Everything else is truthy. That includes the number 0. It includes the empty string "". Both of those count as truthy in Lua.

Common beginner mistakes

SymptomFix
'=' expected near '==' or assigning with = inside an ifUse == to compare and = only to assign. Write if x == 5 then, not if x = 5 then. One equals stores a value; two equals asks a question.
'end' expectedEvery if needs a matching end. Count your ifs and your ends and pair them up. Each if, even with elseif and else, closes with exactly one end.
unexpected symbol near '!'Lua does not use != for not-equal. Use ~= instead. The tilde is Lua's not symbol.
In Lua, is the number 0 truthy or falsy?

It is truthy. Lua treats every value as truthy except two: false and nil. The number 0 and the empty string "" both pass an if test. This trips up people coming from languages where 0 is false. When you actually care that a number is zero, write the comparison out: if count == 0 then.

Try it yourself

What you can do now

  • Write an if statement that runs a block only when its condition is true.
  • Use the six comparison operators, and remember Lua spells not-equal as ~= , never !=.
  • Tell == (a question) apart from = (storing a value).
  • Handle the no case with else and chain several checks with elseif, tightest condition first.
  • Combine conditions with and, or, and not.
  • Recall the Lua truthiness rule: only false and nil are falsy, so 0 and "" both count as true.

You can now make your code choose between paths. The next step is making it repeat work without copying lines a hundred times. That is the job of loops, and it is the next lesson, "Loops: doing things many times".