Learn the fundamentals of Lua programming including variables, strings, numbers, and booleans.
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.
-- 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!
Lua has several basic data types: strings, numbers, booleans, nil, tables, and functions.
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.
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.
local isAlive = true
local hasWeapon = false
local canJump = true
Booleans represent true or false values, commonly used in conditions.
local emptyValue = nil
local undefinedVar -- This is also nil
nil represents the absence of a value. Undefined variables are nil by default.
Use the `print()` function to output values to the console. This is essential for debugging!
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.
Create three local variables: one string for your name, one number for your age, and one boolean for if you like scripting.
-- Create your variables here
-- Print them out
print(name)
print(age)
print(likesScripting)
local name = "Alex"
local age = 16
local likesScripting = true
print(name)
print(age)
print(likesScripting)
Create a greeting message by combining multiple strings.
local firstName = "John"
local lastName = "Doe"
-- Combine them into a full greeting
local greeting = -- Your code here
print(greeting)
local firstName = "John"
local lastName = "Doe"
local greeting = "Hello, " .. firstName .. " " .. lastName .. "!"
print(greeting)
Continue your learning journey