Good to see you again. In the previous lesson, you created local variables that store numbers, strings, booleans, and nil. You could print each value, but Output did not explicitly tell you what kind of value it was.
This lesson adds a small but powerful debugging tool: Luau’s built-in type() function. You will use it to inspect the value currently stored in a variable and confirm whether it is a number, string, boolean, or nil.
Ask Luau what kind of value you have
The pattern is:
type(value)
Put a value, or a variable containing a value, inside the parentheses. Luau returns that value’s type as text.
For example:
local startingCoins = 25
print(type(startingCoins))
When you playtest, Output shows:
number
startingCoins holds the number 25, so type(startingCoins) produces the text "number".
A useful way to read this line is:
print(type(startingCoins))
“Find out the type of the value in
startingCoins, then print that answer.”
The type() function does not change your variable. It only examines it.
Check the four value types you already know
Create a few variables in your PracticeScript:
local startingCoins = 25
local welcomeMessage = "Welcome to the practice game!"
local isRoundActive = false
local selectedMap = nil
print(type(startingCoins))
print(type(welcomeMessage))
print(type(isRoundActive))
print(type(selectedMap))
Run the game and look in Output:
number
string
boolean
nil
Each result matches the kind of value stored in its variable.
| Variable | Stored value | Result of type(...) |
|---|---|---|
startingCoins | 25 | "number" |
welcomeMessage | "Welcome to the practice game!" | "string" |
isRoundActive | false | "boolean" |
selectedMap | nil | "nil" |
Notice that Output displays number, not "number" with quotation marks. The quotation marks are part of how you write a string in code; print() displays the characters inside them.
The result of type() is itself a string. For example, this is true:
local healthType = type(startingCoins)
print(healthType)
print(type(healthType))
Output:
number
string
The first line prints the text returned by type(startingCoins). The second line confirms that this returned answer is text, so its type is string.
Read the parentheses carefully
A frequent beginner mistake is accidentally inspecting text instead of inspecting the variable.
Compare these two lines:
local startingCoins = 25
print(type(startingCoins))
print(type("startingCoins"))
Output:
number
string
Why are the answers different?
type(startingCoins)examines the value stored in the variable:25, which is a number.type("startingCoins")examines the text inside quotation marks. Any value inside quotation marks is a string.
This is the same distinction you used with print() in the prior lesson:
print(startingCoins) -- Prints 25
print("startingCoins") -- Prints the word startingCoins
When you want to inspect a variable’s value, write its name without quotation marks.
nil is worth observing closely:
local selectedMap = nil
print(selectedMap)
print(type(selectedMap))
Both Output lines may appear as:
nil
nil
But the code is doing two different things:
print(selectedMap)prints the current value, which isnil.print(type(selectedMap))prints the string returned bytype():"nil".
In both cases, Output displays the letters nil without quotation marks.
See the Roblox documentation’s example
Scripting | Documentation - Roblox Creator Hub
Read the “Luau basics” portion of Roblox Creator Hub’s Scripting documentation. It confirms that Luau can inspect a value with type() and shows an example with a string and another with nil.
In the “Luau basics” subsection, begin with the paragraph explaining that Luau checks types. Study the short logMessage example immediately below it, especially the line that prints type(logMessage). Then continue to the paragraph beginning “Luau uses nil” and inspect the messageToUser example directly below it. Focus on the fact that type() reports the current value's category rather than changing that value.
Use type() as a quick script check
For now, you will mainly use type() to answer questions such as:
- Did I accidentally put quotation marks around a number?
- Is this variable currently
nilbecause nothing has been chosen yet? - Did I store
trueor"true"?
Consider a game setting that controls whether a door starts open:
local isDoorOpen = false
print(type(isDoorOpen))
Output:
boolean
Now compare it with this mistake:
local isDoorOpen = "false"
print(type(isDoorOpen))
Output:
string
The word looks similar, but the game receives a completely different kind of value.
falseis a boolean state."false"is text containing five characters.
Later, conditions will need real booleans such as true and false. Checking with type() is a fast way to catch this kind of mistake before it becomes harder to trace.
Build a value-inspector Script
Replace the contents of your PracticeScript with the following:
-- Inspect the types of several game settings.
local startingCoins = 25
local welcomeMessage = "Welcome to the practice game!"
local isRoundActive = false
local selectedMap = nil
print("startingCoins type:")
print(type(startingCoins))
print("welcomeMessage type:")
print(type(welcomeMessage))
print("isRoundActive type:")
print(type(isRoundActive))
print("selectedMap type:")
print(type(selectedMap))
Your Output should be organized like this:
startingCoins type:
number
welcomeMessage type:
string
isRoundActive type:
boolean
selectedMap type:
nil
The extra label lines make Output easier to read. As your scripts become larger, clear Output messages will help you identify which value you are checking.
Make one controlled change, then run the script again:
local startingCoins = "25"
The value still looks like 25, but Output should now report:
string
Change it back to a number when you are finished:
local startingCoins = 25
This is a useful scripting habit: when a value behaves unexpectedly, print both the value and its type.
print(startingCoins)
print(type(startingCoins))
Key takeaways
type() inspects a value and returns its type as a string:
type(value)
Use it inside print() to see the result in Output:
print(type(startingCoins))
For the values you know so far, the answers are:
25has typenumber"Hello"has typestringtrueandfalsehave typebooleannilhas typenil
Most importantly, inspect a variable without quotation marks:
type(startingCoins)
Writing "startingCoins" would inspect the text itself, which is always a string.
Next, you will start changing numeric variables with arithmetic operators, allowing a script to calculate values such as scores, health, and coin totals.
Can't find a good explanation? Sign up and we'll make it for you
Sign up