Scripting Helpers is winding down operations and is now read-only. More info→
← Blog Home

Terrain generation, what and how?

What is terrain generation and how does it work?

The key to terrain generation is math.noise. The most important thing to note from the start, math.noise is not random. (From now on i'll reference math.noise as just "noise".)

What is noise?

The function uses a Perlin noise algorithm to assign fixed values to coordinates. (Same input(s), same output.)

How do we use it?

The noise function takes 3 arguments, but only one is required.

math.noise (x [, y [, z]]) is how roblox describes it on their wiki, wich is what confuses most people. The correct way to get noise working for a terrain generator is y = math.noise(x,z,seed).

How can we use it to make terrain?

It's simple.

  1. Set a seed (For this example, 42, as it is the answer to life, the universe and everything.)
  2. Create a for loop for the X coordinate.
  3. Create a for loop for the Z coordinate.
  4. Create parts
  5. Call local Y = math.noise(X,Z,42)
  6. Set the part position to X,Y,Z.

And thats it!

Now hold up a seccond, this doesn't look smooth at all, thats just a flat base! - That is correct.

The reason why it looks like a flat base, is because all of the values are integers. If all values are integers, math noise will return 1. If we devide the X and Z coordinates by 20, you'll notice it starts to get nice and smooth like your roblox smooth terrain looks like, but the parts are still very close to eachother, we can make the terrain have a bit more depth by multiplying the Y value, lets say by 5. (local Y = math.noise(X/20,Z/20,42) * 5)

Into the scripts

How would a script look like to run this? result

local Seed = 42 -- The answer to life, the universe and everything.

for X = 0,50 do -- X Loop

    for Z = 0,50 do -- Z Loop

        -- Make the part
        local p = Instance.new("Part",workspace) 
        p.Anchored = true
        p.Size = Vector3.new(1,1,1)

        --Just for style
        p.TopSurface = "Smooth"
        p.Material = Enum.Material.Grass
        p.BrickColor = BrickColor.Green()

        --The noise
        local Y = math.noise(X/20,Z/20,Seed) * 5

        --Set the position
        p.CFrame = CFrame.new(X,Y,Z)
    end
    --Wait a bit so your toaster doesn't freeze.
    wait()
end
--Tfw you write all of this and it works in one try.

Well, that's about all there is to it. Experiment around with it and comment what you've made with this.

Posted in Scripting Tips

Commentary

Leave a Comment

Perci1 says: March 27, 2017
good article. I'd like to see a better explanation of what the noise algorithm actually does, though
RubenKan says: March 27, 2017
Well, roblox doesn't show that to the comunity (not that i know off). You can follow the wikipedia link to the formula.
TheeDeathCaster says: March 28, 2017
Very interesting! The only way I knew how to set up a terrain was, yes a for loop, but use the random function for the size. :P
crabbyninja says: March 30, 2017
I made a simple terrain generation script with it: https://www.roblox.com/games/714604803/Terrain-Generation
farrizbb says: April 10, 2017
7.8/10 too much water
Pilot13579 says: May 3, 2017
Very helpful and interesting (Btw....im a Douglas Adams fan too :P)