I was working on terrain generation when a problem appeared: The parts generate really slow. I tried Instancing MeshParts and other types of parts but it still ran slow. Then i tried the real time CSG and it seems to reduce the lag but not improve the speed.
Here is the code where i used csg:
local Octaves = 12
local Lacun = 2
local Gain = 0.25
local partstab
function fBm(x, y, z, octaves,lacunarity,gain)
local amplitude = 1
local frequency = 1.5
local sum = 0
for i = 1,octaves do
local xf = 1 * x / frequency
local yf = 1 * y / frequency
local zf = 1 * z / frequency
sum = sum + amplitude * math.noise(x * frequency, y * frequency, z * frequency)
amplitude = amplitude * gain;
frequency = frequency * lacunarity;
end
return sum
end
local mapScale = 300
local mapHeight = 200
local waterLvl = 10
math.randomseed(tick())
local mapScale = 250
local mapHeight = math.random(10 , 200)
local waterLvl = math.random(10 , 20)
local seed = math.random(1,10e6)
local smoothness = math.random(10 , 60)
for x = 1, mapScale do
for z = 1, mapScale do
partstab = {}
for _,v in pairs(workspace.Parts:GetChildren()) do
table.insert(partstab , v)
end
if workspace.Parts:FindFirstChild("Part") then
local newunion = workspace.Parts.Part:UnionAsync(partstab)
newunion.Parent = workspace
for _,ae in pairs(partstab) do
ae:Destroy()
end
end
local height = fBm(x/smoothness, z/smoothness, seed , Octaves , Lacun , Gain) --Find out the height
for y = 1, (height*mapHeight)+10 do
local density = fBm(x/smoothness, y/smoothness, z/smoothness, Octaves , Lacun , Gain) --Find out the density
if y > waterLvl then
if density* -5 >= 0 then
--workspace.Terrain:FillBall(Vector3.new(x * 2,y * 2,z * 2) , 5 , "Grass")
local p = Instance.new("Part", workspace.Parts)
p.Anchored = true
p.Size = Vector3.new(10,10,10)
p.CFrame = CFrame.new(x * 10,y * 10,z * 10)
p.Color = Color3.fromRGB(0, 100 , 0)
end
else
--workspace.Terrain:FillBall(Vector3.new(x * 2,y * 2,z * 2) , 5 , "Grass")
local p = Instance.new("Part", workspace.Parts)
p.Anchored = true
p.Size = Vector3.new(10,10,10)
p.CFrame = CFrame.new(x * 10,y * 10,z * 10)
p.Color = Color3.fromRGB(0, 100 , 0)
end
end
end
wait()end
Now i dont know. Games like Minecraft or terraria generate the ground in 5 seconds(Even though terraria is 2d i dont think it would generate terrain much slower). I understand that those games were made from nothing and not game developing platform but is this an issue for roblox?

8 comments
Have you thought about spawning each instantiation?
?
https://www.robloxdev.com/articles/Built-in-Functions-and-Variables/Roblox#spawn ?
You can prevent lag even further by setting properties of a part before parenting it to the workspace.Parts. The second parameter of Instance.new is deprecated anyway.
For that very reason
always parent an instance after setting all of its properties
https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296/8
Ok it became faster but is there a way to make something similar to the "for" loop in a spawn or hearbeat?