Perlin Noise Generates Chunked Terrain Wrong

  1. I want to fix my perlin noise chunk generator

  2. The Chunks Seem to Be misaligned or the chunks arent even connected by the perlin noise map

  3. ive tried everything i could think of even went on other forums but still nothing works

local Storage = game.Workspace.PerlinNoiseStorage
local RunService = game:GetService(“RunService”)

local YHeight = 5
local PartSize = 2
local ChunkSize = 16
local ChunkAmount = 2
local Resolution = 150
local Frequency = 3
local Amplitude = 65

local Chunks = {}

local Coroutines = {}
local CoroutinesCreated = 0

local rng = Random.new()
local DirtSeed = rng:NextInteger(-10e6, 10e6)

local function LoadChunk(ChunkX, ChunkZ, CoroutinesCreated)
local printbool = false
local Success, ErrMessage = pcall(function()
local Blocks = Chunks[ChunkX][ChunkZ]
for X, ValueX in pairs(Blocks) do
for Z, ValueZ in pairs(ValueX) do
for Y, ValueY in pairs(ValueZ) do
local Part = Instance.new(“Part”)
Part.Anchored = true
Part.Size = Vector3.new(PartSize,PartSize,PartSize)
Part.Position = Vector3.new(X, Y*Amplitude+YHeight, Z)
Part.Color = Color3.new((Y+0.5)/10, Y+0.5, (Y+0.5)/10)
Part.Parent = Storage
task.wait()
if printbool == false then
printbool = true
print(“ChunkX:”, ChunkX, “ChunkZ:”, ChunkZ)
print(“X:”, X, “Y:”, Y, “Z:”, Z)
print(“Part Position:”, Part.Position)
end
end
end
end
Coroutines[CoroutinesCreated].close()
end)
end

for X = 1, ChunkAmount do
Chunks[X] = {}
for Z = 1, ChunkAmount do
Chunks[X][Z] = {}
task.wait()
end
end

for X, XValue in pairs(Chunks) do
for Z, ZValue in pairs(XValue) do
local Success, ErrMessage = pcall(function()
CoroutinesCreated +=1
local Blocks = ZValue

		local ExtraX = (X * ChunkSize * PartSize) + PartSize
		local ExtraZ = (Z * ChunkSize * PartSize) + PartSize
		
		for X = 1, ChunkSize do
			Blocks[ExtraX+(X*PartSize)] = {}
			for Z = 1, ChunkSize do
				Blocks[ExtraX+(X*PartSize)][ExtraZ+(Z*PartSize)] = {}
				local Y = math.clamp(math.noise((ExtraX+X)/Resolution*Frequency, (ExtraZ+Z)/Resolution*Frequency, DirtSeed), -0.5, 0.5)
				Blocks[ExtraX+(X*PartSize)][ExtraZ+(Z*PartSize)][Y] = "Part"
			end
		end
		
		warn("New Chunk Started")
		Coroutines[CoroutinesCreated] = coroutine.create(LoadChunk(X, Z, CoroutinesCreated))
		Coroutines[CoroutinesCreated].resume()
		task.wait()
	end)
end

end

local Storage = game.Workspace.PerlinNoiseStorage
local RunService = game:GetService("RunService")

local YHeight = 10
local PartSize = 2
local ChunkSize = 16
local ChunkAmount = 2
local Resolution = 150
local Frequency = 3
local Amplitude = 65

local Chunks = {}

local Coroutines = {}
local CoroutinesCreated = 0

local rng = Random.new()
local DirtSeed = rng:NextInteger(-10e6, 10e6)

local function LoadChunk(ChunkX, ChunkZ, CoroutinesCreated)
	local printbool = false
	local Success, ErrMessage = pcall(function()
		local Blocks = Chunks[ChunkX][ChunkZ]
		for X, ValueX in pairs(Blocks) do
			for Z, ValueZ in pairs(ValueX) do
				for Y, ValueY in pairs(ValueZ) do
					local Part = Instance.new("Part")
					Part.Anchored = true
					Part.Size = Vector3.new(PartSize,PartSize,PartSize)
					Part.Position = Vector3.new(X, (Y*Amplitude)+YHeight, Z)
					Part.Color = Color3.new((Y+0.5)/10, Y+0.5, (Y+0.5)/10)
					Part.Parent = Storage
					task.wait()
					if printbool == false then
						printbool = true
						print("ChunkX:", ChunkX, "ChunkZ:", ChunkZ)
						print("X:", X, "Y:", Y, "Z:", Z)
						print("Part Position:", Part.Position)
					end
				end
			end
		end
		Coroutines[CoroutinesCreated].close()
	end)
end

for X = 1, ChunkAmount do
	Chunks[X] = {}
	for Z = 1, ChunkAmount do
		Chunks[X][Z] = {}
		task.wait()
	end
end

for X, XValue in pairs(Chunks) do
	for Z, ZValue in pairs(XValue) do
		local Success, ErrMessage = pcall(function()
			CoroutinesCreated +=1
			local Blocks = ZValue
			
			local ExtraX = (X * ChunkSize * PartSize) + PartSize
			local ExtraZ = (Z * ChunkSize * PartSize) + PartSize
			
			for X = 1, ChunkSize do
				Blocks[ExtraX+(X*PartSize)] = {}
				for Z = 1, ChunkSize do
					Blocks[ExtraX+(X*PartSize)][ExtraZ+(Z*PartSize)] = {}
					local Y = math.clamp(math.noise((ExtraX+X)/Resolution*Frequency, (ExtraZ+Z)/Resolution*Frequency, DirtSeed), -0.5, 0.5)
					Blocks[ExtraX+(X*PartSize)][ExtraZ+(Z*PartSize)][Y] = "Part"
				end
			end
			
			warn("New Chunk Started")
			Coroutines[CoroutinesCreated] = coroutine.create(LoadChunk(X, Z, CoroutinesCreated))
			Coroutines[CoroutinesCreated].resume()
			task.wait()
		end)
	end
end