Most Simple and effective way of generating a cube

I have looked around on google, but most things that come up are just calculating volumes not generating them. I have found a way that works with all sizes. But I doubt it is the most simnple or cost effective ways to do it.

Here is my code:

local Volume = 20
local PartSize = 20

for Number = -Volume / 2, Volume / 2 do
	for Number2 = -Volume / 2, Volume / 2 do
		for Number3 = -Volume / 2, Volume / 2 do
			if (Number / (Volume / 2)) % Volume == 1 or (-Number / (Volume / 2)) % Volume == 1 or (Number2 / (Volume / 2)) % Volume == 1 or (-Number2 / (Volume / 2)) % Volume == 1 or (Number3 / (Volume / 2)) % Volume == 1 or (-Number3 / (Volume / 2)) % Volume == 1 then
				local Part ="Part")
				Part.CFrame = * PartSize, Number2 * PartSize, Number3 * PartSize)
				Part.Anchored = true
				Part.Size =, PartSize, PartSize)
				Part.Parent = workspace.Terrain

I am wondering if anyone can help me shorten it as it is an eyesore.

The simplest way you could do it would be this.

local min = numberhere
local max = numberhere

local Cube ="Part")
Cube.Parent = game.Workspace
Cube.Size =, max),math.random(min, max),math.random(min, max))

You could also have a for loop in here, but that’s basically the simplest way.

Are serious? That is not a hollow cube?

Wait, you need a hollow cube? Or just a regular cube?

Well I will ask common sense. Ring Ring Uhh yes hello. Since I did not just say omg thank you and since I mentioned it is not a hollow cube and since my code generates a hollow cube one would assume that I need a more efficient way of generating a hollow cube

There are other ways to do it that are more simple, but are less effective
For example

local function HollowCube(Size)
    local OriginalPart ="Part")
    OriginalPart.Size =, Size, Size)
    OriginalPart.Parent = workspace

    local Negate ="Part")
    Negate.Size =, Size, Size) -, 1, 1)

    local Union = OriginalPart:SubtractAsync({Negate})


    return Union

local Cube = HollowCube(30)
Cube.Parent = workspace

It will create a hollow union of a cube
According to the wiki, it’s expensive so it might be a bad approach

This will create a cube with six parts.

local function createHollowCube(size, surfaceThickness, center)
    local offset = size*.5-surfaceThickness*.5
    local sizeMinusThickness = size-surfaceThickness*2
    local bottomAndTopSize =, surfaceThickness, size)
    local frontAndBackSize =, sizeMinusThickness, surfaceThickness)
    local leftAndRightSize =, sizeMinusThickness, sizeMinusThickness)
    for yi = 0, 1 do
        local part ="Part")
        part.Anchored = true
        part.Position =, (-1)^yi*offset, 0)
        part.Size = bottomAndTopSize
        part.BrickColor ="Lime green")
        part.Parent = workspace
    for zi = 0, 1 do
        local part ="Part")
        part.Anchored = true
        part.Position =, 0, (-1)^zi*offset)
        part.Size = frontAndBackSize
        part.BrickColor ="Bright blue")
        part.Parent = workspace
    for xi = 0, 1 do
        local part ="Part")
        part.Anchored = true
        part.Position =^xi*offset, 0, 0)
        part.Size = leftAndRightSize
        part.BrickColor ="Bright red")
        part.Parent = workspace

I remember a thread from a while back where someone posted a scripting challenge to create a hollow cube: