Heyo, i have a decently large script and i was noticing i violated dry pretty heavily, i have a terrain system and was wonder if there was a nice way to go about cleaning this up a bit.
Here is the script.
local Players = game:GetService("Players")
------------------------------------------------------------------------------------------------------------------------------------------------
local CHUNK_SCALE = 16
local RENDER_DISTANCE = 100
local GENERATION_SEED = math.random()
------------------------------------------------------------------------------------------------------------------------------------------------
local chunks = {}
local function chunkExists(chunkX, chunkZ)
if not chunks[chunkX] then
chunks[chunkX] = {}
end
return chunks[chunkX][chunkZ]
end
local function mountLayer(x, y, z, mat)
local cframe = CFrame.new(x * 2 + 2, math.floor(y * 2 + 2), z * 2 + 2)
local size = Vector3.new(2, 2, 2)
workspace.Terrain:FillBlock(cframe, size, mat)
end
function GenerateOverWorldChunk(chunkX, chunkZ)
local rootPosition = Vector3.new(chunkX * CHUNK_SCALE, 0, chunkZ * CHUNK_SCALE)
chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
for x = 0, CHUNK_SCALE - 1 do
for z = 0, CHUNK_SCALE - 1 do
local cx = (chunkX * CHUNK_SCALE) + x
local cz = (chunkZ * CHUNK_SCALE) + z
local noise = math.noise(GENERATION_SEED, cx / 90, cz / 90)
local cy = noise * 25
mountLayer(cx, cy, cz, Enum.Material.Grass)
end
end
end
function GenerateSkyChunk(chunkX, chunkZ)
local rootPosition = Vector3.new(chunkX * CHUNK_SCALE, 0, chunkZ * CHUNK_SCALE)
chunks[chunkX][chunkZ] = true -- Acknowledge the chunk's existance.
for x = 0, CHUNK_SCALE - 1 do
for z = 0, CHUNK_SCALE - 1 do
local cx = (chunkX * CHUNK_SCALE) + x
local cz = (chunkZ * CHUNK_SCALE) + z
local noise = math.noise(GENERATION_SEED, cx / 90, cz / 90)
local clipmask = math.noise(GENERATION_SEED, cx / 90, cz / 90)
local cy = noise * 25 + 250
local cy2 = (-noise * 175) + 265
if clipmask > 0.08 then
mountLayer(cz, cy, cx, Enum.Material.Grass)
mountLayer(cz, cy2, cx, Enum.Material.Ground)
end
end
end
end
function checkSurroundings(location)
local chunkX, chunkZ = math.floor(location.X / 2 / CHUNK_SCALE), math.floor(location.Z / 2 / CHUNK_SCALE)
local range = math.max(1, RENDER_DISTANCE / CHUNK_SCALE)
--Overworld
for x = -range, range do
for z = -range, range do
local cx = chunkX + x
local cz = chunkZ + z
if not chunkExists(cx, cz) then
GenerateOverWorldChunk(cx, cz)
end
end
end
--Sky
for x = -range * 4, range * 4 do
for z = -range * 4, range * 4 do
local cx = chunkX + x
local cz = chunkZ + z
if not chunkExists(cx, cz) then
GenerateSkyChunk(cx, cz)
end
end
end
end
if game.Workspace.Baseplate then
game.Workspace.Baseplate:Destroy()
end
game:GetService("RunService").Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
if player.Character then
local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
checkSurroundings(humanoidRootPart.Position)
end
end
end
end)