Help speeding up my slow terrain generation script

Hello!
I am creating a Minecraft style game here on Roblox. I am using a terrain generation algorithm I made. However, the script is slowing down my computer and the server by a bit (lag spikes). I would like to know how to optimize my script to remove lag spikes and get the game running at an acceptable framerate. Here’s my code: (serverscript)

local Terrain = game.Workspace.Terrain
local seed = 12345
local brickSize = 3

math.randomseed(seed)

local Storage = Instance.new("Folder",workspace)
Storage.Name = "Chunk_Storage"

function brick(x,value,z,typ,p)
	local brick = Instance.new("Part")
	brick.Size = Vector3.new(brickSize, brickSize, brickSize)
	brick.Position = Vector3.new(x*brickSize, math.floor(value)*brickSize, z*brickSize)
	brick.Anchored = true
	if typ == "grass" then
		brick.Color = Color3.new(0,1,0)
	elseif typ == "dirt" then
		brick.Color = Color3.new(0.5,0.15,0)
	elseif typ == "stone" then
		brick.Color = Color3.new(0.65,0.65,0.65)
	end
	brick.Name = typ
	brick.Parent = p
end

function bordermark(x,z)
	local brick = Instance.new("Part")
	brick.Size = Vector3.new(brickSize, brickSize, brickSize)
	brick.Position = Vector3.new(x*brickSize, 0, z*brickSize)
	brick.Anchored = true
	brick.Name = "Loc"
	brick.Color = Color3.new(1,0,0)
	brick.Parent = workspace
end

function GenerateChunk(sx,sz,pcl)
	sx,sz = math.floor(sx),math.floor(sz)
	local pf = Instance.new("Model",game.ServerStorage)
	pf.Name = pcl
	for x = 0, 16-1 do
		for z = 0, 16-1 do
			local value = (math.noise((x+sx) / 25, (z+sz) / 25) * 25)*0.25  -- Adjust amplitude as needed
			brick(x+sx,value,z+sz,"grass",pf)
			local ix = 0
			value = math.floor(value)
			while value > -6 and ix < 500 do
				ix += 1
				value -= 1
				brick(x+(sx),value,z+sz,"dirt",pf)
			end
			while value > -25 and ix < 500 do
				ix += 1
				value -= 1
				brick(x+sx,value,z+sz,"stone",pf)
			end
		end
	end
	pf.Parent = workspace.Chunk_Storage
end

local pcl = 1

local worldsx,worldsz = 10,10
for x = 1,worldsx do
	for z = 1,worldsz do
		bordermark((z-1)*16,(x-1)*16)
		--GenerateChunk((x-1)*16,(z-1)*16)
	end
end

repeat wait() until workspace:FindFirstChild("User")

local char = workspace:FindFirstChild("User")
wait(0.5)
local function getCurrentCell(character)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	local position = rootPart.Position
	local x = math.floor(position.X / (32*brickSize))
	local z = math.floor(position.Z / (16*brickSize))
	return x, z
end

GenerateChunk(0,0,"0")
GenerateChunk(16,0,"1")
GenerateChunk(16,16,"17")
GenerateChunk(0,16,"16")

while wait() do
	local x,z = getCurrentCell(char)
	local pcl = x+z*16
	if not workspace:WaitForChild("Chunk_Storage"):FindFirstChild(pcl) then
		local x,z = getCurrentCell(char)
		x,z = x*32,z*16
		
		local rootPart = char:WaitForChild("HumanoidRootPart")
		local position = rootPart.Position
		--print(position.X / 32*brickSize)
		
		GenerateChunk(x,z,pcl)
		wait(0.1)
		GenerateChunk(x+16,z,pcl)
	end
end
1 Like

Oh yeah - and I have to mention, this is unfinished

If you want to just remove the lag spikes, you can put a task.wait() in parts where it generates chunks (inside the for loop). task.wait() will wait one frame, which will allow other things to happen while the code is running instead of blocking the main thread from continuing execution.

Did you take a look at parallel lua? Maybe this will help Parallel Luau | Documentation - Roblox Creator Hub
It specifically mentions custom terrain generation as a use case, good luck

It makes red bricks and and that’s it … not enough to work with here.