World building in code: MapBuilder, lighting, atmosphere

Build a Roblox map in Luau instead of by hand. Read Beacon Yard's MapBuilder and WorldLook, change the walls, the lighting and the atmosphere, and learn when studios build in code.

Project lesson, about 70 minutes, 11 steps

By the end you can

  • Read MapBuilder.build and name the parts it creates under Workspace.Yard, with their stud sizes
  • Separate the dusk that WorldLook sets at boot from the two Lighting values a drill changes
  • Explain why a part you place in Workspace.Yard by hand disappears on Play, and where to add it instead

Before this: Parts, models, materials and pivots, The data model: every service and where things live

1Press Play and the yard appears

At m05-the-round, where this lesson starts, Explorer still shows a hand-placed Yard folder in edit mode. By m06-drone-wrench, where the BuildAlong takes you, that folder is gone before you press Play. Press Play and it appears: a 96 by 96 stud slab, walls, two gates, six glowing pads, a beacon and a shop. A script built all of it in the first fraction of a second.

Figure 1.1.

An orbit of the yard during Play. Nothing in this clip was placed by hand.

Transcript
  1. 0:00 Play builds Workspace.Yard from MapBuilder.
  2. 0:02 The base is 96 studs across. The walls are 10 studs tall.
  3. 0:05 Six pads sit on a circle of radius 22.
  4. 0:08 The north gap is 16 studs, closed by two gates until a drill starts.

That script is MapBuilder.build, in src/server/MapBuilder.luau. Up to m05 the yard lived in default.project.json, part by part. At m06 it moved into code, because a loop can place six pads on a circle and a helper can make four signs that match.

From m06 on, where does Workspace.Yard come from?

  1. MapBuilder.build creates it on the server when Play starts
  2. It is saved in the place file and loaded with the game
  3. Each client builds its own copy in a LocalScript
  4. Rojo copies it from default.project.json
Answer

MapBuilder.build creates it on the server when Play starts. The server script calls MapBuilder.build at boot. It creates the Yard folder and every part inside it, and replication sends them to each client.

Open this step in lesson mode · Link to this step · This confused me

2Folders in a cabinet

Everything in a Roblox game is an InstanceInstanceAny object in a Roblox game: a part, a script, a folder, a sound.Think of it as a folder or file in a filing cabinet., and every instance has a parent. Explorer draws that as a tree. MapBuilder.build first deletes any old Workspace.Yard, then makes a new Folder named Yard and parents each part to it.

The game is a filing cabinet. Services such as Workspace and Lighting are drawers, and folders and models hold the parts. A script finds a part the way you find a file: by its path, such as Workspace.Yard.CoinPads.Pad3.

Think of it as Folders in a filing cabinet

Both sidesRemember: The instance tree is folders in a filing cabinet.

function MapBuilder.build(): MapHandles
	local previous = Workspace:FindFirstChild("Yard")
	if previous then
		previous:Destroy()
	end
 
	local yard = Instance.new("Folder")
	yard.Name = "Yard"
	yard.Parent = Workspace
Figure 2.1

In edit mode you make your own Folder named Yard in Workspace, put a Part named Bench inside it, and press Play. What happens to Bench?

local previous = Workspace:FindFirstChild("Yard")
if previous then
	previous:Destroy()
end
  1. It is destroyed with the old folder, and only the scripted yard returns
  2. It stays, because hand-placed parts are protected
  3. It is copied into the new Yard folder
  4. Play fails with an error
Answer

It is destroyed with the old folder, and only the scripted yard returns. build destroys any existing Yard, children included. A part you want in the yard has to be created inside build, after the new folder exists.

Open this step in lesson mode · Link to this step · This confused me

3Walls from five numbers

Every wall size comes from a few locals at the top of the wall section. Distances are in StudStudRoblox's unit of length. Sizes, positions and speeds are all in studs.Think of it as one square on the grid of the baseplate.s, Roblox's length unit1. The inner yard is 88 studs, walls are 2 thick and 10 tall, and each gate is 8 wide.

	local wallHeight = 10
	local wallThick = 2
	local inner = 88
	local halfInner = inner / 2
	local wallCenter = halfInner + wallThick / 2
	local outerSpan = inner + wallThick * 2
	local gateWidth = 8
	local gap = gateWidth * 2
	local segment = (outerSpan - gap) / 2
	local segmentCenter = gap / 2 + segment / 2
	local wallY = wallHeight / 2
Figure 3.1

Each wall is an anchored part. Anchored parts stay where you put them and ignore gravity2. The server can still move one by writing its CFrame, which is how the gates slide open in the round lesson. A CFrame holds a position and a rotation together3.

inner is 88 and wallThick is 2. MapBuilder computes wallCenter = halfInner + wallThick / 2. How far from the middle of the yard is the centre of each wall, in studs?

Answer

45 studs. 44 + 1 = 45. The wall's inner face sits at 44, the edge of the 88-stud yard, and its centre is half a thickness further out.

Open this step in lesson mode · Link to this step · This confused me

4Six pads on a circle

The pads are where code beats hand placement. One loop puts six pads on a circle of radius 22 around the beacon. Each pad gets a name, a PadIndex attribute and the tag CoinPad, which the coins lesson uses.

	for index = 1, 6 do
		local angle = (index - 1) * (math.pi * 2 / 6)
		local pad = anchoredPart(
			"Pad" .. tostring(index),
			Vector3.new(4, 1, 4),
			CFrame.new(math.cos(angle) * 22, 0, math.sin(angle) * 22),
			PAD_COLOR,
			Enum.Material.Neon,
			padsFolder
		)
		pad:SetAttribute("PadIndex", index)
		CollectionService:AddTag(pad, "CoinPad")
		table.insert(pads, pad)
Figure 4.1

The loop starts at 1, but the angle uses index - 1, so pad 1 sits at angle 0, due east. Run the same maths and read the positions.

Luau
for index = 1, 6 do
	local angle = (index - 1) * (math.pi * 2 / 6)
	local x = math.round(math.cos(angle) * 22)
	local z = math.round(math.sin(angle) * 22)
	print("Pad" .. index, x, z)
end
Figure 4.2. Expected output: Pad1 22 0 Pad2 11 19 Pad3 -11 19 Pad4 -22 0 Pad5 -11 -19 Pad6 11 -19

You change both 6s in the loop to 8. What do you get?

  1. Eight pads, evenly spaced on the same 22-stud circle
  2. Eight pads crowded into three quarters of the circle
  3. Six pads, because the folder only holds six
  4. An error, because PadIndex only goes to 6
Answer

Eight pads, evenly spaced on the same 22-stud circle. With 8 in both places, the step is a full turn divided by 8, so the pads stay evenly spaced. Changing only the loop bound would bunch them up.

Open this step in lesson mode · Link to this step · This confused me

5Landmarks and sightlines from spawn to beacon

Building a map in code is not only geometry; it also decides where a new player walks in the first second. Two level-design ideas explain why MapBuilder.build puts every piece where it does:

  • Landmarks are tall, bright, unmistakable objects you can orient by from anywhere in the yard. The glowing cyan Beacon at (0, 0) rises to y = 6 with a 24-stud PointLight and 10 particles per second. Secondary landmarks anchor the edges: the orange Quartermaster shop at (22, 22) and the twin hazard-striped Yard Gate at (0, 45).
  • Sightlines are the clear lines of view between where a player stands and where you want them to go. MapBuilder places Spawn at (-30, 3, 0), puts Pad4 straight east at (-22, 0) (8 studs ahead), and leaves the corridor to the Beacon at (0, 0) completely open.
Sightline from spawn to the beaconThree boxes left to right along the east axis: Spawn at (-30, 0) leads 8 studs to Pad4 at (-22, 0), which leads 22 studs to the glowing Beacon at (0, 0), with labels marking the Shop and Yard Gate to the sides.8 studs22 studsSpawn(-30, 3, 0)Pad4 (+5)(-22, 0), 8 studsBeacon(0, 0), y=6 glowShop (22, 22)Yard Gate (0, 45) + hazard stripe
Figure 5.1. An unobstructed west-to-east line pulls a new player over a coin pad and straight to the beacon.

Without a single tutorial popup, the sightline teaches the first two steps of the core loop. When you expand the yard into multiple zones with greyboxing, flow and choke points in Module 14, see Prototype fast: paper, greybox, blockout.

You add a 10-stud-tall decorative crate stack at (-15, 5, 0) right between Spawn (-30, 3, 0) and the Beacon (0, 0). What does that break?

  1. The sightline from spawn to the main landmark, so new players no longer see the glowing beacon as soon as they join
  2. The Beacon's ProximityPrompt, which stops working behind parts
  3. The coin ring loop in MapBuilder
  4. WorldLook's dusk lighting
Answer

The sightline from spawn to the main landmark, so new players no longer see the glowing beacon as soon as they join. (-15, 5, 0) sits halfway along the spawn-to-beacon sightline. Keep major obstacles off the player's opening line of sight so the main landmark pulls them forward naturally.

Open this step in lesson mode · Link to this step · This confused me

6Dusk is set by a script too

The sky, the haze and the warm light come from WorldLook.start, which runs before the map is built. Lighting is a service, and its properties set the look of the whole place for every player4. Haze lives on an Atmosphere object under Lighting5.

	Lighting.ClockTime = 17.4
	Lighting.Brightness = 2.6
	Lighting.ExposureCompensation = 0.45
	Lighting.GeographicLatitude = 18
	Lighting.Ambient = Color3.fromRGB(92, 84, 104)
	Lighting.OutdoorAmbient = Color3.fromRGB(176, 132, 104)
	Lighting.EnvironmentDiffuseScale = 0.55
	Lighting.EnvironmentSpecularScale = 0.55
	Lighting.ShadowSoftness = 0.35
	Lighting.GlobalShadows = true
 
	local atmosphere = effect("Atmosphere", "BeaconAtmosphere") :: Atmosphere
	atmosphere.Density = 0.18
	atmosphere.Offset = 0.18
	atmosphere.Color = Color3.fromRGB(210, 168, 132)
	atmosphere.Decay = Color3.fromRGB(92, 64, 88)
	atmosphere.Glare = 0.22
	atmosphere.Haze = 0.75
Figure 6.1

A drill changes the look again, but only two values. RoundService saves ClockTime and Ambient when it starts, moves the clock to 18.4 and a red ambient while the drill runs, then puts both back.

Figure 6.2.

Starting a drill: the gates open and the clock jumps from 17.4 to 18.4.

Transcript
  1. 0:00 The beacon prompt starts the drill for the whole server.
  2. 0:05 The gates tween open over one second.
  3. 0:08 ClockTime jumps to 18.4. Exposure stays where WorldLook set it.

Which Lighting values does a drill change? Pick every one.

  1. ClockTime
  2. Ambient
  3. Brightness
  4. ExposureCompensation
  5. Atmosphere.Haze
Answer

ClockTime; Ambient. setDrillLight writes ClockTime (18.4) and Ambient (a dark red), and restores the saved pair in Cooldown. Brightness, exposure and the atmosphere keep WorldLook's values.

Open this step in lesson mode · Link to this step · This confused me

7Materials that survive a missing upload

WorldLook.apply gives a part its base material and, when it exists, a textured MaterialVariant. The walls become steel and the base becomes asphalt. A running server cannot set MaterialVariant.BaseMaterial, so the variants are written into the place file by scripts/gen-assets.mjs from your own texture ids. If a variant is missing, the part keeps the plain material and boot prints one [Ids] warning.

Roblox Studio
Roblox Studio during Play, looking down on Beacon Yard: the ring of yellow pads, the cyan beacon in the middle, the north wall and the HUD.

Select a numbered pin, or a row below, to read what it marks.

Figure 7.1.

The yard from above during Play. Select a pin to see which function made it.

function WorldLook.apply(part: BasePart, slot: Slot)
	local style = SLOTS[slot]
	part.Material = style.material
	local variant = MaterialService:FindFirstChild(style.variant)
	if variant ~= nil and variant:IsA("MaterialVariant") then
		part.MaterialVariant = style.variant
Figure 7.2

You run Beacon Yard with every texture id still 0. What do the walls look like?

  1. Plain DiamondPlate, with one [Ids] warning in Output
  2. Bright pink, the missing-texture colour
  3. Invisible until you upload textures
  4. Nothing; the server stops before the yard is built
Answer

Plain DiamondPlate, with one [Ids] warning in Output. apply always sets the base material and skips the variant when it is missing. WorldLook.start warns once. The yard still builds and plays.

Open this step in lesson mode · Link to this step · This confused me

8Where the cabinet picture breaks

In a real cabinet, a file is the same file in any drawer. In Roblox, the drawer changes what the thing does. Try to answer before you open the breakdown.

Where does it break?“Folders in a filing cabinet” is a good picture of the instance tree. Where does it stop being true?

It breaks here. Location changes behaviour: the same part renders in Workspace, is invisible and server-only in ServerStorage, and a script runs or not depending on its container.

The true model. Every instance has one parent, and the container decides what it does: whether it renders, replicates, runs or is simulated. Moving an instance between services changes its behaviour, not just its address.

MapBuilder builds a part. Match where it is parented to what happens.

  1. Workspace.Yard
  2. ServerStorage
  3. ReplicatedStorage
  4. Not parented at all
Answer

Workspace.Yard: It renders, collides and replicates to every client; ServerStorage: It exists only on the server and nobody sees it; ReplicatedStorage: Clients receive a copy, but it is not in the 3D world; Not parented at all: It exists in memory and nowhere else. Location is behaviour. Workspace is the world, ServerStorage never replicates, ReplicatedStorage replicates but does not render, and an unparented instance is just a value in a script.

Open this step in lesson mode · Link to this step · This confused me

9Build it: from a hand-placed yard to MapBuilder

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Build in code (MapBuilder)A server script creates every part at boot.Repeated or measured layouts, procedural maps, and repos that need readable diffs.You cannot drag a wall in edit mode, and an artist has to read Luau to change the map.
Build by hand in StudioParts and models placed with the Move and Scale tools and saved in the place file.Most hand-crafted maps. Builders and level designers work this way in nearly every studio.Binary place files do not diff, so two builders editing the same map need Team Create or Packages.
PackagesReusable models that update everywhere they are used.Shared props and kits across places or teams.Versioning lives on Roblox, not in Git.
TerrainVoxel ground, water and rock painted with the Terrain Editor or generated by script.Natural landscapes.Heavier to stream and harder to keep exact measurements.
  • src/server/MapBuilder.luau
  • src/server/WorldLook.luau
  • src/server/init.server.luau
  • default.project.json
  1. In the game repo, run git diff --stat m05-the-round m06-drone-wrench -- src default.project.json.

    You should see git lists MapBuilder.luau and WorldLook.luau as new files and default.project.json with many lines removed.

  2. Check out m06-drone-wrench, open default.project.json and src/server/init.server.luau, and find the two calls.

    You should see The hand-placed Yard entries are gone from default.project.json, and init.server.luau calls MapBuilder.build after WorldLook.start.

  3. Run rojo serve, connect from Studio, and press Play.

    You should see Explorer shows no Yard in edit mode. After Play, Workspace.Yard holds Base, the walls, GateLeft, GateRight, Spawn, Beacon, CoinPads and Shop.

  4. In MapBuilder.luau, change wallHeight from 10 to 18, save, and press Play again.

    You should see The walls and both gates are 18 studs tall. The pads and beacon do not change.

  5. Put wallHeight back to 10. Later lessons assume it.

    You should see The walls are back to 10 studs.

Open this step in lesson mode · Link to this step · This confused me

10Challenges

Level 1, Apprentice

Challenge

Prove that wall height is a map number, not a Config number. Make the walls 18 studs tall, run the suite, then put them back.

Start from
The game at milestone m06 (download)

Done means

  • The walls and gates are taller during Play
  • The test suite's config_numbers check still passes
  • wallHeight is 10 again afterwards

Hints

  1. Search MapBuilder.luau for wallHeight.
  2. Run the suite from the server command bar with game.ServerScriptService.RunGameTests:Invoke().
  3. config_numbers reads Config.luau. The wall height is not in Config.

Level 2, Builder

Challenge

Add a part that survives Play by creating it in MapBuilder.build.

Start from
The game at milestone m06 (download)

Done means

  • A small anchored part exists in Workspace.Yard during Play
  • Stopping and pressing Play again does not make two copies
  • The extra code is removed before the next lesson

Hints

  1. anchoredPart takes a name, size, CFrame, colour, material and parent.
  2. Call it inside build, after the yard folder exists.
  3. A copy you also placed by hand is destroyed at the top of build.

Why does the canopy challenge ask for CanCollide off?

  1. So a player jumping near the beacon is not blocked by the roof
  2. So the roof does not render
  3. So the roof replicates faster
  4. So the server can move it
Answer

So a player jumping near the beacon is not blocked by the roof. A part with CanCollide off still renders and stays anchored, but characters pass through it. A decorative roof should never change how the beacon plays.

Level 3, Professional

Challenge

Put a roof over the beacon that does not block anyone. The check is challenges/world-building.luau in the game repo.

Start from
The game at milestone m06 (download)
Grade it
To grade work from an earlier milestone, apply your challenge changes to a checkout based on m14-ship, which includes the Course Runner. In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("world-building", "world-building-canopy") in the server command bar. Copy the pass code from Output.

Done means

  • Workspace.Yard.Canopy exists, size 20 by 1 by 20
  • Its X and Z are within 1 stud of 0, and Y is at least 12
  • It is anchored with CanCollide off
  • The test prints TEST PASS for canopy_present, canopy_size, canopy_place and canopy_flags

Hints

  1. Parent it to yard, not to the Beacon model.
  2. A Y of 14 clears the crystal anchor at Y 6.
  3. Run the check from the server script's require cache, as challenges/README.md describes.

Open this step in lesson mode · Link to this step · This confused me

11Recap

Retiring the analogy

You no longer need “folders in a filing cabinet”. Every instance has one parent, and the container decides what it does: whether it renders, replicates, runs or is simulated. Moving an instance between services changes its behaviour, not just its address.

Recap

  1. MapBuilder.build destroys the old Workspace.Yard and builds a new one at boot, so hand-placed parts there vanish on Play.
  2. Sizes come from locals in the builder (88-stud yard, 2-stud walls 10 tall, pads on a 22-stud circle); Config holds timers and prices, not layout.
  3. WorldLook sets the dusk and materials once; a drill only changes ClockTime and Ambient, then restores them.

The instance tree is folders in a filing cabinet, until location changes behaviour: the same part renders in Workspace, is invisible and server-only in ServerStorage, and a script runs or not depending on its container.

Next Coins: touch, debounce, respawn, leaderstats

Open this step in lesson mode · Link to this step · This confused me