If you want the part to maintain it’s original orientation, you need to add the angles of it to your new CFrame.
local angle = Part.CFrame - Part.Position
while Dragging do -- don't need to compare to true, this is checks if it's true by default.
wait()
if ObjBeingDragged ~= nil and Mouse.Target ~= nil and Mouse.Target ~= ObjBeingDragged then
Part.Anchored = true
ObjBeingDragged.CanCollide = false
Part.CFrame = angle + Vector3.new(filter(Mouse.Hit.X,Increment),Mouse.Hit.Y + (Part.Size.Y/2),filter(Mouse.Hit.Z,Increment))
end
end
I you want to be able to rotate it, just change the CFrame line to add a rotation and then add 90 degress (or degrees of your choice) every time the player presses R. You can do the same thing for tilting by adding to X or Z.;
local CAS = game:GetService'ContextActionService' -- probably most efficient if this line is in the outermost scope at the top of the script.
local angle = Part.CFrame - Part.Position
local rotation = Vector3.new(0, 0, 0)
local rotate = function()
rotation = rotation + Vector3.new(0, -90, 0) -- negative is clockwise, positive is counterclockwise
end
CAS :BindAction('Rotate Action', rotate, true, Enum.KeyCode.R) -- true means there will be a mobile button added for this too, which will automatically work the same as pressing R while moving a part.
while Dragging do
wait()
if ObjBeingDragged ~= nil and Mouse.Target ~= nil and Mouse.Target ~= ObjBeingDragged then
Part.Anchored = true
ObjBeingDragged.CanCollide = false
Part.CFrame = (angle + Vector3.new(filter(Mouse.Hit.X,Increment),Mouse.Hit.Y + (Part.Size.Y/2),filter(Mouse.Hit.Z,Increment))) * CFrame.Angles(math.rad(rotation.X), math.rad(rotation.Y), math.rad(rotation.Z)) -- if you add tilt to the rotation function, this cframe line will already work for your needs.
end
end
CAS :UnbindAction('Rotate Action')