The Complete Beginner's Guide to Roblox Lua Scripting
Never written a line of Lua? This guide walks you through everything — from your first script to your first working game system — step by step.
What Is Lua and Why Does Roblox Use It?
Lua is a lightweight scripting language built for speed and simplicity. Roblox chose it because it runs fast inside their engine and is easy to learn compared to languages like Java or C++. Every interactive thing in a Roblox game — a door that opens, a tool that fires, a leaderboard that updates — is driven by Lua scripts.
You do not need any prior programming experience to start. If you can follow instructions and think logically, you can learn this.
Setting Up Roblox Studio
Download Roblox Studio for free from roblox.com/create. Once installed:
- Open Studio and create a Baseplate template
- In the top menu, go to View → Explorer and View → Properties
- Click View → Script Editor to open the code editor
- In the Explorer panel, find ServerScriptService and insert a new Script
You are now ready to write your first script.
Your First Script
Click on the Script you just created. You will see this default code:
print("Hello world!")
Press Play (F5) and look at the Output window at the bottom. You should see Hello world! appear. That is Lua running.
Now try this:
print("My name is: " .. "Alex")
print("2 + 2 = " .. tostring(2 + 2))
The .. operator joins strings together. tostring() converts a number to a string so you can print it.
Variables and Data Types
Variables store values so you can use them later.
local playerName = "Alex" -- string (text)
local score = 100 -- number
local isAlive = true -- boolean (true/false)
local nothing = nil -- nil means "no value"
print(playerName) -- prints: Alex
print(score + 50) -- prints: 150
Always use local for your variables. It keeps them contained to the current script and runs faster.
If Statements
If statements let your script make decisions.
local score = 75
if score >= 90 then
print("Grade: A")
elseif score >= 70 then
print("Grade: B")
elseif score >= 50 then
print("Grade: C")
else
print("Grade: F")
end
Roblox uses then and end instead of curly braces. Every if must end with end.
Loops
Loops repeat code. There are three main types:
Counting loop:
for i = 1, 5 do
print("Count: " .. i)
end
While loop:
local count = 0
while count < 3 do
count = count + 1
print("While: " .. count)
end
For-each loop:
local fruits = {"apple", "banana", "cherry"}
for index, fruit in ipairs(fruits) do
print(index .. ": " .. fruit)
end
Functions
Functions are reusable blocks of code.
local function greet(name)
return "Hello, " .. name .. "!"
end
print(greet("Alex")) -- Hello, Alex!
print(greet("Sam")) -- Hello, Sam!
Working with the Roblox Engine
Changing a part's color:
local part = game.Workspace.Part
part.BrickColor = BrickColor.new("Bright red")
part.Size = Vector3.new(10, 1, 10)
Detecting when a player joins:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined the game!")
end)
Your First Real System: A Point Counter
Put this in a Script inside ServerScriptService:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats
while player.Parent do
task.wait(5)
points.Value = points.Value + 1
end
end)
Press Play. A leaderboard appears in the top right and your points go up every 5 seconds.
What to Learn Next
- RemoteEvents — communicate between server and client
- DataStoreService — save player data between sessions
- TweenService — smooth animations
- ModuleScripts — organise code into reusable files
Browse Roblox scripts on ModForge to study how real systems are structured.