Syntax
local Lessons = require(game:GetService("ReplicatedStorage"):WaitForChild("Lessons"))Examples
Define the shared module
Roblox Studio: create a ModuleScript named Lessons in ReplicatedStorage. Its public type describes a record; its returned table exposes the function. This module contains no secrets.
--!strict
export type Lesson = { title: string, minutes: number }
local Lessons = {}
function Lessons.describe(lesson: Lesson): string
return `{lesson.title}: {lesson.minutes} minutes`
end
return LessonsRequire and use it
Script in ServerScriptService, with the Lessons ModuleScript above already present. A LocalScript may also require this shared module, but its runtime state is separate.
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Lessons = require(ReplicatedStorage:WaitForChild("Lessons"))
local lesson: Lessons.Lesson = { title = "Types", minutes = 12 }
print(Lessons.describe(lesson))Best practices
- Keep server-only rules and secrets in server-only containers.
- Avoid cyclic require chains; move shared types or small helpers to a dependency both modules can use.
- Return a constructor when callers need fresh mutable state instead of sharing the module table.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
Script, LocalScript and ModuleScript placement
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.Client/server boundaries and project structure
A server owns shared game decisions while each client handles its own input, camera and presentation. Treat client messages as requests. Put private rules in server containers and expose only the data and operations the client actually needs.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.
The same code can behave differently depending on where it runs. These lessons use server Scripts in ServerScriptService and LocalScripts in StarterPlayerScripts or StarterGui. A ModuleScript runs when required. Script RunContext also affects execution, so inspect it when a script appears silent.Client/server boundaries and project structure
A server owns shared game decisions while each client handles its own input, camera and presentation. Treat client messages as requests. Put private rules in server containers and expose only the data and operations the client actually needs.Closures and captured state
A closure is a function that keeps access to locals from its surrounding scope. Each call to a factory can create independent state. Capturing a value also extends how long it stays reachable, which matters for long-lived callbacks.Luau and Roblox scripting
Start with Luau values and functions, then use Roblox Studio to connect that code to players, Instances and events. Luau is the language; Roblox supplies the game engine APIs. Codectionary supports editing and highlighting, while these Roblox examples run in Studio.