Comments and Style
Prerequisites: Functions
Comments
-- A single-line comment
local taxRate = 0.2 -- after code
--[[
A block comment
spanning several lines
]]
---Returns the price including tax.
---@param price number the net price
---@return number
local function withTax(price)
return price * (1 + taxRate)
end
print(withTax(100))
120.0
Comments starting with --- and @param/@return tags are LuaCATS
annotations: lua-language-server uses them for autocompletion, hover
documentation, and type checking.
Style guidelines
There's no single official style guide, but common conventions (as in the LuaRocks and Olivine Labs guides) are:
localfor everything; avoid globals.- Two-space indentation (four is also common).
snake_caseorcamelCasefor variables and functions — consistently;PascalCasefor classes and modules that act like classes.UPPER_CASEfor constants._for values you ignore.- Double quotes for strings (single quotes when the text contains double quotes).
- Keep functions small; return early instead of nesting
ifs.
Tools
- StyLua — formats code automatically.
- luacheck — warns about globals, unused variables, and shadowing.
- lua-language-server — errors and type hints in your editor.
What's next
You now know the core of Lua. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with closures.