beginner
15 min

Lua Basics: Variables & Data Types

Learn the fundamentals of Lua programming including variables, strings, numbers, and booleans.

VariablesData TypesPrint Statements

1
Introduction to Variables

Variables are containers that store data. In Lua, you can create variables using the `local` keyword for local scope or without it for global scope.

Creating Variables

-- Local variable (recommended)
local playerName = "Steve"
local playerAge = 25

-- Global variable
gameTitle = "My Game"

Local variables are only accessible within their scope and are more efficient. Always use local when possible!

💡 Tips:

  • • Always use `local` for variables unless you need them globally
  • • Variable names should be descriptive
  • • Use camelCase for naming

2
Data Types

Lua has several basic data types: strings, numbers, booleans, nil, tables, and functions.

String Type

local message = "Hello World!"
local name = 'Player'
local multiline = [[
  This is a
  multiline string
]]

Strings can use single quotes, double quotes, or double brackets for multiline text.

Number Type

local health = 100
local speed = 16.5
local damage = -10

All numbers in Lua are doubles (decimal numbers). Integers and decimals are the same type.

Boolean Type

local isAlive = true
local hasWeapon = false
local canJump = true

Booleans represent true or false values, commonly used in conditions.

Nil Type

local emptyValue = nil
local undefinedVar  -- This is also nil

nil represents the absence of a value. Undefined variables are nil by default.

3
Print Statements

Use the `print()` function to output values to the console. This is essential for debugging!

Basic Printing

print("Hello, World!")
print(100)
print(true)

local name = "Player"
print("Welcome, " .. name)  -- String concatenation

The print function displays output in the Roblox output console. Use `..` to concatenate (join) strings.

💡 Tips:

  • • Use print() frequently to debug your code
  • • You can print multiple values: print(var1, var2, var3)

Practice Exercises

Exercise 1: Create Your First Variables

Create three local variables: one string for your name, one number for your age, and one boolean for if you like scripting.

Starter Code:

-- Create your variables here

-- Print them out
print(name)
print(age)
print(likesScripting)
Show Solution
local name = "Alex"
local age = 16
local likesScripting = true

print(name)
print(age)
print(likesScripting)

Exercise 2: String Concatenation

Create a greeting message by combining multiple strings.

Starter Code:

local firstName = "John"
local lastName = "Doe"

-- Combine them into a full greeting
local greeting = -- Your code here

print(greeting)
Show Solution
local firstName = "John"
local lastName = "Doe"

local greeting = "Hello, " .. firstName .. " " .. lastName .. "!"

print(greeting)

Ready for more?

Continue your learning journey

Executors.Online - Your Roblox Developer Hub