How to split parts depending on mouse position?

It’s probably my script I was editing that made me think that it doesn’t work with rotated parts. Well how can I make it split and change the Y axis instead of Z axis?

I think I got all the changes, haven’t tested it tho:


local InputS = game:GetService("UserInputService")
local TagS = game:GetService("CollectionService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function getTargetSplitable()
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = {player.Character}
	local result = game.Workspace:Raycast(
		mouse.UnitRay.Origin,
		mouse.UnitRay.Direction * 5000,
		params
	)
	
	if result and TagS:HasTag(result.Instance, "Splitable") then
		return result
	end
	
	return nil
end

function splitablePartPointToOffsetFromBase(part, point)
	local partSpaceBase = Vector3.FromNormalId(Enum.NormalId.Bottom) * part.Size.Y / 2 --Same as Vector3.new(0, part.Size.Y/2, 0)   
	local partSpacePoint = part.CFrame:PointToObjectSpace(point)
	return partSpaceBase.Y - partSpacePoint.Y
end

function splitPartAtOffsetFromBase(part, offset)
	assert(offset < part.Size.Y, "Cannot split part further along its length that it is long.")
	
	local partSize = part.Size
	local partCFrame = part.CFrame

	local part1 = part
	part1.Size = Vector3.new(partSize.X, offset, partSize.Z)
	part1.CFrame = partCFrame * CFrame.new(0, partSize.Y/2 - part1.Size.Y/2, 0)
	part1.BrickColor = BrickColor.Random()
	
	local part2 = part:Clone()
	part2.Size = Vector3.new(partSize.X, partSize.Y - offset, partSize.Z)
	part2.CFrame = partCFrame * CFrame.new(0, -partSize.Y/2 + part2.Size.Y/2, 0)
	part2.Parent = part.Parent
	part2.BrickColor = BrickColor.Random()
end

function onClicked()
	local result = getTargetSplitable()
	if result then
		local offset = splitablePartPointToOffsetFromBase(result.Instance, result.Position)
		splitPartAtOffsetFromBase(result.Instance, offset)
	end
end

InputS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		onClicked()
	end
end)

Instead, the wood’s size got higher and a small piece of wood fell.
https://gyazo.com/f5f86fd6fa5f6feaae51a167d9a19ead

can you show script then please???

2 Likes

You can use Part:BreakJoints() an explosion will fling it

Yeah sorry, not great with the explosion API service or have ever used, from what I sighted from the documentation thought probably could be pulled eh, I guess should’ve not suggested.

Right now I just need help with this. If anyone can help me, I’ll be glad.

I still need help, the problem at the moment is at message 22.