Lesson 7 of 8
Programming from zero, part 1: values and variables
Learn programming from absolute zero in Luau, part 1 of 3: runnable code cells, numbers, strings, booleans, nil, variables, assignment and naming.
By the end you can
- Run Luau code in the browser and use numbers, strings, booleans and nil.
- Store and update values in local variables with = and +=.
- Spot case-sensitive naming mistakes that turn a variable into nil.
1Your first program
When a player touches a coin pad in Beacon Yard, the server runs a few lines of code that boil down to "add five to this player's coins". Over these three parts you will write that logic yourself, plus a little shop.
The language is LuauLuauThe programming language of Roblox, derived from Lua 5.1 with gradual types and a faster runtime.Think of it as lua with labels on the jars., the language every Roblox game is written in 1. The grey boxes below are real Luau running in your browser. Press Run, read the output, then change the code and run it again. You cannot break anything.
print("Hello, Beacon Yard")
Hello, Beacon Yardprint shows whatever is inside its brackets in the output. The text in double quotes is printed exactly as written. Try changing the words.
Open this step in lesson mode · Link to this step · This confused me
2Print several things
print can take several things separated by commas, and it puts a space between them. Lines that start with -- are commentsCommentA note in the code that Luau ignores, starting with --.Think of it as a margin note for the next person who reads the page.: notes for humans, which Luau ignores.
-- A comment: Luau skips this line.
print("Coins:", 5)
print("Pad", 3, "is ready")
Coins: 5
Pad 3 is readyWhat does this print?
print("Round", 2, "of", 5)
Answer
Round 2 of 5. print separates its values with a single space. The quotes mark where text starts and ends; they are not printed.
Open this step in lesson mode · Link to this step · This confused me
3Numbers and arithmetic
A program works with valuesValueA piece of data a program works with, such as a number, a string, true or nil.Think of it as the thing a name tag gets stuck on.. The first kind is a number. Luau does arithmetic with +, -, * (times) and / (divide), plus three you may not know: // divides and drops the remainder, % gives the remainder, and ^ raises to a power.
print(5 + 10)
print(40 - 15)
print(5 * 3)
print(10 / 4)
print(7 // 2, 7 % 2)
print(2 ^ 3)
15
25
15
2.5
3 1
8A round lasts 40 seconds. What does this print?
print(40 // 15, 40 % 15)
Answer
2 10. 40 divided by 15 is 2 with 10 left over. // gives the 2 and % gives the 10.
Open this step in lesson mode · Link to this step · This confused me
4Text: strings
Text in quotes is a stringStringText in quotes, such as "Hello".Think of it as a row of letters strung together.. You can use double or single quotes. Two dots, .., join strings end to end, and if one side is a number, Luau turns it into text first.
print("Beacon" .. " " .. "Yard")
print("Pad" .. 3)
print("Coins: " .. 5 * 2)
Beacon Yard
Pad3
Coins: 10Notice the last line. Luau did the multiplication first, because * comes before .., just as multiplication comes before addition in maths.
What does this print?
print("Pad" .. 1 .. 2)
Answer
Pad12. .. joins text; it never adds. The numbers 1 and 2 become the text "1" and "2" and are stuck on the end.
Open this step in lesson mode · Link to this step · This confused me
5true, false and nil
Two more kinds of value complete the basics. A boolean is true or false, the answer to a yes-or-no question. And nil means nothing: no value at all. You will see nil whenever you ask for something that is not there.
type tells you what kind of value you have:
print(type(5), type("5"), type(true), type(nil))
print(5 == 5, 5 == 6)
number string boolean nil
true false== asks "are these equal?" and answers with a boolean. One = means something else, as the next two steps show.
| In the picture | In Roblox |
|---|---|
| Checking whether two values match | Comparison with ==, which returns true or false |
| Setting a name to a value | Assignment with =, which works out the right side and moves the tag |
What is type("40")?
Answer
string. The quotes make it text. "40" and 40 look alike but are different kinds of value.
Open this step in lesson mode · Link to this step · This confused me
6Variables: name tags
| In the picture | In Roblox |
|---|---|
| The tag | The variable name |
| The thing the tag is stuck to | The value |
| Moving the tag | Assignment with = |
| A tag only this part of the room can read | A local variable and its scope |
A program needs to remember things, such as how many coins you have. A variableVariableA name for a value, made with local and changed with =.Think of it as a name tag stuck on a value. is a name for a value. Think of it as a name tag stuck on the value. local coins = 0 makes a tag called coins and sticks it on the number 0. Whenever you write coins afterwards, Luau looks at the tag and uses the value it is stuck on.
local means the tag belongs to this part of the program. Always use it when you make a new variable; part 2 explains why.
Think of it as A name tag stuck on a value
local coins = 0
local coinValue = 5
local playerName = "Player1"
print(playerName, "has", coins, "coins")
print("Each coin is worth", coinValue)
Player1 has 0 coins
Each coin is worth 5Open this step in lesson mode · Link to this step · This confused me
7Change a variable
One = means assign: work out the right-hand side, then move the tag on the left onto the result. So coins = coins + 5 reads "take what coins is now, add 5, and stick the coins tag on the answer". Notice there is no local the second time, because the tag already exists.
Both sidesRemember: Assignment with = is a maths statement saying two sides are equal.
Where does it break?“A maths statement saying two sides are equal” is a good picture of assignment with =. Where does it stop being true?
It breaks here. In maths, coins = coins + 5 is impossible, because a number cannot equal itself plus five; in Luau, = is an order to compute the right side first and stick the left tag on the answer.
The true model. Single = evaluates the right-hand expression completely, then binds the variable on the left to that result. Double == compares two values and produces a boolean without changing either side.
local coins = 0
coins = coins + 5
coins = coins + 5
print(coins)
10What does this print?
local coins = 10
coins = coins * 2
coins = coins - 5
print(coins)
Answer
15. 10, then 10 * 2 = 20, then 20 - 5 = 15. Each line starts from the value the previous line left.
Open this step in lesson mode · Link to this step · This confused me
8A shortcut: +=
Adding to a variable is so common that Luau has a shortcut. coins += 5 means exactly coins = coins + 5. The same works with -=, *= and /=, and ..= adds text to the end of a string.
Both sidesRemember: A variable is a name tag stuck on a value.
local coins = 0
coins += 5
coins += 5
coins -= 3
local log = "Picked up"
log ..= " two coins"
print(coins, log)
7 Picked up two coinsGo deeper If you know Python
Python also has +=. Luau has no ++ and no i = i++. Luau joins strings with .., while Python uses +. In Luau, + on two strings tries to do arithmetic.
Which line adds the coin value to coins?
Answer
coins += COIN_VALUE. += is the shortcut for coins = coins + COIN_VALUE. The first option works out a sum and throws it away.
Open this step in lesson mode · Link to this step · This confused me
9Naming things
Variable names can use letters, digits and _, but cannot start with a digit or contain spaces. Names are case-sensitive: coins and Coins are two different tags. A few words, such as local, if, end and function, belong to Luau and cannot be names.
Beacon Yard follows the usual Roblox style. Variables use camelCase (coinValue), and fixed settings use UPPER_CASE (COIN_VALUE), so a reader can tell at a glance which numbers are meant to be tuned in Config.luau:
local Config = {
ROUND_SECONDS = 40,
COOLDOWN_SECONDS = 8,
COIN_VALUE = 5,
COIN_RESPAWN_SECONDS = 4,
SURVIVAL_BONUS = 10,
DRONE_SPEED = 12,
DRONE_DAMAGE = 15,
DRONE_HIT_COOLDOWN = 1,
DRONE_MAX_HEALTH = 100,
WRENCH_DAMAGE = 34,
WRENCH_RANGE = 14,
STUN_SECONDS = 1.6,
WALK_SPEED = 16,
Which are valid Luau variable names?
Answer
coinValue; COIN_VALUE; _hidden. Names cannot start with a digit, cannot contain spaces, and cannot be reserved words like end.
Open this step in lesson mode · Link to this step · This confused me
10When a name is misspelt
Here is the first bug every programmer meets. Misspell a variable and Luau does not complain straight away. It looks for a tag with that name, finds none, and uses nil. The trouble only shows when you try to do something with that nil:
local coins = 5
print(coinz)
print(coinz + 1)
The error says where (main:3, line 3) and what (it tried to add nil and a number). The errors lesson takes this apart properly. For now, when you see nil where you expected a value, check the spelling first.
Fix the bug so this prints 15.
local coinValue = 5
local coins = 10
print(coins + coinvalue)
Answer
Line 3: print(coins + coinvalue). Fixed: print(coins + coinValue). coinvalue (lower-case v) is a different, empty name. Names are case-sensitive.
Open this step in lesson mode · Link to this step · This confused me
11Challenge: tally a round
Put values, variables and += together. Finish the code in the cell below, then press Run: when the output matches, the cell shows Passed and this challenge completes.
Level 1, Apprentice
Challenge
Track a player's balance through three coin pickups and one Speed boost purchase, then print what is left.
- Start from
- The code cell below the challenge. Press Run to check it.
- Check it here
- Run the code cell in the lesson view; the challenge completes when it passes. No Studio needed.
Done means
- coins starts at 10, gains three pickups of COIN_VALUE (5), then spends SPEED_PRICE (25)
- The cell prints 'Coins left: 0'
- Self-check, not graded: you use the variable names rather than writing 0 directly
Hints
- Add three pickups with coins += COIN_VALUE * 3, then subtract SPEED_PRICE with coins -= SPEED_PRICE.
- Print the final line with print("Coins left:", coins).
Solution
local COIN_VALUE = 5
local SPEED_PRICE = 25
local coins = 10
coins += COIN_VALUE * 3
coins -= SPEED_PRICE
print("Coins left:", coins)
local COIN_VALUE = 5
local SPEED_PRICE = 25
local coins = 10
-- TODO: add 3 pickups of COIN_VALUE to coins
-- TODO: subtract SPEED_PRICE from coins
print("Coins left:", coins)
Coins left: 0Open this step in lesson mode · Link to this step · This confused me
12Recap
Retiring the analogy
You no longer need “a maths statement saying two sides are equal”. Single = evaluates the right-hand expression completely, then binds the variable on the left to that result. Double == compares two values and produces a boolean without changing either side.
Recap
- Values are numbers, strings, booleans and nil. type(x) tells you which kind a value is, and .. joins text.
- A variable is a name tag: local makes one, = works out the right side and moves the tag, and += is a shortcut for adding to it.
- Names are case-sensitive; a misspelt variable is an empty tag that reads as nil.
Assignment with = is a maths statement saying two sides are equal, until in maths, coins = coins + 5 is impossible, because a number cannot equal itself plus five; in Luau, = is an order to compute the right side first and stick the left tag on the answer.
A variable is a name tag stuck on a value, until two tags can be stuck on the same table, so changing the table through one tag shows through the other, because there is only one table.
Queued for review
Next Programming from zero, part 2: decisions, loops and functions
Open this step in lesson mode · Link to this step · This confused me