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 = Instance.new("Part")
				Part.CFrame = CFrame.new(Number * PartSize, Number2 * PartSize, Number3 * PartSize)
				Part.Anchored = true
				Part.Size = Vector3.new(PartSize, PartSize, PartSize)
				Part.Parent = workspace.Terrain
			end
		end
	end
	wait()
end

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 = Instance.new("Part")
Cube.Parent = game.Workspace
Cube.Size = Vector3.new(math.random(min, 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

1 Like

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

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

    local Negate = Instance.new("Part")
    Negate.Size = Vector3.new(Size, Size, Size) - Vector3.new(1, 1, 1)

    local Union = OriginalPart:SubtractAsync({Negate})

    OriginalPart:Destroy()
    Negate:Destroy()

    return Union
end

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 = Vector3.new(size, surfaceThickness, size)
    local frontAndBackSize = Vector3.new(size, sizeMinusThickness, surfaceThickness)
    local leftAndRightSize = Vector3.new(surfaceThickness, sizeMinusThickness, sizeMinusThickness)
    for yi = 0, 1 do
        local part = Instance.new("Part")
        part.Anchored = true
        part.Position = center+Vector3.new(0, (-1)^yi*offset, 0)
        part.Size = bottomAndTopSize
        part.BrickColor = BrickColor.new("Lime green")
        part.Parent = workspace
    end
    for zi = 0, 1 do
        local part = Instance.new("Part")
        part.Anchored = true
        part.Position = center+Vector3.new(0, 0, (-1)^zi*offset)
        part.Size = frontAndBackSize
        part.BrickColor = BrickColor.new("Bright blue")
        part.Parent = workspace
    end
    for xi = 0, 1 do
        local part = Instance.new("Part")
        part.Anchored = true
        part.Position = center+Vector3.new((-1)^xi*offset, 0, 0)
        part.Size = leftAndRightSize
        part.BrickColor = BrickColor.new("Bright red")
        part.Parent = workspace
    end
end

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

local function createHollowCuboid(w, h, d, surfaceThickness, center)
	local model = Instance.new("Model")
	
	local doubleThickness = surfaceThickness*2
	local halfThickness = surfaceThickness*.5
	
	local wOffset = w*.5-halfThickness
	local hOffset = h*.5-halfThickness
	local dOffset = d*.5-halfThickness
	
	local wSizeMinusThickness = w-doubleThickness
	local hSizeMinusThickness = h-doubleThickness
	local dSizeMinusThickness = d-doubleThickness
	
	local bottomAndTopSize = Vector3.new(w, surfaceThickness, d)
	local leftAndRightSize = Vector3.new(surfaceThickness, hSizeMinusThickness, dSizeMinusThickness)
	local frontAndBackSize = Vector3.new(w, hSizeMinusThickness, surfaceThickness)

	
	for yi = 0, 1 do
		local part = Instance.new("Part")
		part.Anchored = true
		part.Position = center+Vector3.new(0, (-1)^yi*hOffset, 0)
		part.Size = bottomAndTopSize
		part.BrickColor = BrickColor.new("Lime green")
		part.Parent = model
	end
	for zi = 0, 1 do
		local part = Instance.new("Part")
		part.Anchored = true
		part.Position = center+Vector3.new(0, 0, (-1)^zi*dOffset)
		part.Size = frontAndBackSize
		part.BrickColor = BrickColor.new("Bright blue")
		part.Parent = model
	end
	for xi = 0, 1 do
		local part = Instance.new("Part")
		part.Anchored = true
		part.Position = center+Vector3.new((-1)^xi*wOffset, 0, 0)
		part.Size = leftAndRightSize
		part.BrickColor = BrickColor.new("Bright red")
		part.Parent = model
	end
	return model
end

Late, but here’s a version that works properly with three spatial dimensions.