Lesson 2 of 5
Procedural generation: noise, seeds, chunking
Generate a seeded scrap field around Beacon Yard with hashes, Perlin and value noise and chunks, run the noise in the browser, and move the heavy maths to Parallel Luau.
By the end you can
- Make random content repeatable with a seed and a stateless hash instead of math.random
- Use noise (math.noise or hand-written value noise) to make placement cluster naturally
- Split a large generated area into chunks and build only the chunks that are needed
- Decide what generation work can run in Parallel Luau and what must stay serial
Before this: World building in code: MapBuilder, lighting, atmosphere, Parallel Luau: Actors and multithreading
1The empty field outside the walls
Press Play in Beacon Yard and walk to a wall. Inside, everything has a place: the base is 96 studs across, the walls enclose 88, the pads sit on a ring. Outside the walls there is nothing at all. MapBuilder builds the yard from numbers every time the server starts:
-- Inner yard is 88 studs. Walls are 2 thick, so their centers sit at
-- 45. North and south spans include the corners (92). The +Z wall
-- leaves a 16-stud gap for two 8-stud gates.
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
In this lesson you fill the field around those walls with piles of scrap. Nobody places them by hand. A few rules and one number, the seed, decide where every pile goes, and the same seed always gives the same field.
From the code above: the walls' centres sit at wallCenter studs from the middle, and they are wallThick studs thick. How far from the centre does the outside face of a wall end?
Answer
46 studs. halfInner is 44, so wallCenter is 45. The outer face is 1 stud further out: 46. The scrap field keeps 6 more studs clear, so nothing generates inside the 52-stud square around the centre.
Open this step in lesson mode · Link to this step · This confused me
2Rules instead of placement
Procedural generationProcedural generationContent placed by rules and numbers in code, instead of by hand.Think of it as a recipe for a level rather than the level itself. means writing the rules that place things, instead of placing them. You have already seen a tiny version: MapBuilder does not list six pad positions, it computes them on a circle.
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)
A generator for a whole field needs three more ideas. A seed makes the randomness repeatable. Noise makes it look natural, with clusters and gaps instead of even sprinkling. Chunks split the field into squares so you only build the part you need.
Using the loop above, where does Pad4 sit?
Answer
About (-22, 0, 0): angle 180 degrees on a 22-stud circle. Angle 180 degrees: cos is -1 and sin is 0, so x = -22 and z is 0 (a tiny rounding error away from it).
Open this step in lesson mode · Link to this step · This confused me
3Seeds: random, but repeatable
math.random gives a different field every time, and its answer depends on how many times it was called before. Generate chunk A then chunk B, and B differs from generating B first. For a world that must look the same to every server and every visit, use a seedSeedThe number that picks one world out of all the worlds a generator can make.Think of it as the page number in a book of random numbers. and a hash: a function with no hidden state that scrambles its inputs into a number between 0 and 1.
Run this. Same seed and cell, same number. Change the seed, and the whole field changes.
-- A seeded hash: the same seed and cell always give the same number in [0, 1).
local function hash(seed: number, x: number, z: number): number
local h = (x * 73856093 + z * 19349663 + seed * 83492791) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 16))
h = (h * 1664525 + 12345) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 13))
h = (h * 1664525 + 12345) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 16))
return h / 2 ^ 32
end
print(string.format("seed 7, cell (3, 4): %.3f", hash(7, 3, 4)))
print(string.format("seed 7, cell (3, 4): %.3f", hash(7, 3, 4)))
print(string.format("seed 8, cell (3, 4): %.3f", hash(8, 3, 4)))
seed 7, cell (3, 4): 0.924
seed 7, cell (3, 4): 0.924
seed 8, cell (3, 4): 0.281Why does the scrap field use a seeded hash instead of math.random?
Answer
The same seed gives the same field on every server and every visit; A chunk comes out the same whatever order the chunks are generated in; You can store or share one number instead of every pile's position. A stateless hash depends only on its inputs. That makes the field repeatable, order-independent and described by one seed. math.random is available; it just keeps hidden state.
Open this step in lesson mode · Link to this step · This confused me
4Noise: randomness with neighbours
Hashing every cell separately gives static, like an untuned TV. Real scrap piles up in heaps with clear ground between them. Noise is randomness where nearby points get similar values. Luau's standard library has math.noise(x, y, z), which returns 3D Perlin noisePerlin noiseSmooth random values where nearby points are similar; math.noise in Luau.Think of it as rolling hills instead of TV static. between -1 and 112.
Three things matter in practice:
- Scale. Divide positions before sampling.
math.noise(x / 32, z / 32)changes slowly over 32 studs;x / 4is busy. - Seeding.
math.noisehas no seed parameter. Offset the input by the seed instead:math.noise(x / 32 + seed * 101.7, z / 32). - Whole numbers. Perlin noise is exactly 0 at every whole-number input, so never sample integer positions unscaled.
What does this print?
print(math.noise(3, 7, 0))
Answer
0. At whole-number inputs Perlin noise is exactly 0. That is why you scale positions (x / 32) before sampling.
Open this step in lesson mode · Link to this step · This confused me
5Draw the field
math.noise is fine for most games. Writing your own noise once shows you what it does, and lets you seed it properly. Value noise puts a hashed random value at every whole-number corner and blends smoothly between the four corners around a point. The map below prints one character per 8-stud cell from -96 to 96 studs: Y is the yard and its margin, # is dense scrap, . is open ground.
local SEED = 7 -- change me and run again
local function hash(seed: number, x: number, z: number): number
local h = (x * 73856093 + z * 19349663 + seed * 83492791) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 16))
h = (h * 1664525 + 12345) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 13))
h = (h * 1664525 + 12345) % 2 ^ 32
h = bit32.bxor(h, bit32.rshift(h, 16))
return h / 2 ^ 32
end
-- Value noise: a random value at each whole-number corner, blended in between.
local function valueNoise(seed: number, x: number, z: number): number
local x0, z0 = math.floor(x), math.floor(z)
local tx, tz = x - x0, z - z0
tx, tz = tx * tx * (3 - 2 * tx), tz * tz * (3 - 2 * tz)
local a, b = hash(seed, x0, z0), hash(seed, x0 + 1, z0)
local c, d = hash(seed, x0, z0 + 1), hash(seed, x0 + 1, z0 + 1)
local near, far = a + (b - a) * tx, c + (d - c) * tx
return near + (far - near) * tz
end
-- One character per 8-stud cell, from -96 to 96 studs. Y is the yard.
for cellZ = -12, 11 do
local row = ""
for cellX = -12, 11 do
local x, z = cellX * 8 + 4, cellZ * 8 + 4
if math.max(math.abs(x), math.abs(z)) < 52 then
row ..= "Y"
elseif valueNoise(SEED, cellX / 4, cellZ / 4) > 0.55 then
row ..= "#"
else
row ..= "."
end
end
print(row)
end
##................####..
##......##........####..
####..####..............
##########..............
##########..............
##########..............
######YYYYYYYYYYYY......
#####.YYYYYYYYYYYY......
#####.YYYYYYYYYYYY......
###...YYYYYYYYYYYY......
......YYYYYYYYYYYY......
......YYYYYYYYYYYY....##
......YYYYYYYYYYYY..####
......YYYYYYYYYYYY..####
......YYYYYYYYYYYY.#####
###...YYYYYYYYYYYY######
###...YYYYYYYYYYYY######
###...YYYYYYYYYYYY######
###########...##########
###################...##
###################...##
###################....#
###..........######.....
##............#####.....Change SEED and run it again: a different field, with the yard in the same place. Change cellX / 4 to cellX / 2 and the heaps get smaller and busier.
How many Y characters are there in each row that crosses the yard?
Answer
12. Cell centres run -92, -84, ... 92. Those within 52 studs are -44 to 44: twelve cells, which is 96 studs, the size of the yard's base.
Open this step in lesson mode · Link to this step · This confused me
6Chunks: build the field in squares
A field around a 96-stud yard is small, but the same code could fill thousands of studs. A chunkChunkA fixed square of a generated world that is built and removed as one unit.Think of it as one square of a tiled floor, laid only where people walk. is a fixed square of the world, here 64 by 64 studs, that the generator builds as one unit. Chunk coordinates come from floor division: x // 64. A server builds the chunks near players and skips the rest.
Think of it as Stage sets built as you walk
Both sidesRemember: Building the world near the player (chunks and StreamingEnabled) is stage sets built as you walk.
local CHUNK = 64 -- studs on each side of a chunk
local function chunkOf(x: number, z: number): (number, number)
return x // CHUNK, z // CHUNK
end
local spawnX, spawnZ = -30, 0 -- the yard's spawn pad
local cx, cz = chunkOf(spawnX, spawnZ)
print("spawn is in chunk", cx, cz)
local needed = {}
for dx = -2, 2 do
for dz = -2, 2 do
table.insert(needed, (cx + dx) .. "," .. (cz + dz))
end
end
print(#needed, "chunks within 2 of the spawn")
print("first:", needed[1], "last:", needed[#needed])
spawn is in chunk -1 0
25 chunks within 2 of the spawn
first: -3,-2 last: 1,2Because the hash has no state, any chunk can be built at any time, in any order, by any server, and it always comes out the same. That is what makes chunks safe to build lazily.
Pad4 sits at x = -22. With 64-stud chunks, what is its chunk column, x // 64?
Answer
-1. -22 / 64 is about -0.34, and the floor of that is -1. Rounding towards zero would give 0, and two chunks would claim the same strip.
Open this step in lesson mode · Link to this step · This confused me
7Where the stage-set picture breaks
Chunked generation and Roblox's streaming both build the world near the player, so they are easy to confuse. Answer before you open the breakdown.
Where does it break?“Stage sets built as you walk” is a good picture of building the world near the player (chunks and streamingenabled). Where does it stop being true?
It breaks here. There are two crews. Your generator decides what exists on the server; StreamingEnabled separately decides which of it each client has received, so a piece can exist on the server and still be missing on a client.
The true model. Chunked generation is your code creating instances on the server near players. StreamingEnabled is the engine sending only nearby instances to each client. Client code must handle parts that are not streamed in yet, whatever created them.
The server generates the scrap chunks around every player. With StreamingEnabled on, a player far from a chunk looks for one of its piles in a LocalScript. What happens?
Answer
The pile can be missing on that client: the server built it, but streaming has not sent it yet. Your generator decides what exists on the server. Streaming separately decides which parts each client has received. Client code must wait for parts or handle them being absent.
Open this step in lesson mode · Link to this step · This confused me
8Build the field in Beacon Yard
The level 3 challenge's learner file, challenges/work/scrap_field.luau at m14-ship, is where the generator lives: generate(seed, chunkX, chunkZ) returns the piles for one chunk as plain tables. MapBuilder then turns them into parts. Keeping the maths free of instances means it can be tested without Studio and moved into Parallel Luau later.
Build along
challenges/work/scrap_field.luausrc/server/MapBuilder.luau
Write
generatewith a seeded hash and value noise, as in the cells above. Keep every pile 52 studs or more from the centre on at least one axis.You should see The level 3 challenge prints TEST PASS for all five checks.
In
MapBuilder.build, after the walls, require the module, loop chunks -3 to 2 on both axes, and create one anchored, rustyMetalpart per pile in aScrapFieldfolder under the yard.You should see Pressing Play builds Workspace.Yard.ScrapField with a few hundred anchored parts outside the walls and none inside.
Use a fixed seed such as 7, play twice and compare a few piles' positions in the Explorer.
You should see Stopping and pressing Play again rebuilds an identical field.

Select a numbered pin, or a row below, to read what it marks.
The yard from above with seed 7. The pins mark a dense heap, open ground, and the 6-stud margin outside the walls.
Beacon Yard's MapBuilder destroys and rebuilds Workspace.Yard every time the server starts. What does that mean for the scrap field?
Answer
It must be regenerated from the seed on each start; the seed is the only thing worth saving. The yard is code-built on every start, so the field is too. Same seed, same field, and nothing to store but the number.
Open this step in lesson mode · Link to this step · This confused me
9Heavy generation in Parallel Luau
Six chunks of scrap take milliseconds. Terrain voxels, caves or thousands of trees can stall a server for whole frames. Parallel Luau spreads work across CPU cores: put scripts under separate Actor instances, then call task.desynchronize() to run in parallel and task.synchronize() to return to serial3. Scripts in the same actor still run one after another, so you need several actors.
The rule that shapes the design: most reads are safe in parallel, but changing the shared world, such as parenting a part into Workspace, must happen in a serial phase. So compute the pile list in parallel (pure maths, like generate), then synchronize and create the parts. Roblox's own guide uses exactly this split for server-side procedural terrain, with workers receiving chunk messages through Actor:SendMessage3.
Other ways pros do this
| Option | What it is | When pros pick it | Trade-offs |
|---|---|---|---|
| math.noise | Built-in Perlin noise in the Luau standard library. | Most Roblox terrain and placement. Fast and always available. | No seed parameter (offset the input instead), and exactly 0 at whole numbers. |
| Hand-written value or simplex noise | Your own noise on top of a seeded hash. | You need true seeds, identical results outside Roblox (tests, tools), or a specific look. | More code, and slower than the built-in unless you are careful. |
| Terrain voxels (WriteVoxels, FillBlock) | Write noise-driven materials into Roblox Terrain. | Hills, caves and water rather than separate props. | Terrain has its own memory and streaming costs; props still need parts or models. |
| Hand-built in Studio | Place everything yourself, maybe with a scatter plugin. | Small, curated spaces where each spot matters, like Beacon Yard's lobby. | Every variation is manual work and lives in the place file. |
In a parallel scrap generator, which step belongs after task.synchronize()?
Answer
Parenting the new pile parts into Workspace.Yard.ScrapField. Hashing, noise and building a list are pure maths and run in parallel. Putting parts into Workspace changes shared state, so it runs serially.
Open this step in lesson mode · Link to this step · This confused me
10Challenges
Level 1, Apprentice
Challenge
Tune the map: try seeds and the threshold until the field has a walkable path out of the gates.
- Start from
- The scrap-map cell on this page
Done means
- You found a seed whose field leaves a clear path from the north gate (positive z) to the edge of the map
- You changed the density threshold so roughly a third of the field is scrap
- You can explain what dividing by 4 versus 8 does to the heaps
Hints
- The gates are in the north wall, so look at the rows printed last.
- Raise 0.55 to get less scrap, lower it to get more.
- A larger divisor stretches the noise over more cells.
Level 2, Builder
Challenge
Build chunks lazily around players instead of all at boot.
- Start from
- m06-drone-wrench with the scrap field built
Done means
- Only the chunks within 2 of any player exist on the server
- Chunks that no player is near are destroyed after a few seconds
- Walking away and back rebuilds an identical chunk
Hints
- Keep a table of built chunks keyed by the chunk string, as in the chunks cell.
- Check every second, not every frame.
- Destroying and rebuilding is safe because generate has no hidden state.
The build-along loops chunks -3 to 2 on both axes at boot. How many chunks is that?
Answer
36 chunks. -3, -2, -1, 0, 1, 2 is six values per axis, so 6 x 6 = 36 chunks, covering -192 to 192 studs. The lazy version in level 2 builds far fewer while one player stands in the yard.
Level 3, Professional
Challenge
Start from the m14-ship tag in the game repo, then make the stock generator pass. It uses math.random and ignores the yard. Run it from the command bar with game.ServerScriptService.RunGameTests:Invoke("procedural-generation").
- Start from
- The game at milestone m14 (download)
- Grade it
- In Studio, press Play, then run
game.ServerScriptService.RunGameTests:Invoke("procedural-generation")in the server command bar. Copy the pass code from Output.
Done means
- challenges/work/scrap_field.luau gives the same piles for the same seed and chunk, in any call order
- Seeds 7 and 8 give different fields
- No pile inside the square around the centre: a pile is rejected when both its x and z are within 52 studs of 0, so (40, 40) fails even though it is more than 52 studs away in a straight line
- Every pile is inside its chunk, sizes are 1 to 8, there are at most 48 per chunk, and the field is not empty
Hints
- Replace math.random with a hash of seed, cell and a salt per value.
- Work in 8-stud cells: 64 // 8 = 8 cells per side.
- Drop a pile when math.max(math.abs(x), math.abs(z)) < 52. This is a square, not a circle, so do not use the distance from the centre.
The level 3 test generates chunk (2, -1), then two other chunks, then (2, -1) again. What bug does that catch?
Answer
Hidden state, such as math.random, that makes a chunk depend on what was generated before it. If anything between the two calls can change the result, the generator has hidden state. Other checks cover the keep-out square, density and chunk bounds.
Open this step in lesson mode · Link to this step · This confused me
11Recap
Retiring the analogy
You no longer need “stage sets built as you walk”. Chunked generation is your code creating instances on the server near players. StreamingEnabled is the engine sending only nearby instances to each client. Client code must handle parts that are not streamed in yet, whatever created them.
Recap
- A seed plus a stateless hash makes generation repeatable and order-independent; math.random keeps hidden state.
- Noise (math.noise or value noise) gives clusters and gaps; scale the input, offset it by the seed, and avoid whole-number samples.
- Chunks let you build only what is near players; compute in parallel actors, then create parts in a serial phase.
Building the world near the player (chunks and StreamingEnabled) is stage sets built as you walk, until there are two crews. Your generator decides what exists on the server; StreamingEnabled separately decides which of it each client has received, so a piece can exist on the server and still be missing on a client.
Queued for review
- Why does the scrap field use a seeded hash instead of math.random?
- What does this print?
- Pad4 sits at x = -22. With 64-stud chunks, what is its chunk column, x // 64?
- The server generates the scrap chunks around every player. With StreamingEnabled on, a player far from a chunk looks for one of its piles in a LocalScript. What happens?
- In a parallel scrap generator, which step belongs after task.synchronize()?
Open this step in lesson mode · Link to this step · This confused me