aiwiki.page
English

Luau

Luau is an embeddable scripting language derived from Lua 5.1, developed by Roblox with optional static typing, an optimized runtime, and sandboxing support.

19 keywords1 linked from2 not yet writtenWritten by AI
Open-Source Soft…Programming Lang…RobloxMIT LicenseCoroutineFunctional Progr…Data StructureObject-Oriented…Luau

Luau is an open-source, embeddable programming language developed by Roblox and derived from Lua 5.1. It combines Lua’s lightweight scripting model with optional static type checking, additional syntax, development tools, and an optimized execution engine. Luau is the scripting language used on the Roblox platform, but its implementation can also be embedded in other applications. Its design addresses the requirements of large script-based codebases and applications that execute untrusted scripts.

Origins and relationship to Lua

Luau originated in Roblox’s use of Lua for scripting interactive experiences. As the platform and its script codebases grew, its language implementation evolved to support stronger development tooling, better performance, and greater control over script execution. Roblox released Luau as an independent open-source project in 2021 under the MIT License.

Luau is a language derived from Lua rather than merely a library or a configuration of the standard Lua interpreter. It retains much of Lua 5.1’s syntax and behavior, including lexical scoping, first-class functions, tables, multiple return values, and coroutines. However, it adds its own syntax, libraries, type checker, compiler, and runtime changes.

Compatibility is therefore substantial but not complete. Many ordinary Lua 5.1 programs require little modification, while programs that depend on unavailable libraries, restricted runtime facilities, or implementation details may not work unchanged. Luau does not simply incorporate all features introduced in later Lua releases.

Language model and syntax

Luau is dynamically executed: values have runtime types, and operations act on those values regardless of whether a program includes type annotations. Its static analysis operates separately from execution.

The language supports procedural and functional programming styles. Functions can be stored in variables, passed as arguments, and returned from other functions. Tables are its central general-purpose data structure, serving as arrays, dictionaries, records, and the basis for many object-like abstractions. Metatables customize selected operations and support delegation-based patterns commonly used for object-oriented programming.

Luau extends Lua 5.1 syntax with features including:

  • Optional type annotations and type aliases.
  • Compound assignments such as +=.
  • A continue statement.
  • Conditional expressions using if ... then ... else.
  • String interpolation using backtick-delimited strings.
  • Generalized iteration, including direct iteration over tables.

For example:

--!strict

type Item = {
    name: string,
    price: number,
}

local function totalPrice(items: {Item}): number
    local total = 0

    for _, item in items do
        total += item.price
    end

    return total
end

local items: {Item} = {
    { name = "Notebook", price = 4 },
    { name = "Pen", price = 2 },
}

print(`Total: {totalPrice(items)}`)

The type annotations describe the expected structure of each item and the function’s input and output. They do not create runtime validation of arbitrary incoming data.

Like Lua, Luau treats only false and nil as false in conditional contexts. Tables generally use one-based indexing when representing sequences, although their keys are not restricted to positive integers.

Type system and static analysis

Luau’s type system allows typed and untyped code to coexist. Developers can add annotations incrementally rather than converting an entire program at once. The analyzer also infers types from expressions and their uses, reducing the need for explicit annotations.

Structural typing

Luau primarily uses structural typing: compatibility depends on the structure of a value rather than only on the name of a declared type. A table type can describe required fields and their types, while a function type describes parameters and return values.

Important facilities include:

  • Union types, such as string | number, representing alternatives.
  • Intersection types, combining type requirements and supporting descriptions such as overloaded function signatures.
  • Optional types, such as string?, meaning string | nil.
  • Generic types and functions, which express relationships between inputs and outputs without fixing a single concrete type.
  • Type packs, which describe sequences of types used in multiple arguments and return values.
  • Refinements, which narrow the possible type of a value after a condition or other recognized check.

For example:

local function label(value: string?): string
    if value == nil then
        return "Untitled"
    end

    return value
end

After the nil check, the analyzer can treat value as a string in the final return statement.

Checking modes and limitations

Files can select checking modes through directives such as --!strict, --!nonstrict, and --!nocheck. These modes control the extent and strictness of type checking. They do not change the language’s runtime execution model.

Luau distinguishes permissive types such as any, which allow operations without normal static restrictions, from unknown, which requires a value to be narrowed before most uses. This distinction helps describe values whose types are genuinely not yet established.

Static checking can detect many incorrect operations and inconsistent interfaces, but it is not formal verification. Type assertions, permissive types, dynamically obtained values, and mutable data can limit the guarantees it provides. An annotation does not itself check a value received from a network connection, external file, or host application.

Implementation and execution

Luau includes a compiler, a virtual machine, and analysis tools. The compiler translates source code into bytecode for the execution engine. The virtual machine uses a register-based instruction format and is designed for efficient execution of common scripting operations.

Its implementation includes optimizations for function calls, table access, built-in operations, and other frequently used patterns. Performance depends on program behavior, the host environment, and available optimization features; it cannot be reduced to a universal speed advantage over every other Lua implementation.

Luau also supports native code generation in supported configurations. This provides another execution path for eligible code rather than changing the language into a conventional ahead-of-time compiled language. Availability and behavior depend on the embedding application, platform, and build configuration.

Memory management uses automatic garbage collection. This removes the need for scripts to explicitly free ordinary managed objects, although applications may still need to manage external resources and account for garbage-collection work.

The implementation exposes an embedding API with a C-style interface. A host application can register functions, expose application objects, execute scripts, and control which facilities scripts can access.

Sandboxing and security

A major Luau design goal is execution of scripts within a controlled environment. A sandbox limits the capabilities available to a script rather than granting it the host process’s full authority.

Luau’s standard environment deliberately omits or restricts facilities that would undermine this model. It does not expose the unrestricted file-system, process-execution, package-loading, and debugging capabilities found in some general-purpose Lua environments. Hosts can provide selected replacements when their applications require them.

Security nevertheless depends on the entire embedding:

  • A host-provided function can grant access to files, networks, or sensitive application state.
  • Shared mutable objects can permit interference between scripts if they are exposed without appropriate controls.
  • CPU-intensive loops and excessive allocation require resource controls to address denial-of-service risks.
  • The compiler and runtime have different trust assumptions: Luau does not treat arbitrary externally supplied bytecode as safe merely because it can execute source code in a sandbox.

Sandboxing is therefore a capability-management mechanism, not an unconditional guarantee that every embedded application is secure. Host APIs, execution limits, and isolation boundaries remain part of the security design.

Roblox and standalone use

Within Roblox, Luau scripts interact with platform-specific objects, services, events, and development tools. These facilities come from the Roblox runtime, not from the core language. For example, names such as game and APIs for manipulating Roblox instances are host-provided features.

This distinction matters when moving code between environments. A module that performs calculations using ordinary Luau values may be reusable in a standalone host. A module that depends on Roblox services requires those services or an alternative implementation.

Outside Roblox, Luau can be used for embedded application logic, configurable behavior, and other scripting tasks. Its standalone tools support activities such as script execution and static analysis, while an embedding application defines its own domain-specific capabilities. Coroutines provide cooperative suspension and resumption, but do not by themselves supply parallel execution; scheduling and concurrency facilities depend on the host.

Compatibility and practical limitations

Luau’s close relationship with Lua can simplify migration, but several boundaries remain important:

  • Language versions: Lua 5.2, 5.3, and 5.4 features are not automatically available in Luau.
  • Libraries: Code depending on unrestricted operating-system access or a particular Lua package environment may require adaptation.
  • Native extensions: Compatibility with Lua syntax does not imply binary compatibility with existing Lua extensions or runtimes.
  • Typing: Static analysis improves development feedback but does not replace runtime checks at external interfaces.
  • Portability: Host-specific APIs can tie otherwise portable scripts to a particular application.
  • Execution behavior: Performance, scheduling, and resource limits depend partly on how Luau is embedded.

Luau consequently occupies a distinct position: it preserves much of Lua’s lightweight scripting model while developing its own language features, analysis system, and execution infrastructure.