AI Generate Lua docs instantly

Lua Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

4 snippets

Basic Lua syntax

Hello World

print("Hello, World!")

Comments

-- Single line comment

--[[
  Multi-line
  comment
--]]

Run Lua

lua script.lua
lua -e 'print("Hello")'  -- One-liner

Semicolons Optional

print("Hello")
print("World")  -- No semicolons needed

Variables & Data Types

6 snippets

Dynamic typing

Variable Declaration

local x = 10      -- Local variable
y = 20            -- Global variable
local name = "Lua"

Nil

local x = nil     -- Nil/null value
if x == nil then
  print("x is nil")
end

Numbers

local int = 42
local float = 3.14
local exp = 1.5e-3

Strings

local s1 = 'single quotes'
local s2 = "double quotes"
local s3 = [[multi-line
string]]

Booleans

local flag = true
local active = false
-- Only nil and false are falsy

Type Checking

print(type(x))    -- "number", "string", etc.
print(type(nil))  -- "nil"

Operators

5 snippets

Arithmetic and logical operations

Arithmetic

local sum = a + b
local diff = a - b
local prod = a * b
local quot = a / b
local mod = a % b
local pow = a ^ b  -- Exponentiation

Comparison

a == b   -- Equal
a ~= b   -- Not equal (note: ~=)
a > b    -- Greater
a < b    -- Less
a >= b   -- Greater or equal
a <= b   -- Less or equal

Logical

a and b  -- Logical AND
a or b   -- Logical OR
not a    -- Logical NOT

String Concatenation

local full = first .. " " .. last
local msg = "Count: " .. tostring(42)

Length Operator

#"hello"      -- 5
#mytable      -- Table length

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Control Flow

4 snippets

Conditional statements

If Statement

if age >= 18 then
  print("Adult")
else
  print("Minor")
end

Elseif

if score >= 90 then
  grade = "A"
elseif score >= 80 then
  grade = "B"
else
  grade = "F"
end

Truthy/Falsy

-- Only nil and false are falsy
if 0 then print("0 is truthy") end
if "" then print("Empty string is truthy") end

Ternary Alternative

-- Lua doesn't have ternary, use:
local result = condition and value1 or value2

Loops

6 snippets

Iteration constructs

While Loop

local i = 1
while i <= 10 do
  print(i)
  i = i + 1
end

Repeat-Until

local i = 1
repeat
  print(i)
  i = i + 1
until i > 10

Numeric For

for i = 1, 10 do
  print(i)
end

for i = 10, 1, -1 do  -- Step of -1
  print(i)
end

Generic For (ipairs)

local arr = {10, 20, 30}
for i, v in ipairs(arr) do
  print(i, v)  -- 1 10, 2 20, 3 30
end

Generic For (pairs)

local t = {a=1, b=2}
for k, v in pairs(t) do
  print(k, v)  -- "a" 1, "b" 2
end

Break

for i = 1, 100 do
  if i == 50 then break end
  print(i)
end

Functions

5 snippets

First-class functions

Function Declaration

function greet(name)
  print("Hello, " .. name)
end

greet("Alice")

Return Values

function add(a, b)
  return a + b
end

local sum = add(5, 3)

Multiple Returns

function stats(a, b)
  return a + b, a - b, a * b
end

local sum, diff, prod = stats(10, 5)

Anonymous Functions

local square = function(x)
  return x * x
end

print(square(5))  -- 25

Variadic Functions

function sum(...)
  local total = 0
  for _, v in ipairs({...}) do
    total = total + v
  end
  return total
end

print(sum(1, 2, 3, 4))  -- 10

Tables

6 snippets

Lua's primary data structure

Array-like Table

local arr = {10, 20, 30}
print(arr[1])  -- 10 (1-indexed!)

Dictionary-like Table

local person = {
  name = "John",
  age = 30
}
print(person.name)  -- "John"
print(person["age"])  -- 30

Mixed Table

local t = {
  10, 20, 30,
  name = "mixed",
  [5] = "five"
}

Table Manipulation

table.insert(arr, 40)  -- Add to end
table.insert(arr, 1, 5)  -- Insert at index
table.remove(arr)  -- Remove last
table.remove(arr, 1)  -- Remove at index

Table Length

local arr = {10, 20, 30}
print(#arr)  -- 3

Iterate Table

for k, v in pairs(tbl) do
  print(k, v)
end

Metatables & OOP

3 snippets

Advanced object-oriented features

Metatable Basics

local t = {}
local mt = {__index = {default = 10}}
setmetatable(t, mt)
print(t.default)  -- 10

Class Pattern

Dog = {}
Dog.__index = Dog

function Dog.new(name)
  local self = setmetatable({}, Dog)
  self.name = name
  return self
end

function Dog:bark()
  print(self.name .. " barks!")
end

local d = Dog.new("Buddy")
d:bark()

Operator Overloading

local mt = {
  __add = function(a, b)
    return a.value + b.value
  end
}
local a = setmetatable({value=5}, mt)
local b = setmetatable({value=3}, mt)
print((a + b))  -- 8

More Cheat Sheets

FAQ

Frequently asked questions

What is a Lua cheat sheet?

A Lua cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Lua. It helps developers quickly look up syntax without searching through documentation.

How do I learn Lua quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Lua concepts?

Key Lua concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Lua code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Lua code using AI.

Code Conversion Tools

Convert Lua to Other Languages

Easily translate your Lua code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Lua Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week