Variables
By the end of this lesson you'll store text, numbers, and true/false values in Lua, know its handful of types, join strings with .. , measure them with # , convert between types on purpose, and understand why local should be your default — the bedrock of every Lua script.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Lua course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A global variable is like writing on a shared whiteboard in a busy office — anyone in any room can read it, and anyone can wipe it or scribble over it. A local variable is a sticky note inside your own notebook: only you, in this room, can see it, and it's thrown away when you leave. Most bugs in beginner Lua come from scribbling on the shared whiteboard by accident, so you'll reach for the notebook ( local ) almost every time.
1. local vs Global Variables
In Lua you make a variable just by assigning to a name: score = 100 . But here's the surprise that catches everyone — that variable is global by default . To keep it tidy and safe you put local in front: local score = 100 . A global is visible to your whole program and every library; a local only lives inside its current block. Read this worked example, run it, and watch the local vanish outside its block.
Because a bare assignment creates a global, a typo silently makes a new variable instead of erroring. Write socre = 100 when you meant score , and Lua happily creates socre — your real value never changes and nothing warns you. Defaulting to local turns this whole class of bug into something you can actually spot.
2. The Eight Types (and Dynamic Typing)
Lua keeps things small: there are exactly 8 types — nil , boolean , number , string , table , function , userdata , and thread . Day to day you'll use the first five. Note that one number type covers both whole numbers and decimals. Lua is dynamically typed : a variable name carries no type at all — only the value inside it does — so the same name can hold a number now and a string a moment later. Use type(x) any time you want to ask what a value is.
The greyed rows ( function , userdata , thread ) come later — you'll meet function and table properly in the next lesson.
Your turn. The program below is almost finished — fill in the three blanks marked ___ using the hints, then run it and compare with the expected output.
3. Strings: Joining, Length, and Converting
To glue text together you use the concatenation operator .. (two dots) — not + , which is only for number maths. To find how long a string is, put the length operator # in front of it: #"Lua" is 3 . When you need to move between text and numbers deliberately, tostring(value) turns anything into text and tonumber(text) turns text into a number (or gives nil if it isn't one). Numbers used inside .. convert to text for you automatically.
Now you try. Text typed by a user always arrives as a string , so before doing maths you convert it. Fill in the two blanks:
No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. This is the kind of tiny program real games and tools are stitched together from.
Practice quiz
In Lua, a variable declared without local is...
- Global
- Constant
- Local to the file
- A syntax error
Answer: Global. Variables are global by default; local makes them block-scoped.
How many types does Lua have?
- 5
- 8
- 12
- 3
Answer: 8. Lua has 8 types: nil, boolean, number, string, table, function, userdata, thread.
What does type("Lua") return?
- text
- char
- string
- str
Answer: string. The string type is reported as "string" by type().
What does nil represent in Lua?
- Zero
- An empty string
- False
- No value / absence
Answer: No value / absence. nil means there is no value present.
Which operator gives the length of a string?
- #
- len()
- length
- $
Answer: #. The # operator returns a string's length (or a table's array length).
Does Lua have one number type for both integers and decimals?
- No, separate int and float keywords
- Yes, a single number type covers both
- Only integers exist
- Only decimals exist
Answer: Yes, a single number type covers both. Lua's number type covers whole numbers and decimals alike.
What does tonumber("hello") return?
- 0
- An error
- nil
- "hello"
Answer: nil. tonumber returns nil when the text isn't a valid number.
Lua is dynamically typed, which means...
- Types never change
- Variables have fixed types
- You must annotate every type
- The value has a type, not the variable name
Answer: The value has a type, not the variable name. A name can hold a number now and a string later; only the value has a type.
Which function converts a number to text?
- tostring(n)
- toString(n)
- str(n)
- string(n)
Answer: tostring(n). tostring() converts any value to its string form.
Why is the bug socre = 100 (meant: score) hard to spot?
- It errors loudly at runtime
- It silently creates a new global variable
- It crashes the program
- Lua auto-corrects it
Answer: It silently creates a new global variable. A typo without local just creates an unintended global; nothing warns you.