Variables are one of the most important concepts in programming. They allow us to store, update, and reuse values in our code.
📦 What is a Variable?
A variable is a named storage location that holds a value or a reference to a value.
You can use it to remember things like a player’s score, a player’s name, the time of day, references to other objects, and much more!
We can retrieve these variables and access the values stored inside of them, so think of them like labelled cardboard boxes in your home that you can put stuff in and take stuff out.
The following code below will be executed line-by-line from top to bottom, initializing each variable, and then printing the value stored in score:
score =100
playerName ="Alex"
timeOfDay ="12:00 PM"
spawnLocation = workspace.SpawnLocation
print(score)-- '100'
Technical Info: For the curious
All values are stored in computer memory in binary format.
When you store a new value in a variable, your computer reserves a physical spot on your computer’s RAM chip to store that information.
Some variables may refer to the same data stored on your RAM chip, while other variables will store their own data in computer memory, even if the values are the same.
Program State
Variables are building blocks for the state of your scripts.
100 represents the current state of score, but at some point in the future, the value of score could change, which reflects a new state of your code at that time.
Ask yourself: What do you think state is?
State refers to the current conditions and values stored in memory that influences the behavior of code.
Multi-Line Declaration
You can even declare multiple variables on the same line using multiple assignment.
You just need to separate the names and values by commas:
score, health, age =500, 100, 25
print(score, health, age)-- 500, 100, 25
Changing a Variable
You can update a variable by assigning it a new value using the assignment (=) operator.
health =100
print(health)-- 100
health =80-- assigned a new value to 'health'
print(health)-- 80
Naming Conventions
You may have noticed that we named our variables in a particular way in the previous code blocks.
The most common naming convention is called camelCase, whose rule is that the first word must be lowercase while every subsequent word must have their first letter capitalized!
There are many other common naming conventions in the programming world:
ThisIsCalledPascalCase
this_is_called_snake_case
THIS_IS_LOUD_SNAKE_CASE
Each naming convention is unique for a particular situation. For example, loud snake case is used to represent a variable as being constant, meaning its value should never change.
Other programming languages have the ability to define constants (such as const in C++), but Luau does not have this feature.
To work around this, we name constants differently to communicate to the programmer its value should never change during runtime.
Naming Tips
When we practice more with variables, it’s best to make sure that your variable names are clear, descriptive, and direct as possible.
Use Positive Form
It’s good practice to make sure variable names are in positive form.
A mutable block of memory for data compression and serialization
The vector and buffer data types are also exclusive to Luau and do not exist in standard Lua.
The fundamental data types we’ll be prioritizing over the next few lectures will be nil, string, number, boolean, table, function, and thread.
The other data types will be explored more in the future, so do not worry about them for now!
Data Type Nuances: For the curious
There are certain technical nuances with how these different built-in types behave, which we’ll see over time.
It’s not crucial to memorize these nuances right away, but understanding them early-on can help clear up confusion with their behavior.
You may hear terms like “primitive data types”, “reference data types”, etc., which I explain fully in the cheat sheet.
Do not worry about memorizing this right now. We’ll slowly learn these technicalities throughout future lectures!
type
type is a global function that you can call to determine the type of a given value returned as a string.
This is a useful function for identifying the types of values, and we’ll utilize this function more in the future:
TypesExample
1
myValue =10
2
valueType =type(myValue)
3
print(valueType)-- "number"
4
5
myValue =true
6
valueType =type(myValue)
7
print(valueType)-- "boolean"
8
9
myValue ={1,2,3}
10
valueType =type(myValue)
11
print(valueType)-- "table"
Practice Activities
What will the following code print into the console?
myValue =nil
valueType =type(myValue)
print(valueType)
Click to reveal answer
Answer: "nil"
What will the following code print into the console?
myValue ="Hello, world!"
valueType =type(myValue)
print(valueType)
Click to reveal answer
Answer: "string"
typeof
typeof is another global function that is extremely similar to type but is specifically built for the Roblox game engine.
This function is useful for further refining Roblox data types specific to the game engine and not the Luau language itself.
We’ll see this function used in the future.
Storing Object References
Since learning about indexing for instances, we can store object references in variables and use them for later, which is an extremely common pattern you’ll perform constantly.
Going to back to our first practice activities that we attempted in a previous lecture for indexing instances, I’d like for you to update your code to store the instances in variables before printing them.
Setup
Create a new Script in ServerScriptService
Set the script’s name to "VariablesPractice"
Script the instructions step-by-step
Goal
Store the Terrain, SpawnLocation, and Baseplate into variables
Print any three properties for each instance
Run the game and ensure your prints appear in the output
Tip
When you write the code, notice how you can keep referencing your newly created variables instead of having to constantly re-index the workspace to access your desired object.
Bonus Challenge
Along with printing the values of the properties, utilize the type function to also print the types of the values.
If any of the properties you print display as userdata, then try to further refine the type using typeof instead!
Solution
Click to reveal possible solution
VariablesPractice
1
terrain = workspace.Terrain
2
print(terrain.CollisionGroup)
3
print(type(terrain.CollisionGroup))
4
print(terrain.WaterReflectance)
5
print(type(terrain.WaterReflectance))
6
print(terrain.WaterColor)
7
print(type(terrain.WaterColor))
8
9
spawnLocation = workspace.SpawnLocation
10
print(spawnLocation.CastShadow)
11
print(type(spawnLocation.CastShadow))
12
print(spawnLocation.Material)
13
-- this property will print "userdata"
14
-- so the type can be refined further with typeof
15
print(type(spawnLocation.Material))
16
print(typeof(spawnLocation.Material))
17
print(spawnLocation.Position)
18
print(type(spawnLocation.Position))
19
20
baseplate = workspace.Baseplate
21
print(baseplate.Locked)
22
print(type(baseplate.Locked))
23
print(baseplate.Transparency)
24
print(type(baseplate.Transparency))
25
print(baseplate.Shape)
26
-- typeof can be used here too!
27
print(type(baseplate.Shape))
28
print(typeof(baseplate.Shape))
Output:
Terminal window
Default - Server - VariablesPractice:2
string - Server - VariablesPractice:3
1 - Server - VariablesPractice:4
number - Server - VariablesPractice:5
0.0470588, 0.329412, 0.360784 - Server - VariablesPractice:6
userdata - Server - VariablesPractice:7
true - Server - VariablesPractice:10
boolean - Server - VariablesPractice:11
Enum.Material.Plastic - Server - VariablesPractice:12
userdata - Server - VariablesPractice:15
EnumItem - Server - VariablesPractice:16
0, 0.5, 0 - Server - VariablesPractice:17
vector - Server - VariablesPractice:18
true - Server - VariablesPractice:21
boolean - Server - VariablesPractice:22
0 - Server - VariablesPractice:23
number - Server - VariablesPractice:24
Enum.PartType.Block - Server - VariablesPractice:25
userdata - Server - VariablesPractice:27
EnumItem - Server - VariablesPractice:28
âś… Key Takeaways
Variables store state.
They represent values in memory that describe what your program knows right now. When a variable changes, the state of your code changes.
Assignment = sets or updates values.
You can use it to initialize a variable or reassign it later to reflect new information.
Variables are read top-to-bottom.
Code executes line-by-line, so the order you declare and change variables matters.
Clear naming is better than clever naming.
Descriptive, positive, and consistent names makes your code easier to read and maintain.
Naming conventions communicate intent.
Styles like camelCase, PascalCase, and LOUD_SNAKE_CASE help signal how a variable should be used.
Comments explain why, not just what.
Use comments to clarify intent, assumptions, and reasoning that is not obvious from the code itself.
Every value has a type!
Luau uses different data types to represent different kinds of values, and understanding these types is foundational for writing correct logic.
type & typeof are your inspection tools.
When you are unsure what kind of value you are working with, type/typeof helps you verify.
The Big Picture
Variables are the foundation of everything that follows in this course. Conditionals, loops, functions, systems, and game logic all rely on storing and updating values.
Mastering variables early will make every future concept easier to understand and reason about!