Complete Lua Programming Guide (Beginner to Advanced)
What is Lua? Lua is a lightweight, high-level, multi-paradigm programming
language designed for embedded use in applications. It’s widely used in
game development (like Roblox), IoT devices, and configuration scripting.
1. Getting Started
Installation: Download from https://www.lua.org/download.html
Editors: Use VS Code (with Lua extension), ZeroBrane Studio, or
Notepad++
Try Online: replit.com, tutorialspoint.com
2. Lua Basics
Hello World
print("Hello, World!")
Variables
local name = "Lua"
local age = 10
Data Types: nil, boolean, number, string, table, function, userdata,
thread
Operators: +, -, *, /, %, ^, .., ==, ~=, >, <, >=, <=, and, or, not
3. Control Structures
If Statements
if condition then
elseif condition then
else
end
Loops
for i = 1, 10 do print(i) end
while condition do ... end
repeat ... until condition
Break and Goto
for i = 1, 5 do
if i == 3 then break end
end
4. Functions
function add(a, b)
return a + b
end
Anonymous and higher-order functions
Closures and lexical scoping
5. Tables (Associative arrays, the only data structure in Lua)
local t = {name = "Lua", age = 28}
print(t.name)
Arrays, dictionaries, and object representation
Metatables and metamethods (__index, __add, __tostring)
6. Advanced Features
Metatables & Metamethods
local mt = {
__add = function(a, b)
return a + b
end
}
setmetatable(t1, mt)
Coroutines
co = coroutine.create(function()
print("Hi")
end)
coroutine.resume(co)
Error Handling
local status, err = pcall(function()
error("Something went wrong")
end)
Modules & Packages
-- mymodule.lua
local M = {}
function M.hello() print("Hello") end
return M
7. File I/O
local file = io.open("test.txt", "w")
file:write("Hello Lua")
file:close()
Reading and writing files
Modes: r, w, a, rb, etc.
8. Object-Oriented Programming (OOP) in Lua
Person = {}
Person.__index = Person
function Person:new(name)
return setmetatable({name = name}, Person)
end
function Person:greet()
print("Hi, " .. self.name)
end
9. Debugging and Tools
Use debug library
Print logs and watch variables
Use IDE breakpoints
10. Lua in Practice (Projects)
Text RPG Game
Inventory System
Event Handler Framework
Save/Load Data in Files
11. Lua in Roblox (Luau)
Roblox uses a custom dialect of Lua called Luau
Basic Script in Roblox
print("Game Started")
Player GUI Buttons
script.Parent.MouseButton1Click:Connect(function()
print("Button Clicked")
end)
Kill Brick
script.Parent.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then hum.Health = 0 end
end)
Checkpoint System
local checkpoint = script.Parent
checkpoint.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
player.RespawnLocation = checkpoint
end
end)
Player Stats
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = stats
12. Learning Resources
Lua Manual
Learn X in Y
Roblox Developer Hub
YouTube Channels: TheDevKing, AlvinBlox
13. Practice Ideas
Create a calculator, stopwatch, quiz app
Build a 2D game using Love2D
Make Roblox games with leaderboard, teleporters, quests
14. Tips
Comment your code
Write modular code (split into reusable scripts)
Always test small parts first
Keep Coding, Keep Learning! Lua is small but mighty. You’ll master it
with practice and by building real projects.