Trying to Unionize two parts via code

I’m making a terrain system but it gets very laggy when it all loads in, i know i need to turn it into a union to enhance performance but i don’t exactly know how

Code:

local TileSize = math.random(1,100)
local Smoothness= math.random(2,20)
local Size = math.random(100,150)
local Frequency = math.random(10,60)

local Colours = 
{
	Color3.new(math.random(0,255),math.random(0,255),math.random(0,255)),
	Color3.new(math.random(0,255),math.random(0,255),math.random(0,255))
}

for	x = 1, Size do
	for z = 1, Size do
		local Height = (math.noise(x / Smoothness, z / Smoothness) + 2) * Frequency
		p = Instance.new("Part")
		p.Anchored = true
		p.Parent = game.Workspace.Perlin
		p.Size = Vector3.new(TileSize,Height,TileSize)
		p.Color = Colours[math.random(1, #Colours)]
		p.Position = Vector3.new(z * TileSize, Height, x * TileSize)
	end
end

local Terrain = game.Workspace.Perlin:GetChildren()

p:UnionAsync(Terrain) -- issue

it’s also telling me this in the syntax:
Something went wrong. CSG returned error code -11. Please file a bug report, preferably with a file containing the problematic object(s).

It might be because you are trying to unionize too many parts at once? Try creating a union every time the x loop runs.
(also unions may not help performance much)

-- Get the two parts you want to unionize
local part1 = workspace.Part1 -- replace "Part1" with the actual name of your first part
local part2 = workspace.Part2 -- replace "Part2" with the actual name of your second part

-- Check if the parts exist
if part1 and part2 then
    -- Create a union operation
    local unionizedModel = Instance.new("Model")
    unionizedModel.Name = "UnionizedModel"
    
    local unionOperation = Instance.new("UnionOperation")
    unionOperation.Parent = unionizedModel
    unionOperation.Name = "UnionOperation"
    unionOperation.Part0 = part1
    unionOperation.Part1 = part2
    
    -- Perform the union operation
    unionizedModel:MakeJoints()
    unionizedModel.Parent = workspace
    
    print("Unionized successfully!")
else
    print("Error: Parts not found.")
end

You go copy my script and there you have it it would work promising