Codectionary / Developer documentation / Lua

Create a .lua file, run it with a local interpreter, understand variables, concatenation and functions, and fix common first-run errors.

Create a project folder

Install a Lua interpreter first, then make a folder named lua-first and open it in a plain-text editor. Create hello.lua inside that folder. The .lua extension helps tools recognise the language; it does not by itself execute the program. Open a terminal in the same folder and use lua -v to confirm the interpreter is available.

The code examples on this page use ordinary Lua features. If your operating system supplies a versioned command such as lua5.4, substitute it consistently in the run command.

  1. Create the lua-first folder.
  2. Create hello.lua inside it and paste the first example.
  3. Save the file as plain text; check it is not hello.lua.txt.
  4. Open a terminal in lua-first and check the interpreter version.

Check Lua

lua -v

Understand the greeting

local name = "Codectionary" binds a string to a local variable. The equals sign assigns the value. The quotation marks delimit text and are not part of the printed result. Lua does not require a semicolon after each of these statements.

The next line joins three strings with the concatenation operator .. and passes the joined result to print. The parentheses contain the argument to print. The terminal should display Hello, Codectionary! on one line. Change the name, save, and rerun to see how the pieces work together.

Run the saved file

In the folder containing hello.lua, run lua hello.lua. The first word starts the installed Lua interpreter; the second names the source file to load. Lua executes the statements in order and writes print output to that terminal. The filename can be a relative path if the file is elsewhere.

An ordinary Lua file does not run by double-clicking it in a browser. Codectionary’s Lua Playground is an editor and does not execute this script. Run it with a local interpreter to see output or errors.

Execute the first program

lua hello.lua

Add a reusable calculation

Replace the greeting with the second example. The function named total receives price and quantity as parameters. return sends their product back to its caller. total(4, 3) passes concrete numbers, and print displays 12. The text after -- is a comment for readers; Lua ignores it.

A function is useful when the same calculation will be called with different values. Try total(7, 2), predict the result, save the file, and run it again. This is a small model of a price calculation, although real money-handling code needs additional care for currency and rounding.

Fix common mistakes

If lua is not recognised, the interpreter is missing or not on PATH; reopen the terminal after setup. If Lua says it cannot open hello.lua, check the current folder and exact filename. A syntax error often points to a missing quotation mark, parenthesis or end; inspect the reported line and the one before it.

If the program runs but prints an unexpected value, use print on a variable or intermediate calculation to see where it changes. Read the error text before changing several lines at once.

What to learn next

You now have a saved program and a repeatable run command. Learn conditionals and loops, then tables and modules. If you intend to build Roblox experiences, continue with Luau and learn which APIs Studio supplies rather than assuming they are part of standalone Lua.

Syntax

local name = "Codectionary"
print("Hello, " .. name .. "!")

Examples

Hello, Codectionary!

Save in hello.lua and run lua hello.lua.

local name = "Codectionary"
print("Hello, " .. name .. "!")
  1. local creates a variable and the quoted word supplies its string value.
  2. .. joins the greeting and name. print writes the joined message to the terminal.
  3. Expected output is Hello, Codectionary!. Changing the value of name changes the output after saving and rerunning.

Calculate a total with a function

Replace the file contents and run it again.

local function total(price, quantity)
    return price * quantity
end

print(total(4, 3)) -- 12
  1. The function receives price and quantity, multiplies them with *, and returns the number.
  2. The call total(4, 3) gives 12 to print, so the terminal shows 12. -- begins a comment; it is not part of the calculation.
  3. The same function can be reused with different inputs in a larger script.

Best practices

  • Save the file before rerunning it.
  • Read error locations and change one cause at a time.
  • Use local variables for values that do not need to be global.

Runtime & versions

Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Lua reference manuals

Specifications & further reading