local block_size = 3
local chunk_scale = 16
local render_distance = 20
local seed = math.random(-10e6,10e6)
local noise_scale = 30
local blocks_folder = game:GetService("ReplicatedStorage"):WaitForChild("blocks")
local chunks = {}
local function chunkExists(chunkX, chunkZ)
if not chunks[chunkX] then
chunks[chunkX] = {}
if not chunks[chunkZ] then
chunks[chunkZ] = {}
return chunks[chunkX],chunks[chunkZ]
end
end
end
local function mountLayer(x,y,z,parent,block)
if block then
block:SetPrimaryPartCFrame(CFrame.new(x * block_size,y * block_size,z * block_size))
block.Parent = parent
end
end
function makeChunk(chunkX, chunkZ, chunkY)
local chunk_object = game:GetService("ReplicatedStorage").chunk_object:Clone()
chunk_object.Parent = nil
chunks[chunkX][chunkZ] = true
for x = 0, chunk_scale do
for z = 0, chunk_scale do
for y = 0, chunk_scale do
local cx = (chunkX * chunk_scale) + x
local cz = (chunkZ * chunk_scale) + z
local noise_X = math.noise(y/noise_scale,cz/noise_scale,seed) * 5
local noise_Y = math.noise(cx/noise_scale,cz/noise_scale,seed) * 5
local noise_Z = math.noise(cx/noise_scale,y/noise_scale,seed) * 5
local density = noise_X + noise_Y + noise_Z + y
if density > 9 and density < 10 then
mountLayer(cx, y, cz, chunk_object,blocks_folder:FindFirstChild("grass_block"):Clone())
end
end
end
end
task.wait()
chunk_object.Parent = workspace.terrain
end
function destroy_chunks(location)
for i, chunk in pairs(workspace.terrain:GetChildren()) do
if (chunk:GetPivot().Position - location).Magnitude > 90 then
chunk:Destroy()
end
end
end
function checkSurroundings(location)
local chunkX, chunkZ = math.floor(location.X / block_size / chunk_scale), math.floor(location.Z / block_size / chunk_scale)
local range = math.max(1, render_distance / chunk_scale)
for x = -range, range do
for z = -range, range do
local cx = chunkX + x
local cz = chunkZ + z
if not chunkExists(cx, cz) then
makeChunk(cx, cz)
end
destroy_chunks(location)
end
end
end
while task.wait(1) do
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Character then
local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
checkSurroundings(humanoidRootPart.Position)
end
end
end
end
Everything is important, I can’t shorten it.