Lua Guard

Your comprehensive platform for learning and developing with Lua and Luau

Interactive Documentation

Learn Lua and Luau with comprehensive guides, syntax references, and best practices.

Developer Tools

Professional tools for Lua development including obfuscators, formatters, and analyzers.

Practical Examples

Explore real-world coding examples and learn from practical implementations.

Real-time Processing

Process your Lua code instantly with our integrated development tools.

Introduction to Lua

Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

What is Lua?

Lua is designed, implemented, and maintained by a team at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil. Lua was born and raised in academia but has gradually moved toward industrial applications.

Key Features

  • Lightweight: Small memory footprint
  • Fast: Excellent performance for a scripting language
  • Portable: Runs on virtually any platform
  • Embeddable: Easy to integrate with other languages
  • Simple: Clean syntax and powerful semantics

Hello World

print("Hello, World!")

Basic Syntax

Lua has a simple and clean syntax. Here are the fundamental elements:

Comments

-- This is a single-line comment --[[ This is a multi-line comment --]]

Statements

Statements in Lua don't need semicolons, but you can use them optionally:

print("Hello") print("World"); print("!") -- Multiple statements on one line

Case Sensitivity

Lua is case-sensitive. print and Print are different identifiers.

Keywords

Lua has the following reserved keywords:

and break do else elseif end false for function goto if in local nil not or repeat return then true until while

Variables & Types

Variable Declaration

Variables in Lua are global by default. Use local for local variables:

-- Global variables name = "Alice" age = 25 -- Local variables local city = "New York" local temperature = 72.5

Data Types

Lua has eight basic types:

-- nil local nothing = nil -- boolean local isTrue = true local isFalse = false -- number local integer = 42 local float = 3.14 -- string local text = "Hello, Lua!" local multiline = [[ This is a multiline string ]] -- function local function greet() return "Hello!" end -- userdata (C data) -- thread (coroutines) -- table local array = {1, 2, 3, 4, 5} local object = {name = "John", age = 30}

Type Checking

local value = 42 print(type(value)) -- "number" value = "Hello" print(type(value)) -- "string"

Functions

Function Declaration

-- Basic function function sayHello(name) print("Hello, " .. name .. "!") end -- Local function local function add(a, b) return a + b end -- Anonymous function local multiply = function(a, b) return a * b end

Multiple Return Values

function getCoordinates() return 10, 20 end local x, y = getCoordinates() print(x, y) -- 10 20

Variable Arguments

function sum(...) local args = {...} local total = 0 for i = 1, #args do total = total + args[i] end return total end print(sum(1, 2, 3, 4, 5)) -- 15

Closures

function createCounter() local count = 0 return function() count = count + 1 return count end end local counter = createCounter() print(counter()) -- 1 print(counter()) -- 2

Tables

Tables are Lua's only data structuring mechanism. They can represent arrays, sets, records, graphs, trees, etc.

Array-like Tables

-- Array (1-indexed) local fruits = {"apple", "banana", "orange"} print(fruits[1]) -- "apple" print(#fruits) -- 3 (length)

Dictionary-like Tables

-- Dictionary/Hash table local person = { name = "Alice", age = 30, city = "Boston" } print(person.name) -- "Alice" print(person["age"]) -- 30

Mixed Tables

local mixed = { "first", -- [1] "second", -- [2] name = "Mixed", -- ["name"] count = 100 -- ["count"] } print(mixed[1]) -- "first" print(mixed.name) -- "Mixed"

Table Operations

local t = {} t.x = 10 -- Add field t["y"] = 20 -- Add field table.insert(t, "end") -- Add to end table.remove(t, 1) -- Remove by index -- Iterate over table for key, value in pairs(t) do print(key, value) end

Control Flow

Conditional Statements

-- if-then-else local age = 18 if age >= 18 then print("Adult") elseif age >= 13 then print("Teenager") else print("Child") end

Loops

-- while loop local i = 1 while i <= 5 do print(i) i = i + 1 end -- repeat-until loop local j = 1 repeat print(j) j = j + 1 until j > 5 -- for loop (numeric) for k = 1, 5 do print(k) end -- for loop with step for m = 1, 10, 2 do print(m) -- 1, 3, 5, 7, 9 end -- for loop (generic) local fruits = {"apple", "banana", "orange"} for index, fruit in ipairs(fruits) do print(index, fruit) end

Break and Return

for i = 1, 10 do if i == 5 then break -- Exit loop end print(i) end function findValue(table, target) for i, value in ipairs(table) do if value == target then return i -- Exit function early end end return nil end

Strings

String Creation

local str1 = "Hello" local str2 = 'World' local str3 = [[Multi-line string content]]

String Concatenation

local greeting = "Hello" .. " " .. "World" print(greeting) -- "Hello World"

String Length

local text = "Lua programming" print(#text) -- 15 print(string.len(text)) -- 15

String Functions

local text = "Hello, Lua!" -- Convert case print(string.upper(text)) -- "HELLO, LUA!" print(string.lower(text)) -- "hello, lua!" -- Substring print(string.sub(text, 1, 5)) -- "Hello" -- Find pattern local pos = string.find(text, "Lua") print(pos) -- 8 -- Replace local newText = string.gsub(text, "Lua", "World") print(newText) -- "Hello, World!"

String Formatting

local name = "Alice" local age = 25 local formatted = string.format("Name: %s, Age: %d", name, age) print(formatted) -- "Name: Alice, Age: 25"

Modules

Creating Modules

-- math_utils.lua local M = {} function M.add(a, b) return a + b end function M.multiply(a, b) return a * b end M.PI = 3.14159 return M

Using Modules

-- main.lua local math_utils = require("math_utils") print(math_utils.add(5, 3)) -- 8 print(math_utils.multiply(4, 7)) -- 28 print(math_utils.PI) -- 3.14159

Global vs Local Modules

-- Global approach (not recommended) math_utils = {} function math_utils.add(a, b) return a + b end -- Local approach (recommended) local math_utils = {} local function add(a, b) return a + b end math_utils.add = add return math_utils

Luau Features

Luau is Roblox's version of Lua with additional features and optimizations.

Type Annotations

-- Function with type annotations local function add(a: number, b: number): number return a + b end -- Variable type annotations local name: string = "Alice" local age: number = 25 local isStudent: boolean = true

String Interpolation

local name = "World" local greeting = `Hello, {name}!` print(greeting) -- "Hello, World!" local x, y = 10, 20 local coords = `Position: ({x}, {y})` print(coords) -- "Position: (10, 20)"

Generics

-- Generic function local function identity(value: T): T return value end local numberResult = identity(42) -- number local stringResult = identity("hello") -- string

Improved typeof

local value = 42 print(typeof(value)) -- "number" (more precise than type()) local instance = Instance.new("Part") print(typeof(instance)) -- "Instance"

Continue Statement

for i = 1, 10 do if i % 2 == 0 then continue -- Skip even numbers end print(i) -- Only prints odd numbers end

Compound Assignment

local x = 10 x += 5 -- equivalent to x = x + 5 x -= 3 -- equivalent to x = x - 3 x *= 2 -- equivalent to x = x * 2 x /= 4 -- equivalent to x = x / 4

Lua Quiz

Test your knowledge of Lua with this interactive quiz. Select the correct answer for each question.

What is the correct way to declare a local variable in Lua?
What is the output of: print(#{"apple", "banana", "orange"})?
Which operator is used for string concatenation in Lua?

Lua Development Tools

Lua Obfuscator
Obfuscate your Lua code to protect it from reverse engineering and unauthorized access.
Code Formatter
Format and beautify your Lua code with proper indentation, spacing, and structure for better readability.
Code Minifier
Minimize your Lua code by removing unnecessary whitespace, comments, and shortening variable names to reduce file size.
Syntax Validator
Check your Lua code for syntax errors, common mistakes, and potential issues before running it.
Code Analyzer
Analyze your Lua code for complexity, performance issues, and coding standards compliance.
Base64 Decoder
Decode Base64 encoded Lua scripts and strings back to readable format.
Debug Helper
Add debug statements and logging to your Lua code to help with troubleshooting and development.
Lua Playground
Execute Lua code directly in your browser with a live interactive environment.
JSON to Lua Converter
Convert JSON data structures to equivalent Lua table format.
Bytecode Viewer
View and analyze the bytecode generated from your Lua source code.
Lua to Luau Converter
Convert standard Lua code to Luau (Roblox) syntax with type annotations.
Snippet Generator
Generate reusable code snippets for common Lua programming patterns.
Lua Obfuscator - Input Code
Obfuscated Output
Obfuscated code will appear here...
Code Formatter - Input Code
Formatted Output
Formatted code will appear here...
Code Minifier - Input Code
Minified Output
Minified code will appear here...
Syntax Validator - Input Code
Validation Results
Validation results will appear here...
Code Analyzer - Input Code
Analysis Results
Code analysis results will appear here...
Base64 Decoder - Input
Decoded Output
Decoded content will appear here...
Debug Helper - Input Code
Debug-Enhanced Code
Code with debug statements will appear here...
Lua Playground
Output will appear here...
Playground Information

About the Lua Playground

The Lua Playground allows you to execute Lua code directly in your browser. This is a great way to experiment with Lua syntax and test small code snippets.

Features:

  • Real-time code execution
  • Syntax highlighting
  • Error reporting
  • Output display

Limitations:

  • No file I/O operations
  • No network access
  • Limited standard library
  • Execution timeout for safety

Example Code:

-- Simple function function greet(name) return "Hello, " .. name .. "!" end -- Call the function print(greet("World")) -- Loop example for i = 1, 5 do print("Count: " .. i) end
JSON to Lua Converter - Input JSON
Lua Table Output
Lua table will appear here...
Bytecode Viewer - Input Code
Bytecode Output
Bytecode will appear here...
Lua to Luau Converter - Input Code
Luau Output
Luau code will appear here...
Snippet Generator
Generated Snippet
Select a snippet type and click Generate...

Lua Code Examples

Hello World
A simple introduction to Lua programming.
print("Hello, World!") print("Welcome to Lua programming!")
Variables and Types
Working with different data types in Lua.
local name = "Alice" local age = 25 local height = 5.6 local isStudent = true print("Name:", name, "Type:", type(name)) print("Age:", age, "Type:", type(age)) print("Height:", height, "Type:", type(height)) print("Student:", isStudent, "Type:", type(isStudent))
Functions
Creating and using functions with parameters and return values.
function add(a, b) return a + b end local function factorial(n) if n <= 1 then return 1 else return n * factorial(n - 1) end end print("5 + 3 =", add(5, 3)) print("5! =", factorial(5))
Tables and Arrays
Working with Lua's versatile table data structure.
-- Array-like table local fruits = {"apple", "banana", "orange", "grape"} print("Fruits:") for i = 1, #fruits do print(i .. ":", fruits[i]) end -- Dictionary-like table local person = { name = "John", age = 30, city = "New York" } print("\nPerson info:") for key, value in pairs(person) do print(key .. ":", value) end
Error-Prone Code
Code with syntax errors for testing the validator.
function broken_function() print("Missing closing parenthesis" if true then print("Missing end statement" local x = 5 + return x end
Base64 Example
Base64 encoded Lua script for decoding.
bG9jYWwgZnVuY3Rpb24gc2F5SGVsbG8obmFtZSkKICAgIHByaW50KCJIZWxsbywgIiAuLiBuYW1lIC4uICIhIikKZW5kCgpzYXlIZWxsbygid29ybGQiKQ==
Complex Algorithm
A complex function that would benefit from debug statements.
local function quicksort(arr, low, high) if low < high then local pi = partition(arr, low, high) quicksort(arr, low, pi - 1) quicksort(arr, pi + 1, high) end end local function partition(arr, low, high) local pivot = arr[high] local i = low - 1 for j = low, high - 1 do if arr[j] <= pivot then i = i + 1 arr[i], arr[j] = arr[j], arr[i] end end arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 end
Module Example
Creating and using a simple module.
-- mymodule.lua local M = {} function M.greet(name) return "Hello, " .. name .. "!" end function M.calculate(a, b) return a + b, a * b end return M -- main.lua local mymodule = require("mymodule") print(mymodule.greet("Alice")) local sum, product = mymodule.calculate(5, 3) print("Sum:", sum, "Product:", product)
Playground Example
Try this example in the Lua Playground.
-- Calculate factorial function factorial(n) if n <= 1 then return 1 else return n * factorial(n - 1) end end -- Test the function for i = 1, 10 do print(i .. "! =", factorial(i)) end

Settings

Appearance

Dark Mode

Monaco Editor Settings

Editor Theme
Font Size
14px
Font Family
Line Numbers
Minimap
Word Wrap