Help with sections inside a part (math)

Hi everyone I am having trouble trying to come up with a way to achieve part sectioning.

I would like to create 6 (or however many I need) sections inside a part.

Currently I have divided X by 6 to get the size (as demonstrated by the yellow parts) but I do not know the math to position them correctly so they fit into the part exactly.

Any help would be appreciated thanks!

The previous partâ€™s position + (size.X),0,0

Should work

Doesnâ€™t seem to be working:

``````-365.01, 0.001, -123.049
+
16.84, 0, 0
=
-348.17, 0.001, -123.049 --wrong position at the moment
``````

So Iâ€™m not entirely sure what youâ€™re doing, but I think I get a general idea of what you want. So the way I think of this is that I can create a split version of the block and then use a model to move the parts to the reference partâ€™s pivot point. This means the code Iâ€™ve written is assuming that the reference partâ€™s pivot point is at the center, which is the default.

``````local function splitPart(part: BasePart, sections: Vector3): Model
local splitModel = Instance.new("Model")

local totalSections = Vector3.new(
math.round(math.max(1, sections.X)),
math.round(math.max(1, sections.Y)),
math.round(math.max(1, sections.Z))
)

local sectionSize = part.Size / totalSections

for x = 1,totalSections.X do
for y = 1,totalSections.Y do
for z = 1,totalSections.Z do
local sectionPart = part:Clone()
sectionPart.Size = sectionSize
sectionPart.Parent = splitModel

sectionPart:PivotTo(CFrame.new(
Vector3.new(x, y, z) * sectionSize
))
end
end
end

splitModel:PivotTo(part:GetPivot())
return splitModel
end

local referencePart = workspace.Part
local splitPartModel = splitPart(referencePart, Vector3.new(5, 2, 2))
splitPartModel.Parent = workspace

referencePart:Destroy()
``````

So what the splitPart function above is doing is that itâ€™s getting the total amount of sections needed for each dimension of the split with `math.max` to ensure itâ€™s nothing below 1, and it removes any decimals with `math.round`. Doing this ensures that the `totalSections` variable is a Vector3 with the amount of sections needed for each dimension.

Then I need to get the size for each section and set it to `sectionSize`, which is just the size of the part divided by the amount of sections. Since you can divide Vector3s, thatâ€™s what I did to get the right size for each section part.

The for loops next handle creating the necessary parts for each dimension. Each newly created part uses the `sectionSize` variable for their size and uses the current `x`, `y`, `z` values from the loops to determine where to position the part relative to the other parts.

However, doing it this way will make a part with the correct amount of sections, but it will be positioned in the wrong place. Here is what thatâ€™d look like:

And thatâ€™s why I use that last `splitModel:PivotTo(part:GetPivot())`, so the splitPartModel will be positioned at the referencePartâ€™s location.

This is more than most likely not the greatest way to do this, but it works for the most part, which is what matters to me (and probably to you). This should be a good starting point though.

If you wanted 16 sections only on the X axis, you could use `splitPart(referencePart, Vector3.xAxis * 16)`.

1 Like

We can find â€śthe bounds of a coordinateâ€ť then use `Vector3:Lerp` to match the section parts in the main part. Which coordinate to use is up to you (in this case I will use X).

``````-- assume you have the size of a section, and you're splitting into 6
local sectionSize = Vector3.new()
local bounds = sectionSize.X / 2

local index = 0
for _, section in ipairs(sections) do
section.Position =
(mainPart.Position + Vector3.new(-(bounds * 5), 0, 0)):Lerp(
mainPart.Position + Vector3.new((bounds * 5), 0, 0), index/5)
index += 1
end
``````
1 Like