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 从公开仓库中收录这些内容。

举报或申请下架