← Back to course
easy ✦ 75 Studs

Combining Conditions

Build trickier rules by chaining and, or, and not together.

You already know how to ask one yes-or-no question with if. Now you get to ask two or three at once. Real game rules are almost never about a single condition. “Can the player open this door?” really means “do they have the key AND is the door actually locked?” That is two questions joined into one rule.

Three little words do all the joining: and, or, and not.

  • and is strict: the whole thing is true only when both sides are true.
  • or is generous: the whole thing is true when at least one side is true.
  • not is a flipper: it turns true into false and false into true.
local hasKey = true
local doorLocked = true

if hasKey and doorLocked then
  print("Door unlocked")
else
  print("Stay out")
end

Read that if line out loud like a sentence: “if has key and door locked.” Because both hasKey and doorLocked are true, the whole condition is true, so you get Door unlocked. Flip either one to false and and fails the test, so you fall to the else and print Stay out.

Here is the why behind each word. Use and when every condition has to pass, like “logged in AND has enough Studs.” Use or when any one passing is enough, like “is admin OR is the owner.” Use not to read cleaner, so if not isReady then says “if it is NOT ready” instead of forcing you to write == false. You can even mix them, and parentheses keep the grouping clear: if (hasKey or isAdmin) and not isBanned then.

Now make it yours: print the exact Door unlocked line for this lesson, then invent your own two-part rule. Maybe a chest only opens if the player hasKey and not isTrapped, or a boss only spawns if playerCount >= 2 or testingMode. Say your rule out loud first, then write the if to match.

Output
Click Run to see your output.