# Unexpected result with math.noise terrain generation

I am trying to make a terrain generation script that uses math.noise. I tried making the script place gray blocks under the top green blocks, but for some reason it seems that the green blocks aren’t on top and I really don’t know why.

Here is the script:

``````for x = 1,25 do
for z = 1,25 do
local p = game:GetService("ReplicatedStorage").G:Clone()
p.Parent = workspace
p.Anchored = true
p.Size = Vector3.new(3,3,3)
p.TopSurface = Enum.SurfaceType.Smooth
p.BackSurface = Enum.SurfaceType.Smooth
p.Color = Color3.fromRGB(52, 142, 64)
local height = (math.noise(x / 20, seed, z / 20) + 2) * 50
p.Position = Vector3.new(3*x,height-height%3,3*z)
if p.Position.Y < 120 then
p:Destroy()
end
if p.Position == 120 then
p.Color = Color3.fromRGB(0,0,0)
p.Name = "Baseplate"
end
local am = 3
for i = 1,25 do
local p2 = game:GetService("ReplicatedStorage").Stone:Clone()
p2.Parent = workspace.blocks
p2.Position = Vector3.new(p.Position.X,p.Position.Y-am,p.Position.Z)
if p2.Position.Y < 120 then
p2:Destroy()
elseif p2.Position.Y == 120 then
p2.Color = Color3.fromRGB(0,0,0)
p2.Name = "Baseplate"
end
am = am - 3
end
wait()
end
end
``````

“G” is a green block in replicated storage
“Stone” is the gray block in replicated storage

Here is the result:

The only green I see is a small amount on the bottom.

So why is the script not putting those green blocks on top and how do I make it do so?

This code looks like it will destroy the grass, since you are setting it’s height to be between 0 and 100.
EDIT: My assumption of grass generating between 0 and 100 was wrong. It’s actually between 50 and 150.

It should still work - Y 120 is the very bottom where the black blocks are.

Yeah, I removed that piece and it still doesn’t work. All it does is allow the blocks to be placed under the bottom of the world

Oh, I see the problem! You are decreasing your variable “am” but then setting the height of the grey blocks to be grassHeight - am.

This is a double negative, causing the grey blocked to be getting higher and higher, instead of lower and lower.

3 Likes

Yes, it finally works as I wanted it to!

I missed that double negative, thank you very much for seeing it. I’ll just have to set the bottom of the world height to be lower.

Thanks for the help!

2 Likes