Modern strict Luau coding standard focused on proper use of `const` and `local` according to each variable's role, paired with `table.freeze`, `const function`, `--!strict`, and the `task` library. Use whenever writing, refactoring, or explaining Roblox Luau scripts.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of roblox-luau-const?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jamorant404-roblox-luau-const)
---
name: roblox-luau-const
description: >-
Modern strict Luau coding standard focused on proper use of `const` and `local` according to
each variable's role, paired with `table.freeze`, `const function`, `--!strict`, and the `task`
library. Use whenever writing, refactoring, or explaining Roblox Luau scripts.
---
# Modern Luau: `const` vs `local` Standard
Write every Roblox Luau script with `--!strict` and choose intentionally between **`const`** and **`local`** based on whether the binding is immutable or mutable.
## 1. Definitions: `const` vs `local`
| Keyword | Meaning | Reassignment Allowed? | Best Used For |
| --- | --- | --- | --- |
| `const` | **Immutable local binding** (contextual keyword in modern Luau) | **No** — reassigning throws a compile/type error | Services, `require()` modules, static instances, frozen configs, constants, helper functions |
| `local` | **Mutable local variable** | **Yes** — value can be reassigned (`x = ...`, `x += 1`) | State flags (`isOpen`), counters, timers, nullable handles initialized later (`local conn`), accumulators |
### Critical Rule: Binding Immutability vs Value Immutability
`const` freezes the **variable binding**, **not** the contents of a mutable table.
To make a configuration or lookup table truly read-only, always combine `const` with `table.freeze(...)`:
```luau
--!strict
-- Binding cannot be reassigned, AND table fields cannot be mutated:
const SHOP_CONFIG = table.freeze({
MaxItemsPerPage = 12,
CurrencyName = "Coins",
})
-- WRONG if you intended immutability (fields could still be changed):
-- const MUTABLE_CONFIG = { MaxItemsPerPage = 12 }
```
*(Note: Runtime state registries such as `const activePlayers: { [Player]: number } = {}` intentionally use `const` when the table reference itself is never replaced, even though keys inside the table are added/removed.)*
---
## 2. When to Use `const`
Use `const` by default for everything that is never rebound:
```luau
--!strict
-- 1. Services
const Players = game:GetService("Players")
const ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 2. Required modules
const Modules = ReplicatedStorage:WaitForChild("Modules")
const AssetConfig = require(Modules:WaitForChild("AssetConfig") :: ModuleScript)
-- 3. Constants and frozen configurations
const MAX_RETRY_ATTEMPTS: number = 3
const DEFAULT_WALK_SPEED: number = 16
const RARITY_WEIGHTS = table.freeze({
Common = 70,
Rare = 25,
Legendary = 5,
})
-- 4. Pure or helper functions
const function clampCoins(amount: number): number
return math.max(0, math.floor(amount))
end
```
---
## 3. When to Use `local`
Use `local` **only** when the binding genuinely needs to change over time:
```luau
--!strict
-- 1. Mutable UI / gameplay state
local isMenuOpen: boolean = false
local activeMenuName: string? = nil
-- 2. Counters, accumulators, and retry backoffs
local totalScore: number = 0
local retryDelay: number = 1
-- 3. Late-initialized or replaceable references
local activeConnection: RBXScriptConnection? = nil
local pendingThread: thread? = nil
-- Example mutation
totalScore += 10
isMenuOpen = not isMenuOpen
activeMenuName = if isMenuOpen then "Shop" else nil
```
---
## 4. Complete Example (`const` + `local` Working Together)
```luau
--!strict
const ReplicatedStorage = game:GetService("ReplicatedStorage")
const TweenService = game:GetService("TweenService")
const ANIMATION_INFO = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
const SHOP_FRAME_NAME: string = "Shop"
local currentFrame: GuiObject? = nil
local isShopOpen: boolean = false
local openCount: number = 0
const function setFrameVisible(frame: GuiObject, visible: boolean): ()
frame.Visible = visible
const targetScale = if visible then 1 else 0.95
const uiScale = frame:FindFirstChildOfClass("UIScale")
if uiScale then
const tween = TweenService:Create(uiScale, ANIMATION_INFO, { Scale = targetScale })
tween:Play()
end
end
const ShopController = {}
function ShopController.Init(centerRoot: Instance): ()
const found = centerRoot:FindFirstChild(SHOP_FRAME_NAME)
if found and found:IsA("GuiObject") then
currentFrame = found
found.Visible = false
end
end
function ShopController.Toggle(): ()
const frame = currentFrame
if not frame then
return
end
isShopOpen = not isShopOpen
if isShopOpen then
openCount += 1
end
setFrameVisible(frame, isShopOpen)
end
return ShopController
```
---
## 5. Mandatory Engine Best Practices
- **Strict mode:** Begin modules and scripts with `--!strict`.
- **Task library:** Always use `task.wait`, `task.spawn`, `task.defer`, `task.delay`, `task.cancel` (never `wait`, `spawn`, `delay`).
- **Services:** Always fetch via `game:GetService("ServiceName")` using `const`.
- **Safe instance creation:** Set properties before setting `.Parent` last.
- **Error boundaries:** Wrap yielding Roblox `*Async` / network calls in `pcall` with `const ok, result = pcall(...)`.
- **Cleanup:** Disconnect `RBXScriptConnection`s and clear player keys on `Players.PlayerRemoving` to prevent memory leaks.
See [references/local-vs-const.md](./references/local-vs-const.md) for a detailed Bahasa Indonesia & English comparison, edge cases, and migration checklist.