So im not that good at explaining but basically im making a tool that allows you to cut parts in half from a certian position, my problem is that its not cutting correctly if its rotated or something, as when its not rotated it works correctly but if it is then the tool cuts its kinda weird, ill put a video showing me cutting the parts in half
Code:
--Define Variables
local CAS = game:GetService("ContextActionService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Tool = script.Parent
local mtarget
local omtarget
local down
local newCut
local Side1,Side2,disx1,disx2,pos1,pos2
local targ,CH,Part1,Part2
local siX, siZ
--Cutting Function
function Cut(actionName, UserInputState)
--print(actionName)
--print(UserInputState)
mtarget = mouse.Target
if actionName == "Cutting" then
if mtarget:FindFirstChild("Owner") then
if mtarget.Owner.Value == player.Name then
if UserInputState == Enum.UserInputState.Begin then
down = true
local sized = mtarget.Size
if mtarget.Name ~= "CutPart" then
local newCut = Instance.new("Part", mtarget) --Create Cut Part
targ = newCut.Parent
newCut.BrickColor = BrickColor.new("Pastel orange") --Change Color of part
newCut.Anchored = true --Anchor the part
siX = targ.Size.X
siZ = targ.Size.Z
if (targ.Orientation.Y > 0 and targ.Orientation.Y < 90) or (targ.Orientation.Y < 180 and targ.Orientation.Y > -90) or (siX > siZ) then
newCut.Size = Vector3.new(0.1,0.1,sized.Z+0.1) --Change the size
newCut.Position = Vector3.new(mouse.Hit.p.X,targ.Position.Y,targ.Position.Z)--Change the position
end
if (targ.Orientation.Y > 90 and targ.Orientation.Y < 180) or (targ.Orientation.Y < 0 and targ.Orientation.Y < -90) or (siZ > siX) then
newCut.Size = Vector3.new(sized.X+0.1,0.1,0.1) --Change the size
newCut.Position = Vector3.new(targ.Position.X,targ.Position.Y,mouse.Hit.p.Z)--Change the position
end
print(newCut.Position)
print(targ.Position)
newCut.Orientation = targ.Orientation
newCut.Material = "Neon" --Change the material
newCut.Name = "CutPart" --Name the part
newCut.CanCollide = false
targ.Anchored = true
while down do
newCut.Size = newCut.Size + Vector3.new(0,0.1,0) --Increase size of part
if newCut.Size.Y >= targ.Size.Y + 0.2 then
Side1 = targ.Position.X - (targ.Size.X/2)
Side2 = targ.Position.X + (targ.Size.X/2)
if newCut.Position.X > Side1 then
disx1 = newCut.Position.X - Side1
pos1 = disx1/2
--print("Pos1:", pos1)
Part1 = Instance.new("Part", workspace)
Part1.Size = Vector3.new(disx1,targ.Size.Y,targ.Size.Z)
Part1.Position = Vector3.new(targ.Position.X-pos1,targ.Position.Y,targ.Position.Z)
Part1.BrickColor = targ.BrickColor
Part1.Material = targ.Material
Part1.Orientation = targ.Orientation
else
disx1 = Side1 - newCut.Position.X
pos1 = (newCut.Position.X - Side1)/2
--print("Pos1:", pos1)
Part1 = Instance.new("Part", workspace)
Part1.Size = Vector3.new(disx1,targ.Size.Y,targ.Size.Z)
Part1.Position = Vector3.new(targ.Position.X-pos1,targ.Position.Y,targ.Position.Z)
Part1.BrickColor = targ.BrickColor
Part1.Material = targ.Material
Part1.Orientation = targ.Orientation
end
if newCut.Position.X > Side2 then
disx2 = newCut.Position.X - Side2
pos2 = disx2/2
--print("Pos2:", pos2)
Part2 = Instance.new("Part", workspace)
Part2.Size = Vector3.new(disx2,targ.Size.Y,targ.Size.Z)
Part2.Position = Vector3.new(targ.Position.X-pos2,targ.Position.Y,targ.Position.Z)
Part2.BrickColor = targ.BrickColor
Part2.Material = targ.Material
Part2.Orientation = targ.Orientation
else
disx2 = Side2 - newCut.Position.X
pos2 = (newCut.Position.X - Side2)/2
--print("Pos2:", pos2)
Part2 = Instance.new("Part", workspace)
Part2.Size = Vector3.new(disx2,targ.Size.Y,targ.Size.Z)
Part2.Position = Vector3.new(targ.Position.X-pos2,targ.Position.Y,targ.Position.Z)
Part2.BrickColor = targ.BrickColor
Part2.Material = targ.Material
Part2.Orientation = targ.Orientation
end
newCut:Destroy()
local children = targ:GetChildren()
targ:Destroy()
for i = 1, #children do
CH = children[i]
local CH1 = CH:Clone()
local CH2 = CH:Clone()
CH1.Parent = Part1
CH2.Parent = Part2
end
down = false
end
wait()
end
newCut:Destroy()
end
elseif UserInputState == Enum.UserInputState.End then
if targ:FindFirstChild("CutPart") then
targ.CutPart:Destroy()
end
targ.Anchored = false
down = false
end
end
end
end
end
--Bind actions when equipping tool
function Equip()
CAS:BindAction("Cutting", Cut, false, Enum.UserInputType.MouseButton1)
end
--Unbind actions when unequipping tool
function Unequip()
CAS:UnbindAction("Cutting")
end
--Check is tool is equipped or unequipped
Tool.Equipped:Connect(Equip)
Tool.Unequipped:Connect(Unequip)
Video: http://www.youtube.com/watch?v=ec2XaD9NjJY&feature=youtu.be
