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.
Step 1 of 12
Your 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 shows whatever is inside its brackets in the output. The text in double quotes is printed exactly as written. Try changing the words.
Step 2 of 12
Print 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.
What does this print?
print("Round", 2, "of", 5)
Press 1 to 4 to choose, Enter to check.
Step 3 of 12
Numbers 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.
A round lasts 40 seconds. What does this print?
print(40 // 15, 40 % 15)
Press 1 to 4 to choose, Enter to check.
Step 4 of 12
Text: 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.
Notice 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)
Press 1 to 4 to choose, Enter to check.
Step 5 of 12
true, 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:
== 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")?
Press 1 to 4 to choose, Enter to check.
Step 6 of 12
Variables: 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
Step 7 of 12
Change 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.
What does this print?
local coins = 10
coins = coins * 2
coins = coins - 5
print(coins)
Press 1 to 4 to choose, Enter to check.
Step 8 of 12
A 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.
Go 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?
Press 1 to 4 to choose, Enter to check.
Step 9 of 12
Naming 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?
Press 1 to 6 to choose, Enter to check.
Step 10 of 12
When 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:
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.
Press 1 to 3 to choose, Enter to check.
Step 11 of 12
Challenge: 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; 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
Step 12 of 12
Recap
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