Lesson 3 of 10

Conditionals

Goal

Only nil and false count as false

01

Explanation

if cond then ... end runs what's inside it when the condition is true, elseif adds another condition, and else covers everything left over. A single end closes the whole chain. The branches are checked in order, and only the first one whose condition is true runs. You combine conditions with and, or and not, and you write "not equal" as ~=, not !=.

The biggest difference between Lua and other languages is that only two values count as false in a condition: nil and false. 0 and the empty string "" both count as true. So if gold then passes even when gold is zero, and not gold doesn't mean "no gold". That's why you compare explicitly: gold == 0 and guild ~= "".

and and or return one of their operands as is, not true or false, which is why the common cond and a or b pattern works as a stand-in for the ternary operator. But the pattern breaks when a itself is false or nil: safe and false or true always gives true, because true and false gives false, so or moves on to true.

02

Your task

Use an if, elseif and else chain to set status to "Dead" if hp is zero or less, "Critical" if it's below 30, and "Healthy" otherwise. Make guildLabel equal to guild, or "No guild" if it's empty. Set broke to true if gold is zero. Finally, set canAttack to false when inSafeZone says the player is in the safe zone, and to true otherwise.

Checks

5 checks
  1. status is "Dead" when hp is zero
  2. status is set with an if and elseif chain
  3. guildLabel is "No guild" because the empty string counts as true
  4. broke is true because zero doesn't count as false
  5. canAttack is false in the safe zone
main.lua
OutputLua 5.4 (wasmoon)

Loading Lua.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.