Roblox Studio Beta Updates Luau with Exported Values and Cyclic Requires

Roblox is rolling out a significant update to its scripting language, Luau. The new Studio Beta features simplify module creation and remove a long-standing error regarding circular dependencies.

THE SHORT ANSWER

The update introduces the `export` keyword to remove boilerplate code when defining modules and allows modules to mutually require each other, resolving previous cyclic dependency errors.

  • The `export` keyword simplifies module scripts by removing the need to manually build and return a table.
  • Exported modules are automatically frozen after loading, preventing reassignment of exported constants and functions.
  • Cyclic requires are now supported, allowing modules to depend on one another in a circular pattern.
  • Existing code and modules that manually return tables will continue to function without modification.

Roblox Studio Beta is introducing two major features to the Luau scripting language: exported values and cyclic requires. These updates aim to simplify module creation and resolve a common error regarding circular dependencies between scripts.

Simplifying Modules with the export Keyword

The most visible change is the new export syntax. Previously, creating a reusable module required defining a local table, populating it with functions and variables, and then returning that table. This process added boilerplate code to every module script.

With the new feature, developers can mark top-level variables, constants, functions, and types directly with the export keyword. Luau automatically collects these items into a table that is returned when the module is required. This approach mirrors the existing export type syntax but extends it to functions and values.

The update maintains backward compatibility. Modules that continue to manually return a table will function exactly as before. Additionally, modules that only use export type remain unaffected.

Module Freezing and Limitations

A critical technical detail in this update is how exported modules behave after loading. The official announcement notes that an exported module is conceptually frozen using table.freeze immediately after its code executes. This means that exported constants and functions cannot be reassigned after the module finishes loading.

This freezing applies to the returned table, not the internal state of functions within the module. A module can maintain mutable state in local variables and expose functions to interact with that state. However, attempting to modify the exported table itself after loading will raise an error.

There are specific limitations to keep in mind. The export keyword can only be used at the top level of a module script. Developers cannot combine exported values with a top-level return statement. This restriction applies to returns inside top-level conditional blocks, though returns inside functions are permitted. Duplicate exported names are also not allowed within a single module.

Resolving Cyclic Dependencies

Luau previously prevented modules from requiring each other in a circular pattern. Attempting to have Module A require Module B and Module B require Module A resulted in a cyclic dependency error.

This update lifts that restriction. Modules can now mutually depend on one another. This feature works in tandem with exported values, allowing for complex dependency graphs without the need for workarounds. The announcement clarifies that modules using the new export syntax can participate in these cycles, something that was previously impossible.

KEEP READING

More from this desk