Help Fixing Terrain Generation

I am currently creating my own terrain generation script (primarily based around perlin noise, with other elements). I am continually running into an error however. Whenever I start the game, my code breaks in the GenerateMountain section.

image

Here is the current code, I am trying to figure out the cause of the break, and what I need to do to fix it, additionally, if you have feedback on the generation formulas on their own, I’m open to that.

local Core = {Functions = {}, Pathways = {}}
Core.__index = Core

local len = 250
local size = 2
local zoom = 50
local amp = 30

function Core.Functions.GeneratePerlinTerrain(self)
	for x = -len,len do
		for z = -len,len do
			local y = math.noise(x/zoom,z/zoom)*amp
			if y < -2.75 then
				game.Workspace.Terrain:FillBlock(CFrame.new(x*size,2,z*size),Vector3.new(size,size*2,size),Enum.Material.Water)
			else
				game.Workspace.Terrain:FillBlock(CFrame.new(x*size,y,z*size),Vector3.new(size,size*2,size),Enum.Material.Grass)
			end
		end
	end
end

function Core.Functions.GenerateMountain(self,X,Y,Z,Amount)
	for i = 1,Amount do
		game.Workspace.Terrain:FillBlock(CFrame.new(X,Y,Z),Vector3.new(Amount*Amount-(i*10),Y*(i*2),Amount*Amount-(i*10)),Enum.Material.Rock)
	end
end

Core.Functions:GeneratePerlinTerrain()

for i = 1,10 do
	Core.Functions:GenerateMountain(math.random(10,15),math.random(15,20),math.random(10,15),math.random(5,10))
end

I managed to reproduce the error by using the following code:

workspace.Terrain:FillBlock(CFrame.new(0,10,0),Vector3.new(0,0,0),Enum.Material.Rock)

My guess is that at least one of the coords in the Size Vector3 of the FillBlock function are <1, you could prevent your code from filling a certain position if at least one of the coords is 0 (it wouldn’t show up anyway :eyes:)

Simple answer, the size values (X & Z) are negative. It’s because of the subtraction process on the second multiplication of the variable ‘amount’.

Issue has been fixed thanks to @Spooks_HD and @kennydies.
I’ll finish working out the kinks of the mountain generation tonight and post it in cool creations soon!

1 Like