Codectionary / Developer documentation / Lua

Lua 5.4 const locals

Lua 5.4 <const> prevents reassignment of a local binding. It does not freeze a referenced table. This attribute syntax is not Luau syntax.

Syntax

local settings <const> = { retries = 3 }

Examples

Lua 5.4 const locals

Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.

local settings <const> = {retries = 3}
settings.retries = 4
print(settings.retries)
-- settings = {} -- compile error: assigning a const variable
  1. Output: 4. The table remains mutable; uncommenting reassignment causes a compilation error.

Best practices

  • Use const for binding invariants rather than deep immutability. Mark Lua 5.4 requirements when sharing scripts.

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