--- name: roblox-architecture description: "Use when fixing or reviewing where Roblox code runs and loads: RunContext, script containers, startup order, require, Actors, packages, teleports." when_to_use: "Also when a script never runs or runs twice, client code starts before things exist, a module's state isn't shared, require hangs, a join handler misses players, boot dies silently, packages don't update, or teleports, reserved or VIP servers misroute." last_reviewed: 2026-11-29 sources: - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/scripting/locations.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/enums/RunContext.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/scripting/attributes.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/StarterCharacterScripts.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/ModuleScript.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/scripting/module.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/Actor.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/Player.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/ScriptContext.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/CollectionService.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/datatypes/Collection.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/projects/server-authority/index.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/projects/assets/packages.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/projects/teleport.md - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/TeleportOptions.yaml - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/reference/engine/classes/TeleportService.yaml - https://devforum.roblox.com/t/teleports-to-private-vip-servers-are-now-allowed/4963250 - original --- # Roblox Architecture ## When to Load Engine facts that decide where code runs, when it starts, what a `require` shares, and how places and servers connect. Remotes and replication payloads: `roblox-networking`; Parallel Luau cost and `SharedTable`: `roblox-performance`; teleport behavior in Studio tests: `roblox-debugging`; saving across a teleport: `roblox-data`. "Field case" = seen in the owner's shipped games; re-test in your place. ## Traps (symptom -> cause -> fix) - **Script never runs** -> a `Script` defaults to `RunContext.Legacy`: server only, and only in a server container (`ServerScriptService`, `Workspace`). In `ReplicatedStorage` it needs `RunContext = Client` or `Server`. `LocalScript` can't use RunContext. - **`require` hangs or errors at boot** -> a cyclic require: the module guide says it errors (`Requested module was required recursively`), the class reference says the thread hangs with no error. Treat both as the same bug and break the cycle. A module that yields at top level makes every first `require` yield. - **`script:GetActor()` is `nil` in a worker** -> it returns `nil` in a ModuleScript that isn't a descendant of the Actor (a shared module in `ReplicatedStorage`). Pass the Actor in. `require()` can't run in a desynchronized phase: require in serial first. - **ReplicatedFirst loading script errors on `ReplicatedStorage.X`** -> it runs before the rest of the game loads; only `ReplicatedFirst` contents are safe without `WaitForChild`. Order: ReplicatedFirst scripts -> game loads, `game.Loaded` -> PlayerScripts and client Scripts in `ReplicatedStorage` -> character spawns -> character scripts. - **Join handler misses players or runs out of order** -> players who joined before `PlayerAdded` was connected; two services each wiring `PlayerAdded` race. Wire the lifecycle once, call owners in order, loop `Players:GetPlayers()` (§1). Decide admission first (field case). - **Boot dies quietly, no remotes, blank HUD** -> about 10 s of non-yielding boot hit `Script timeout: exhausted allowed execution time`; a `task.spawn` inside `Init` died with it (field case). Yield in long boot loops, `task.defer` optional warm-up. - **Phone freezes ~5 s after joining live, fine in Studio** -> live clients often find everything already replicated, so no `WaitForChild` yields and the whole client start lands in one frame (field case). Repro with `task.wait(8)` at the top of the client bootstrap; pace starts per frame (`roblox-luau` §1). - **Package copies never update in the live game** -> `AutoUpdate` applies **when a place is opened in Studio**; mass update **saves but doesn't publish**. Any edit marks a copy modified and disables its `AutoUpdate`; deleting or moving the `PackageLink` turns the copy into a plain object. Nested packages: only the top parent's `AutoUpdate` counts, and nested changes publish first. - **Teleport lands in the wrong server or a reserved server never fills** -> routing fields conflict or legacy access codes (§2). ## What changed | Change | Use now | |---|---| | `Player:LoadCharacter()` / `LoadCharacterWithHumanoidDescription` tagged Deprecated | `LoadCharacterAsync()` / `LoadCharacterWithHumanoidDescriptionAsync()`; both yield, call from the spawn owner after admission | | `TeleportService:ReserveServerAsync()`, `TeleportOptions.ShouldReserveServer`, `ReservedServerAccessCode` deprecated | `TeleportOptions.ReservedServerId = HttpService:GenerateGUID(false)` | | `CollectionService:CreateCollection(query, root?)` | live CSS-like query (`"Part.KillBrick"`, `[$Attr]`), root defaults to `Workspace`, callbacks per instance. **Keeps running after its variable goes out of scope**: call `:Destroy()` | | Server Authority (`BindToSimulation`) | create instances inside the bound function; `Destroy()` and reparenting an instance already in the DataModel error there: flag with an attribute, destroy in a server `PostSimulation` connection. `Humanoid:EquipTool()`/`UnequipTools()` have no simulation access: weld a cloned `Model` instead | ## 1. One lifecycle owner ```luau -- Illustrative: one entry point owns join/leave, in order. local Players = game:GetService("Players") local function wirePlayers(admit: (Player) -> boolean, owners: { { onJoin: (Player) -> (), onLeave: (Player) -> () } }) local admitted: { [Player]: true } = {} local function join(player: Player) if admitted[player] or not admit(player) then return end admitted[player] = true for _, owner in owners do owner.onJoin(player) end end Players.PlayerAdded:Connect(join) Players.PlayerRemoving:Connect(function(player) if not admitted[player] then return end admitted[player] = nil for i = #owners, 1, -1 do owners[i].onLeave(player) end end) for _, player in Players:GetPlayers() do task.spawn(join, player) end end ``` Admission first (field case): a joiner about to be bounced triggered first-session auto-start and pulled the host into a tutorial. When adding a gate (bots, practice, Studio-only), list every check it switches off: bot-fill checks once disabled bonus rounds in most servers (field case). ## 2. Places, servers and teleports - `TeleportAsync(placeId, players, options?)` is server-only, at most 50 players per call; client `Teleport()` is deprecated. Once in a universe a client can reach any place unless **Access Control for Places** is "Secure within universe only" (`roblox-security`). - `TeleportOptions` routing: set **one** of `ReservedServerId`, `VipServerId`, `ServerInstanceId` (or the deprecated pair); more than one errors. The first teleport with a new `ReservedServerId` creates the server, reusing it routes there: persist the GUID (DataStore, or MemoryStore for short matches). - Keep a VIP party together across places: `VipServerId = game.PrivateServerId`. Inside: `game.PrivateServerId ~= ""` = reserved or VIP; `PrivateServerOwnerId == 0` = developer-reserved. - `SetTeleportData` is client-visible: send an opaque ticket id, keep the authoritative record server-side (MemoryStore with TTL), claim it once on arrival, fail closed. - `pcall` the call and also handle `TeleportService.TeleportInitFailed` (per player, so a group can half-arrive). `Flooded` = back off; `GameFull` = capacity; don't retry `Unauthorized`. Decide a straggler policy. - Social slots (measured, two accounts): friends and players behind one public IP land together; `ServerInstanceId = host JobId` still moves a non-friend past reserved slots (closed JobId -> `GameEnded`). Publishing doesn't change the slot setting. - Open DevForum reports (2026-10, private/VIP teleports): `TeleportAsyncResult.PrivateServerId` held an access code; a full VIP destination fell back to public; non-friends were routed to a public sub-place. Log where players actually land.