Hello People, I have a code for a basic terrain function using a kind of Perlin noise in it, spawning chunk by chunk up to around 8 layers of blocks. This code has one flaw, which is optimization, which is a big problem since I am using parts and I cannot use terrain because I would obviously lose the blocky style of it. Does anyone have any idea on how I could improve it so I can make it at least playable? Since well, it’s even hard to load in when just a tiny part of it is built, 500 is just and indicative, but I would need much more than that, like a complete idk 10000? and that squared is way bigger than 250k parts per layer (I would need to put more layers too much, like in Minecraft if any of you played it).
local x = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").OmegaNum)
local collectionservice = game:GetService("CollectionService")
local tag = "zpawn"
local Tag = "boss_spawn"
local seed = math.random(1,1000000000)
-- ALL IS MESUARED IN BLOCKS SO IF YSPREAD IS 400 ITS GONNA NEED 400 BLOCKS MINIMUM TO REACH THAT HIGH --
local Msize = 500 -- number dividable by 2 (uses base block)
local Csize = 25 -- the size of each chunk
local peakheight = 45 -- how much the peaks can go in the y axis
local yspread = 50 -- how spread is such value (normally at least 10% more than peakheight)
local Bsize = 5 -- just the block size
local MobSpawnChance = 0.001 -- in % the spawning chance for each block in the world
-- Blocks Layers --
local NB1Layers = 3 -- Num of layers of Block1
local NB2Layers = 4 -- Num of layers of Block2
-- Calculation Vars DONT CHANGE
local TC = math.ceil(Msize/Csize) -- just the tot amt of chunks
local world = workspace.Map -- Where the map is
local Mscale = math.floor(Msize/2)
-- Terrain Chunks Gen --
for CX = 1,TC do -- chunk x val
for CZ = 1,TC do -- chunk z val
local CF = Instance.new("Folder",world) -- The chunk folder
CF.Name = "Chunk_"..CX.."_"..CZ
for X = 1, Csize do
for Z = 1, Csize do
local realx = (CX - 1)*Csize+X
local realz = (CZ - 1)*Csize+Z
-- Generate Terrain Noise
local noise = math.noise(realx/yspread,seed,realz/yspread)*peakheight
-- Layer 0 Block --
local b0 = script.Block:Clone()
b0.Size = Vector3.new(Bsize,Bsize,Bsize)
b0.Position = Vector3.new(realx*Bsize,(math.floor(noise)*Bsize)-Bsize*3,realz*Bsize)
b0.Color = Color3.new(noise,noise,noise)
b0.Parent = CF
if x.leeq(x.rand(0,1),MobSpawnChance) then
-- Determine if it's a boss spawner with a chance
if x.leeq(x.rand(0,1),0.5) then
b0.Name = "MobSpawner"
b0.Color = Color3.fromRGB(0,0,255)
collectionservice:AddTag(b0, tag)
b0:SetAttribute("Mob", "0") -- Replace with the actual mob name
b0:SetAttribute("Amount", math.random(1, 3)) -- Replace with the desired amount
b0:SetAttribute("Cooldown", math.random(2, 5)/10) -- Replace with the desired cooldown
else
b0.Name = "BossSpawner"
b0.Color = Color3.fromRGB(255,0,0)
collectionservice:AddTag(b0, Tag)
b0:SetAttribute("Mob", "0") -- Replace with the actual boss name
b0:SetAttribute("Amount", 1) -- Replace with the desired amount
b0:SetAttribute("Cooldown", math.random(50, 400)/100) -- Replace with the desired cooldown
end
end
-- Layer -1 Block --
for i = 1,NB1Layers do
-- Layer -1 Block --
local b1 = script["Building Blocks"].B1:Clone()
b1.Size = Vector3.new(Bsize,Bsize,Bsize)
b1.Position = Vector3.new(realx*Bsize,math.floor(noise)*Bsize - (Bsize*(i+NB1Layers)),realz*Bsize)
b1.Parent = CF
end
-- Layer -2 Block
for i = 1,NB2Layers do
-- Layer -2 Block --
local b2 = script["Building Blocks"].B2:Clone()
b2.Size = Vector3.new(Bsize,Bsize,Bsize)
b2.Position = Vector3.new(realx*Bsize,math.floor(noise)*Bsize - (Bsize*(i+NB1Layers)),realz*Bsize)
b2.Parent = CF
end
end
game:GetService("RunService").Heartbeat:Wait()
end
end
end
dont mind the if statment for the mob its just another thing for mobspawning
- List item