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.

Lab lesson, about 30 minutes, 12 steps

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.

Luau
print("Hello, Beacon Yard")
Figure 1.1. Expected output: Hello, Beacon Yard

print 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

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.

Luau
print(5 + 10)
print(40 - 15)
print(5 * 3)
print(10 / 4)
print(7 // 2, 7 % 2)
print(2 ^ 3)
Figure 3.1. Expected output: 15 25 15 2.5 3 1 8

A round lasts 40 seconds. What does this print?

print(40 // 15, 40 % 15)
  1. 2.67 0
  2. 2 10
  3. 3 5
  4. 2 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.

Luau
print("Beacon" .. " " .. "Yard")
print("Pad" .. 3)
print("Coins: " .. 5 * 2)
Figure 4.1. Expected output: Beacon Yard Pad3 Coins: 10

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)
  1. Pad3
  2. Pad12
  3. Pad 1 2
  4. an error
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:

Luau
print(type(5), type("5"), type(true), type(nil))
print(5 == 5, 5 == 6)
Figure 5.1. Expected output: 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.

Both sidesAssignment with = is a maths statement saying two sides are equal.
In the pictureIn Roblox
Checking whether two values matchComparison with ==, which returns true or false
Setting a name to a valueAssignment with =, which works out the right side and moves the tag

See the whole analogy cast

What is type("40")?

  1. number
  2. string
  3. boolean
  4. nil
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

Both sidesA variable is a name tag stuck on a value.
In the pictureIn Roblox
The tagThe variable name
The thing the tag is stuck toThe value
Moving the tagAssignment with =
A tag only this part of the room can readA local variable and its scope

See the whole analogy cast

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

Luau
local coins = 0
local coinValue = 5
local playerName = "Player1"
print(playerName, "has", coins, "coins")
print("Each coin is worth", coinValue)
Figure 6.1. Expected output: Player1 has 0 coins Each coin is worth 5

Open 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.

Luau
local coins = 0
coins = coins + 5
coins = coins + 5
print(coins)
Figure 7.1. Expected output: 10

What does this print?

local coins = 10
coins = coins * 2
coins = coins - 5
print(coins)
  1. 10
  2. 15
  3. 20
  4. 25
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.

Luau
local coins = 0
coins += 5
coins += 5
coins -= 3
local log = "Picked up"
log ..= " two coins"
print(coins, log)
Figure 8.1. Expected output: 7 Picked up two coins
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?

  1. coins + COIN_VALUE
  2. coins =+ COIN_VALUE
  3. coins += COIN_VALUE
  4. coins ++ COIN_VALUE
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,
Figure 9.1

Which are valid Luau variable names?

  1. coinValue
  2. 2ndPad
  3. COIN_VALUE
  4. round time
  5. _hidden
  6. end
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:

Luau
local coins = 5
print(coinz)
print(coinz + 1)
Figure 10.1. Run it in the lesson view or in the Studio command bar.

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

  1. Add three pickups with coins += COIN_VALUE * 3, then subtract SPEED_PRICE with coins -= SPEED_PRICE.
  2. Print the final line with print("Coins left:", coins).
Solution
luau
local COIN_VALUE = 5
local SPEED_PRICE = 25
local coins = 10
coins += COIN_VALUE * 3
coins -= SPEED_PRICE
print("Coins left:", coins)
Luau
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)
Figure 11.1. Expected output: Coins left: 0

Open 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

  1. Values are numbers, strings, booleans and nil. type(x) tells you which kind a value is, and .. joins text.
  2. 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.
  3. 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.

Next Programming from zero, part 2: decisions, loops and functions

Open this step in lesson mode · Link to this step · This confused me