-- big square variables
local size = {x=5, y=5, z=5}
local pos = {x=0, y=0, z=0}
local Box = Instance.new("Part")
Box.Color = Color3.fromRGB(255, 255, 255)
Box.Material = Enum.Material.SmoothPlastic
Box.Transparency = .5
Box.Parent = workspace
Box.Size = Vector3.new(size.x, size.y, size.z)
Box.Position = Vector3.new(pos.x, pos.y, pos.z)
Box.CanCollide = true
Box.Anchored = true
-- resolution variable
local resolution = 4
-- calculate the size of each mini square
local miniSize = {
x = size.x / resolution,
y = size.y / resolution,
z = size.z / resolution
}
-- create empty table to store mini square positions
local miniSquares = {}
-- loop through each mini square and add its position to the table
for x = 1, resolution do
for y = 1, resolution do
for z = 1, resolution do
table.insert(miniSquares, {
x = (x - 1) * miniSize.x + pos.x,
y = (y - 1) * miniSize.y + pos.y,
z = (z - 1) * miniSize.z + pos.z
})
end
end
end
for Index, Value in ipairs(miniSquares) do
if type(Value) == "table" then
local Part = Instance.new("Part")
Part.Color = Color3.fromRGB(255, 0, 0)
Part.Material = Enum.Material.SmoothPlastic
Part.Transparency = .5
Part.Parent = workspace
Part.Size = Vector3.new(miniSize.x, miniSize.y, miniSize.z)
Part.Position = Vector3.new(Value.x, Value.y, Value.z)
Part.CanCollide = true
Part.Anchored = true
end
end
local function SubdividePartSize(Part:Instance,SubdivideFaces)
return {X=Part.Size.X/SubdivideFaces;Y=Part.Size.Y/SubdivideFaces;Z=Part.Size.Z/SubdivideFaces}
end
print(SubdividePartSize(CubeToSubdivide,3)['X']) --> X Size for mini cube
print(SubdividePartSize(CubeToSubdivide,3)['Y']) --> Y Size for mini cube
print(SubdividePartSize(CubeToSubdivide,3)['Z']) --> Z Size for mini cube
position the middle minicube in the big cube position
Position the top/bottom/front/etc. Part by adding the middle part position and the middle part size(only 1, which is just X, Y,Z. Don’t put Position+Size. Do X+X or Y+Y for example)
Then add the non- vertical/ horizontal by changing 2 members of XYZ instead of 1, For example (Position = Vector3.new(X.Pos+X.Size,Y.Pos+Y.Size,Z…Pos) (in no. 2 it’s Position=Vector3.new(X.Pos+X.Size,Y.Pos, Z.Pos) Difference)
I just ran your script in studio, and it looks like you’re not accounting for the offset required to line up the mini cubes that are created. When creating the single larger part, you’re setting is position to (0,0,0), and when you’re creating all of the mini cubes, you’re starting at (0,0,0), which is the dead center of the main part.
This can be easily fixed with some simple math during the loop where you’re creating the mini cube positions:
-- loop through each mini square and add its position to the table
for x = 1, resolution do
for y = 1, resolution do
for z = 1, resolution do
table.insert(miniSquares, {
x = ((x - 1) * miniSize.x + pos.x) - ((Box.Size.X / 2) - (miniSize.x / 2)),
y = ((y - 1) * miniSize.y + pos.y) - ((Box.Size.Y / 2) - (miniSize.y / 2)),
z = ((z - 1) * miniSize.z + pos.z) - ((Box.Size.Z / 2) - (miniSize.z / 2))
})
end
end
end
I have tried this with resolutions 2, 3, 4 and 10 and it seems to work correctly every time. Hope this helps!