Skip to content
1 / 5
Lesson 01 of 05

Conditional Statements

Make decisions in your code using if, elseif, and else.

Conditional statements direct program flow based on boolean expressions.

Structure:

  • if condition then ... end
  • if condition then ... else ... end
  • if condition then ... elseif other then ... else ... end
Always close with end!

Comparison operators:

  • == equal, ~= not equal
  • < less than, > greater than
  • <= less or equal, >= greater or equal

Logical operators:

  • and — both must be true
  • or — at least one must be true
  • not — inverts boolean

Nested conditionals work but become hard to read when too deep. Prefer guard clauses (early returns) or elseif chains for cleaner code.

Ternary-like pattern: condition and valueA or valueB Warning: if valueA is falsy, this always returns valueB regardless of condition. Use a proper if statement when valueA might be false or nil.

luau
1local health = 75
2local maxHealth = 100
3
4-- Basic if/elseif/else
5if health <= 0 then
6 print("Player died!")
7elseif health < 25 then
8 print("Critical health!")
9elseif health < 50 then
10 print("Low health")
11else
12 print("Healthy: " .. health)
13end
14
15-- Logical operators
16local hasArmor = true
17local isBlocking = false
18
19if hasArmor and not isBlocking then
20 print("Armor active, not blocking")
21end
22
23if health > 50 or hasArmor then
24 print("Survivable condition")
25end
26
27-- Guard clause pattern (clean code)
28local function takeDamage(player, amount)
29 if not player then return end -- guard
30 if amount <= 0 then return end -- guard
31 player.health = player.health - amount
32 print(player.name .. " took " .. amount .. " damage")
33end
READY · 33 lines