Lua has no let, no const, and no type names to write. There is exactly one keyword you need, and it is local.
local name = "Ada"local age = 17local onDuty = truelocal assignment = nil
print(name, age, onDuty, assignment)Ada 17 true nilThe value decides the type, and you can ask what a value is with type().
print(type("SKYNEST"))print(type(17))print(type(true))print(type(nil))print(type(print))stringnumberbooleannilfunctionNotice the last one. A function is a value, the same as a number is. That fact does almost nothing for you today and becomes the most useful thing you know by Activity 1.8.
Nothing pins a variable to the type it started with, either.
local reading = 42print(reading, type(reading))
reading = "offline"print(reading, type(reading))42 numberoffline stringlocal is not optionalHere is the part of Lua most worth getting right on day one. If you leave off local, the variable is global — visible to every line of every file in your program, for the rest of its run.
A do ... end block is a chunk of code with its own scope. Watch what leaks out of it and what does not.
do shieldLevel = 100end
print(shieldLevel)100do local shieldLevel = 100end
print(shieldLevel)nilGlobals are slower, they collide with each other, and they make a bug in one file able to break a different one. Lua will never warn you. Write local every time until leaving it off feels wrong.
nil is Lua's word for nothing here. It is a real value with its own type, and it is what you get from any variable that was never given anything.
Which produces the single most common Lua bug there is:
local playerName = "Ada"
print(playerNmae)nilThat typo is not an error. playerNmae is simply a global nobody ever assigned, so it is nil, and Lua prints it without complaint. Your program runs, it is just wrong.
When something in Lua is mysteriously nil, check your spelling before you check your logic. It is the typo far more often than it is the reasoning.
Lua lets you assign a whole row of variables on one line, and the right-hand side is worked out before anything is stored. That gives you a swap with no temporary variable.
local a, b = 1, 2print(a, b)
a, b = b, aprint(a, b)1 22 1Open your main.lua and replace what is in it. You are turning last activity's hard-coded readout into one driven by variables.
local variables: a name, a crew count, and whether the ship is online.-- main.lua - Skynest status board
local shipName = "Skynest"local crew = 4local online = true
print(shipName, crew, online)Skynest 4 truenil. The ship has no orders yet, and that is information worth storing.type() of all four.-- main.lua - Skynest status board
local shipName = "Skynest"local crew = 4local online = truelocal nextPort = nil
print(shipName, crew, online, nextPort)print(type(shipName), type(crew), type(online), type(nextPort))shipNmae — the misspelling is the point.-- main.lua - Skynest status board
local shipName = "Skynest"local crew = 4local online = truelocal nextPort = nil
print(shipName, crew, online, nextPort)print(type(shipName), type(crew), type(online), type(nextPort))
print(shipNmae)local from one declaration. Confirm that the program still runs identically — then explain to yourself, in a comment, why that is a bad thing rather than a convenience.Your finished file should produce this.
Skynest 4 true nilstring number boolean nilnilThree things to be able to say out loud before moving on: what local does, what nil means, and why a misspelled variable name does not stop the program.