Skip To Content
Not affiliated with Roblox Corporation. 9Roblox never asks for your password and never offers free Robux.

Luau Variables And Data Types For Beginners

Roblox Studio · Scripting Basics · Updated

Learn local variables, naming conventions, core Luau data types, 1-based tables, truthiness rules, and strict type annotations in Roblox Studio.

Difficulty
Beginner
Time Needed
15 To 20 Minutes
Preferred Scope
local (Faster Lookup And Safer Scoping)
Falsy Values In Luau
Only false And nil (0 And Empty Strings Are Truthy)
Array Indexing
1-Based (First Element Is [1], Not [0])
Type Checking Modes
--!nocheck, --!nonstrict, --!strict

Declare Local Variables And Follow Naming Rules

A variable stores a named value in memory using the assignment operator (=). Although Luau defaults to global scope when no keyword is written, always place local before a new variable name: local variables run faster inside the Luau virtual machine and stay isolated inside their script or code block instead of leaking across functions.

Variable names are case-sensitive, meaning playerScore and PlayerScore refer to two distinct containers. Names may contain letters, numbers, and underscores, cannot begin with a digit, and cannot match reserved Luau keywords such as if, then, end, for, while, function, local, return, true, false, or nil. Avoid leading underscores paired with all-uppercase letters like _VERSION, which Luau reserves for internal globals.

Understand Core Luau Primitive Data Types

Luau builds scripts around four primitive value categories—number, string, boolean, and nil—each carrying small behaviors that often surprise programmers coming from other languages. Numbers use 64-bit IEEE-754 double precision (representing integers accurately up to 2^53) and allow underscore separators like 1_000_000, hexadecimal literals (0xFF), binary literals (0b1010), and scientific notation (12e3). While Luau has one unified number type, individual Roblox APIs still expect whole integers such as 64-bit int64 values for Player.UserId.

Strings hold text inside double quotes, single quotes, or multi-line [[...]] brackets. Joining strings with two dots (..) does not add spacing automatically, so include a space explicitly—or pass comma-separated arguments into print() when logging to Output. Convert between text and numbers explicitly using tostring() and tonumber().

Booleans hold true or false, while nil represents the complete absence of a value. Critically, Luau treats only false and nil as falsy inside conditional checks: both the number 0 and an empty string "" evaluate as true. Assigning nil removes a key from a dictionary, detaches an instance from the world when applied to Parent, or releases large references so Luau's garbage collector can free memory.

Store Lists And Key Pairs Inside Tables

Whenever a script needs to hold an inventory, wave order, or player profile, Luau uses curly braces ({}) to construct a table. Tables store any non-nil values—numbers, strings, booleans, functions, instances, or nested tables—and work either as ordered arrays or as key-value dictionaries.

Arrays assign sequential numeric positions automatically, but unlike JavaScript, Python, or C++, Luau uses 1-based indexing. The first item lives at list[1] rather than list[0], and attempting to read list[0] simply returns nil. Dictionaries map named keys such as stats.coins or stats["spawnPoint"] to values, making configuration objects easy to read and update.

Recognize Roblox Engine Types And Typeof

Beyond core Luau primitives, Roblox Studio adds specialized engine data types such as Vector3 for 3D positions and sizes, CFrame for position combined with orientation, Color3 for RGB colors, and Instance references for objects inside the Explorer hierarchy.

When inspecting values during debugging, reach for Roblox's typeof() function instead of standard Lua's type(). Standard type() reports generic labels such as userdata for engine structures like Color3 or CFrame, whereas typeof() returns the exact Roblox class or data type name so checks remain clear and reliable.

Catch Bugs Early With Luau Type Annotations

Unlike standard Lua 5.1, Luau includes a gradual type checker integrated directly into Studio's Script Editor. Adding a colon and type after a local variable—such as local coins: number = 250 or an optional string? that permits nil—lets Studio warn you about misspelled properties or mismatched math before you even start a playtest.

You control how strictly a file is checked by placing a directive comment on line 1: --!nocheck turns checking off, --!nonstrict checks variables you annotate explicitly, and --!strict validates both inferred and annotated types across the entire script and surfaces mismatches in Script Analysis.

Test Variables And Data Types In Roblox Studio

Insert a Script into ServerScriptService, paste the --!strict Luau example below, and start a playtest with F5. Watch the Output window to verify string concatenation, 1-based array lookup, nil output, Roblox typeof() inspection, and Luau's truthiness rule for zero.

--!strict
local MAX_COINS: number = 1_000
local playerName: string = "BuilderOne"
local isReady: boolean = true
local bonusItem: string? = nil

local starterGear: { string } = { "Flashlight", "Compass", "Map" }
local playerStats = {
	coins = 250,
	spawnPoint = Vector3.new(0, 10, 0),
	teamColor = Color3.fromRGB(60, 180, 120),
}

print(playerName .. " starts with " .. tostring(playerStats.coins) .. " / " .. tostring(MAX_COINS) .. " coins.")
print("First gear item (index 1):", starterGear[1])
print("Bonus item:", bonusItem)
print("Roblox type of teamColor:", typeof(playerStats.teamColor))

if isReady and 0 then
	print("In Luau, 0 evaluates as true; only false and nil are falsy.")
end

More In Scripting Basics