How to split a part from it's center

How would I go about splitting a part directly at its center into two other parts? I have looked all around but I can’t seem to find anything besides the part cutting plugin, but I was looking for another way before “stealing” code from another plugin.

3 Likes

What’d you’d probably want to check out is the BasePart:SubtractAsync() method. What that does is creates union operations with negated parts. This could simulate the ‘splitting effect’ you desire. I’ll attach a wiki post that will go into some further explanation with visuals/code examples :slight_smile:

2 Likes

I was looking more for a splitting function that actually splits the part into two parts and not negate the two. I am working on building shortcuts, so splitting it with negations wouldn’t help with building.

local function cutFromCenter(part, sizeToHalve)
    local size = part.Size
    local mulVec, newSize
    if sizeToHalve == "X" then
        mulVec = Vector3.new(size.X/2, 0, 0)
        newSize = Vector3.new(size.X/2, size.Y, size.Z)
    elseif sizeToHalve == "Y" then
        mulVec = Vector3.new(0, size.Y/2, 0)
        newSize = Vector3.new(size.X, size.Y/2, size.Z)
    else mulVec = Vector3.new(0, 0, size.Z/2)
        newSize = Vector3.new(size.X, size.Y, size.Z/2)
    end
    local cf = part.CFrame
    part.Position = cf*mulVec
    part.Size = newSize
    local clone = part:Clone()
    clone.Position = cf*(-mulVec)
    clone.Parent = part.Parent
    return part, clone
end

Edit: edited a little, but the edits shouldn’t change its behavior

7 Likes

This splits it into multiple parts and not into just two parts. It also spaces them out.

Are you sure that you are only calling it once? It only creates one clone so I don’t understand how it could split it into multiple parts.

1 Like

For some reason it keeps firing over and over and over and I can’t stop it.

There’s actually a really great article that talks about this on Scripting Helpers. See for yourself.

2 Likes

This should work good, works with rotated parts as well as any axis you want it to split.

local function SplitFromCenter(Part,Axis)
	local Clone1 = Part:Clone()
	local Clone2 = Part:Clone()
	
	Clone1.Size = Part.Size - (Vector3.fromAxis(Axis)*Part.Size*0.5)
	Clone2.Size = Clone1.Size
	
	Clone1.CFrame = Part.CFrame * CFrame.new(Vector3.fromAxis(Axis)*Clone1.Size*0.5)
	Clone2.CFrame = Part.CFrame * CFrame.new(Vector3.fromAxis(Axis)*Clone2.Size*-0.5)
	
	return Part,Clone1,Clone2
end

local Part,Clone1,Clone2 = SplitFromCenter(workspace.Part,Enum.Axis.X)
Part:Destroy()
Clone1.Parent = workspace
Clone2.Parent = workspace
1 Like

Could you show the code where you’re calling it?

Try adding a debounce/cooldown too

I fixed it, thank you!! It had to do with the script being parented under the part. (Don’t know how that affects it.)

it was adding spaces between parts, try splitting the mulvec in two since its supposed to add 1/2 of distance, not 1

local function cutFromCenter(part, sizeToHalve)
    local size = part.Size
    local mulVec, newSize
    if sizeToHalve == "X" then
        mulVec = Vector3.new(size.X/2, 0, 0)
        newSize = Vector3.new(size.X/2, size.Y, size.Z)
    elseif sizeToHalve == "Y" then
        mulVec = Vector3.new(0, size.Y/2, 0)
        newSize = Vector3.new(size.X, size.Y/2, size.Z)
    else mulVec = Vector3.new(0, 0, size.Z/2)
        newSize = Vector3.new(size.X, size.Y, size.Z/2)
    end
    local cf = part.CFrame
    part.Position = cf*(mulVec / 2)
    part.Size = newSize
    local clone = part:Clone()
    clone.Position = cf*(-mulVec / 2)
    clone.Parent = part.Parent
    return part, clone
end

'Preciate ya tho, i would have likely been to lazy to write this, i will extend this to use it to bend car parts in my Racing Game

1 Like