Part splits, places incorrectly

hi this my first roblox devforum post again
my current code finds the distance from the cut indicator (the black part) to the top/bottom of the part and creates two new parts. it then moves the new parts accordingly and it deletes the original and the cut indicator
image
i need some help fixing my code to place the two new parts in the correct locations. my goal is for there to be no space between them. it should look exactly like the original part until it’s disturbed

it does this
image
image

local function Split(target, mouse, cut)
    local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
    local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
    
    local part0 = Instance.new("Part", target.Parent)
    part0.Size = Vector3.new(target.Size.X, (top.Position - cut.Position).Magnitude, target.Size.Z)
    part0.Color = target.Color
    part0.Material = target.Material
    part0.CFrame = target.CFrame + target.CFrame.UpVector * (top.Position - cut.Position).Magnitude
    
    local part1 = Instance.new("Part", target.Parent)
    part1.Size = Vector3.new(target.Size.X, (bottom.Position - cut.Position).Magnitude, target.Size.Z)
    part1.Color = target.Color
    part1.Material = target.Material
    part1.CFrame = target.CFrame + target.CFrame.UpVector * -(bottom.Position - cut.Position).Magnitude
    
    for i, v in pairs(target:GetChildren()) do
        v:Destroy()
    end
    target:Destroy()
end

guysd im making uhhh lumber tycoon 2 guys :tractor:

(pieces too far away make them no space)

here the full module script

6 Likes

What happens if you anchor the parts, do they still seperate? Maybe it’s an issue with roblox physics not the script itself.

3 Likes

Positions are normally in the direct middle of parts’ bounding box and it just looks like you forgot to multiply by 0.5 for the second time.

part1.CFrame = target.CFrame + target.CFrame.UpVector * -part1.Size.Y * 0.5

part0.CFrame = target.CFrame + target.CFrame.UpVector * part0.Size.Y * 0.5

Same difference here:

part1.CFrame = target.CFrame * CFrame.new(0, -part1.Size.Y * 0.5, 0)

part0.CFrame = target.CFrame * CFrame.new(0, part0.Size.Y * 0.5, 0)
3 Likes

Try this:

    local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
    
    local part0 = Instance.new("Part", target.Parent)
    part0.Size = Vector3.new(target.Size.X, (top.Position - cut.Position).Magnitude, target.Size.Z)
    part0.Color = target.Color
    part0.Material = target.Material
    part0.CFrame = CFrame.new(target.CFrame.p, cut.Position) * CFrame.new(0, part0.Size.Y / 2, 0)
    
    local part1 = Instance.new("Part", target.Parent)
    part1.Size = Vector3.new(target.Size.X, (bottom.Position - cut.Position).Magnitude, target.Size.Z)
    part1.Color = target.Color
    part1.Material = target.Material
    part1.CFrame = CFrame.new(target.CFrame.p, cut.Position) * CFrame.new(0, -part1.Size.Y / 2, 0)
    
    for i, v in pairs(target:GetChildren()) do
        v:Destroy()
    end
    target:Destroy()
end

In this version, I’ve changed the calculation of the CFrame for part0 and part1. Instead of adding a vector to the target.CFrame, I’m creating a new CFrame positioned at the midpoint between the target.CFrame.p and cut.Position, and then offsetting it by half the size of the new part. This should ensure that the two new parts are exactly adjacent to each other.

I hope this helps with your Lumber Tycoon 2 project! Let me know if you have any other questions. :blush:

3 Likes

thanks for your reply, but anchoring the parts did not fix the spacing issue

2 Likes

hello cool code
it made the parts spawn vertically, rather than assuming the orientation of the original part
it do this
image
kinda strange it does that but i’ll try messing around with your version some more to see if i can get it working right
thank you :smiley_cat:

2 Likes

hi your solution has been the closest to solving the problem so far
it eliminates the space between the parts but it moves them over a little still
ill mess with this some more to fix that part of it but thank you

1 Like
local function Split(target, mouse, cut)
    local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
    local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
    
    local part0 = Instance.new("Part", target.Parent)
    part0.Size = Vector3.new(target.Size.X, (top.Position - cut.Position).Magnitude, target.Size.Z)
    part0.Color = target.Color
    part0.Material = target.Material
    part0.CFrame = CFrame.new(cut.Position) * CFrame.new(0, part0.Size.Y / 2, 0)
    
    local part1 = Instance.new("Part", target.Parent)
    part1.Size = Vector3.new(target.Size.X, (bottom.Position - cut.Position).Magnitude, target.Size.Z)
    part1.Color = target.Color
    part1.Material = target.Material
    part1.CFrame = CFrame.new(cut.Position) * CFrame.new(0, -part1.Size.Y / 2, 0)
    
    for i, v in pairs(target:GetChildren()) do
        v:Destroy()
    end
    target:Destroy()
end

In this new code, Its changed to CFrame.new(target.CFrame.p, cut.Position) to CFrame.new(cut.Position). This sets the CFrame of the new parts relative to the world axes, which should make them spawn in the correct orientation.

If you have any other questions, you know what to do.

1 Like

your thingy thing works but only if the part’s y axis is aligned with the world y axis

local function Split(target, mouse, cut)
    local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0)
    local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0)
    
    local part0 = Instance.new("Part", target.Parent)
    part0.Size = Vector3.new(target.Size.X, (top.Position - cut.Position).Magnitude, target.Size.Z)
    part0.Color = target.Color
    part0.Material = target.Material
    part0.CFrame = CFrame.new(cut.Position, top.Position) * CFrame.Angles(0, target.CFrame.Yaw, 0)
    
    local part1 = Instance.new("Part", target.Parent)
    part1.Size = Vector3.new(target.Size.X, (bottom.Position - cut.Position).Magnitude, target.Size.Z)
    part1.Color = target.Color
    part1.Material = target.Material
    part1.CFrame = CFrame.new(cut.Position, bottom.Position) * CFrame.Angles(0, target.CFrame.Yaw, 0)
    
    for i, v in pairs(target:GetChildren()) do
        v:Destroy()
    end
    target:Destroy()
end

retried this project from scratch,

cutEvent.OnServerEvent:Connect(function(player, target, cutCFrame)
	print(player, "chopped", target)
	local top = target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
	local bottom = target.CFrame * CFrame.new(0, target.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
	
	local lowerPart = target:Clone()
	lowerPart.Size = Vector3.new(target.Size.X, (bottom.Position - cutCFrame.Position).Magnitude, target.Size.Z)
	lowerPart.CFrame = cutCFrame * CFrame.new(0, cutCFrame:PointToObjectSpace(bottom.Position).Y / 2, 0)
	
	local upperPart = target:Clone()
	upperPart.Size = Vector3.new(target.Size.X, (top.Position - cutCFrame.Position).Magnitude, target.Size.Z)
	upperPart.CFrame = cutCFrame * CFrame.new(0, cutCFrame:PointToObjectSpace(top.Position).Y / 2, 0)
	
	upperPart.Parent = target.Parent
	lowerPart.Parent = target.Parent
	target:Destroy()
end)

key is
lowerPart.CFrame = cutCFrame * CFrame.new(0, cutCFrame:PointToObjectSpace(bottom.Position).Y / 2, 0)
and
upperPart.CFrame = cutCFrame * CFrame.new(0, cutCFrame:PointToObjectSpace(top.Position).Y / 2, 0)

axe local script version 3
axe server script version 3

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.