The kinds of values you can put in a variable, why "1" is not 1, and how to convert between them.
A variable is a box. This lesson is about what can go in the box — because Luau won't let you mix everything together. Learn the four types now and you'll dodge a huge share of beginner errors later.
Number — whole numbers, decimals, negatives. Health, speed, and time are all numbers.
local coins = 5
local speed = 3.5
String — text wrapped in quotes. "Sam" is a string; Sam without quotes is an error (Luau thinks you mean a variable named Sam).
local playerName = "Sam"
Boolean — only two values: true or false. Booleans are how your game says yes or no: "is the door open?", "did they win?"
local hasKey = false
nil — means "no value." A variable that has never been assigned holds nil.
Which of these stores a boolean?
This is the trap that catches everyone:
print(1 + 1) --> 2 (two numbers)
print("1" + 1) --> ERROR: attempt to perform arithmetic on a string
The number 1 and the string "1" are completely different things. You can add numbers. You cannot add text.
Same for concatenation the other way — you can't glue a number onto a string without converting:
local coins = 5
print("You have " .. coins .. " coins.") --> You have 5 coins.
Wait — that worked! When you glue with .., Luau is friendly and converts numbers to text for you. But when you try to add with +, it's strict. Memorize it: gluing is friendly, adding is strict.
Why does print('5' + 2) produce an error?
Sometimes you have a string that's actually a number — like the text from a UI input box, where the player typed "10". You need to convert it before math:
local typed = "10"
local amount = tonumber(typed) -- "10" becomes 10
print(amount + 5) --> 15
And the reverse — tostring turns anything into text when you need a string specifically:
local lives = 3
local text = tostring(lives) -- 3 becomes "3"
A TextBox gave you the string '7'. Convert it to a number and add 3, then print the result.
local typed = "7"
-- your line here
local name = Sam errors; local name = "Sam" works.tonumber first... and + to behave the same. .. converts for you, + refuses. Keep them straight.Next lesson: doing the same job more than once — functions.