Roblox Luau Patterns

TabooHarmony/roblox-brain/skills/core/roblox-luau-patterns

作者 TabooHarmony38826be57ee37bcf023e9c2b85681bea3909281c無授權條款收錄於 2026年10月9日更新於 2026年10月9日

Use for Roblox module boundaries, object lifecycles, signals, task scheduling, fallible calls, and cleanup in Luau.

AI 產生的概覽

提供 Roblox 中 Luau 撰寫模式的指引:模組形態、實體歸屬、訊號、任務排程與清理。

功能
此技能為在 Roblox 專案中撰寫 Luau 程式碼提供參考指引。內容涵蓋選擇模組形態、讓實體與連線的歸屬關係清楚可見、以 pcall 保留失敗語意,以及使用 task 函式安排任務。它也包含一份審查清單,並指向更完整的參考文件。
適用情境
適用於決定模組形態、管理 Roblox 實體或事件連線、安排非同步任務,或處理 Luau 中可能失敗的呼叫時。它面向 Roblox 開發,而非通用 Luau 語言行為或專案整體架構。
執行需求
不需要指令碼或工具,僅為說明與參考文件。它假定處於 Roblox Luau 開發環境。

Luau Patterns

When to Load

Load when choosing a module shape, owning Roblox instances or event connections, scheduling work, or preserving failure semantics. Use roblox-luau-core for language behavior, roblox-luau-types for types, and roblox-architecture for project-wide ownership and startup.

Quick Reference

Choose the smallest shape

  • Plain functions: default for stateless transformation or validation.
  • Module with private state: one explicit subsystem owner. Do not create a manager class merely to namespace functions.
  • Object with metatable: multiple independent values need shared behavior and lifecycle.
  • Existing library abstraction: follow it when the project already uses it consistently. Do not add Promise, signal, cleanup, or framework dependencies for one call site.

Constructors use ., instance methods use :, and mutable fields belong on the instance, not the class table.

Make ownership visible

The code that connects a signal, creates an instance, or starts a task owns cleanup. Store connections and cancel or disconnect them when it ends. Type custom signal payloads once (Signal<T...>) and export the alias; see full.md.

Configure an instance before parenting when observers should not see partial state. Parent earlier only when the API or lifecycle requires ancestry, and document that reason. This is visibility control, not a magic replication-race fix.

Preserve failure semantics

luau
local ok, value = pcall(dataStore.GetAsync, dataStore, key)if not ok then    return nil, `read failed: {value}`endreturn value, nil -- value may legitimately be nil

Do not collapse "call succeeded and returned nil" into "call failed." Retry only when the domain operation is safe to repeat. Persistence, HTTP, purchases, and remotes belong to their domain skills.

Schedule deliberately

Use task.defer, task.spawn, task.delay, and task.cancel deliberately. Avoid legacy wait() and unjustified polling. Prefer a real state-change signal; otherwise choose and measure an explicit cadence.

Review

Clear owner, narrow public API, no circular require, no accidental concurrent startup, success and missing-data states remain distinct, connections/tasks cleaned up, client input routed to roblox-networking.

Detailed decision rules and lifecycle examples: references/full.md [blocked]

來源與署名

來源:TabooHarmony/roblox-brain位於skills/core/roblox-luau-patterns提交38826be

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架