How can i make my Noise terrain generator random?

Basically, i wanted to make a random terrain generator so my game could have random maps and consequentially more replayability.

At first it looked like it worked well, but I eventually noticed that the terrain has a pattern and isn’t random at all.


How can I fix that??

My code:

local mapSizeX = 100
local mapSizeZ = 100

local mapNoise = 50
local mapHeight = 400

local maximumHeight = 250
local minimumHeight = 5

local partSizeX = 8
local partSizeY = 32
local partSizeZ = 8

local map = Instance.new("Folder")
map.Name = "Map"
map.Parent = workspace

local mapFolder = game.ServerStorage.Maps:FindFirstChild("Mountain")
local propFolder = mapFolder:FindFirstChild("Props")
local propChance = 20

local tileID = 0

local grid = {}

for x = 1, mapSizeX do
	grid[x] = {}

	for z = 1, mapSizeZ do
		grid[x][z] = math.noise(x/mapNoise, z/mapNoise) * mapHeight
	end
end

for x = 1, mapSizeX do
	for z = 1, mapSizeZ do

		tileID = tileID + 1

		local yPos = grid[x][z] + 10

		local part = Instance.new("Part")
		part.Anchored = true
		part.Name = "Tile"..tileID

		if yPos < 20 then 
			part.Material = Enum.Material.Grass
			part.Color = Color3.fromRGB(124, 156, 107)
		elseif yPos < 100 then
			part.Material = Enum.Material.Slate
			part.Color = Color3.fromRGB(111, 111, 111)
		else
			part.Material = Enum.Material.Snow
			part.Color = Color3.fromRGB(202, 203, 209)
		end

		part.Position = Vector3.new(x*partSizeX, yPos-(partSizeY/2), z*partSizeZ)
		part.Size = Vector3.new(partSizeX, partSizeY, partSizeZ)
		part.Parent = map

		if part.Position.Y > maximumHeight then
			part.Position = Vector3.new(x*partSizeX, maximumHeight, z*partSizeZ)
		elseif part.Position.Y < minimumHeight then
			part.Position = Vector3.new(x*partSizeX, minimumHeight, z*partSizeZ)
		end

		if yPos < 20 then

			local chance = math.random(1,propChance)

			if chance == 1 then

				local propPivot = CFrame.new(x*partSizeX, part.Position.Y, z*partSizeZ) + Vector3.new(0,(partSizeY/2)+0.5,0)
				local props = propFolder:GetChildren()
				local possibleProp = props[math.random(1, #props)]

				local prop = possibleProp:Clone()
				prop.Parent = map
				prop:PivotTo(propPivot)

			end

		end
	end
end

You need to add a seed to get different noise maps everytime.

Add a variable on seeds:
local seed = math.random()

then the place where you set your noise, insert your seed into there:
grid[x][z] = math.noise(seed, x/mapNoise, z/mapNoise) * mapHeight

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.