Appearance
Variables and Values
Written: 2026-10-14
Learning objectives
- You explain what a variable is in your own words.
- You create variables with
localand give them values with=. - You name variables with camelCase and full words.
- You tell values apart from the variables that hold them.
Prerequisites
- Read First Script. You already ran code and pressed Play.
What a variable is
A variable is a name you give to a value so you can use it later. Think of it as a label on a box. The label is the variable. The thing inside the box is the value.
Values are the actual data. "Maya" is a value. 10 is a value. true is a value. A variable is the name you stick on one of those values so your code can find it again.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local playerName = "Maya"
print(playerName)txt
MayaWalkthrough:
- Line 1 turns on strict mode. Every script in this course starts with it.
- Line 2 creates a variable called
playerNameand puts the value"Maya"inside it. - Line 3 prints whatever
playerNamecurrently holds.
Create variables with local
You create a variable in two steps. Write local, then the name, then =, then the value. The = sign is called assignment. It copies the value on the right into the variable on the left.
The word local matters. It keeps the variable inside your script. Without it, the variable leaks into a shared global place and runs slower. Always write local. You can read more about why in scope.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local score = 0
print(score)
score = 10
print(score)txt
0
10Walkthrough:
- Line 1 turns on strict mode.
- Line 2 creates
scoreand assigns it0. - Line 3 prints
0, the current value ofscore. - Line 4 assigns a new value,
10, to the same variable. The old0is gone. - Line 5 prints
10.
Notice that line 4 has no local. You use local once, when you create the variable. After that you assign with a plain =.
You can also create a variable with no value yet. It then holds a special value called nil, which means "nothing here". You will meet nil properly in Strings, Numbers, and Nil.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local nickname = "Max"
local teamScore = 4
local finished = false
print(nickname)
print(teamScore)
print(finished)txt
Max
4
falseWalkthrough:
- Line 1 turns on strict mode.
- Line 2 stores the strings value
"Max"innickname. - Line 3 stores the numbers value
4inteamScore. - Line 4 stores the booleans value
falseinfinished. - Lines 5 to 7 print each variable in order.
Names that help you
Good names make your code read like sentences. Follow three rules.
First, use camelCase. Start with a lowercase letter. Capitalize the first letter of each extra word. No spaces, no dashes.
Second, spell words out fully. teamScore beats ts. playerName beats pn. You read code far more often than you type it.
Third, say what the thing means, not what type it is. winnerName beats string1.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local winnerName = "Maya"
local runnerUpName = "Rio"
print(winnerName)
print(runnerUpName)txt
Maya
RioWalkthrough:
- Line 1 turns on strict mode.
- Line 2 creates
winnerName. Each word after the first starts uppercase. - Line 3 creates
runnerUpNamewith the same style. - Lines 4 and 5 print both values.
Names must start with a letter or an underscore. They can contain letters, digits, and underscores after that. They cannot be reserved words like if, then, local, or true. Names are case-sensitive, so score and Score are two different variables.
Values live on their own
A variable only points at a value, which exists on its own. Two variables can point at the same value. Changing one variable never changes the other.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local firstPick = "apple"
local secondPick = firstPick
print(firstPick)
print(secondPick)
firstPick = "pear"
print(firstPick)
print(secondPick)txt
apple
apple
pear
appleWalkthrough:
- Line 1 turns on strict mode.
- Line 2 puts the value
"apple"infirstPick. - Line 3 copies the current value of
firstPickinto a new variable calledsecondPick. Now both hold"apple". - Lines 4 and 5 print
appletwice. - Line 6 gives
firstPicka new value,"pear". This touches onlyfirstPick. - Line 7 prints
pearforfirstPick. - Line 8 prints
appleforsecondPick. It kept its own copy.
This split causes many beginner bugs. When output looks wrong, ask yourself: which variable did I actually change, and which one did I actually print?
Common mistakes
Forgetting local.
Run this to see the sloppy version: put it in a Script in ServerScriptService and press Play.
luau
--!strict
score = 5
print(score)txt
5This prints 5, but score becomes a global variable. Strict mode underlines it, and globals run slower. Fix it by adding local where you create the variable. Run the fix the same way.
luau
--!strict
local score = 5
print(score)txt
5Putting spaces or dashes in names.
Run this to see it fail: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local team score = 4
print(team score)txt
Red syntax error on line 2: the script does not run until you fix the name.Luau reads team and score as two separate names. Fix it with camelCase. Run the fix the same way.
luau
--!strict
local teamScore = 4
print(teamScore)txt
4Mixing up the value and the variable.
Run this to see it fail: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local playerName = Maya
print(playerName)txt
nilMaya with no quotes is read as a variable name. That variable does not exist, so strict mode flags it and it arrives as nil. Fix it by quoting text values. Run the fix the same way.
luau
--!strict
local playerName = "Maya"
print(playerName)txt
MayaReusing local on every assignment.
Run this: put it in a Script in ServerScriptService and press Play.
luau
--!strict
local score = 0
local score = 10
print(score)txt
10This prints 10, but the second local builds a brand-new variable that hides the first one. Two variables with one name confuse every reader. Fix it by assigning without local after creation. Run the fix the same way.
luau
--!strict
local score = 0
score = 10
print(score)txt
10Exercises
Run every solution below in a Script in ServerScriptService and press Play. Try each exercise yourself before you open its solution.
- Create a variable called
petNamewith your favorite animal name in it. Print it.
Solution 1
luau
--!strict
local petName = "Biscuit"
print(petName)txt
Biscuit- Create
coinswith the value20. Print it. Then changecoinsto35and print it again.
Solution 2
luau
--!strict
local coins = 20
print(coins)
coins = 35
print(coins)txt
20
35- Create
firstNameandlastNamewith two words in them. Print each one on its own line.
Solution 3
luau
--!strict
local firstName = "Maya"
local lastName = "Reyes"
print(firstName)
print(lastName)txt
Maya
Reyes- Predict the output before you run it. Then run it and check yourself.
Solution 4
luau
--!strict
local greeting = "hi"
local copiedGreeting = greeting
greeting = "bye"
print(greeting)
print(copiedGreeting)txt
bye
hiThe third line copies "hi" into copiedGreeting. The fourth line changes only greeting, so copiedGreeting still prints hi.
Summary
You learned that a variable is a named label for a value. You create variables with local, assign values with =, and name them in camelCase with full words. Values stand on their own, and each variable holds its own copy.
Next: Operators and Logic.