What do you want to achieve?
I want to make a part resizing system using handles. This is my first time working with handles.
What is the issue?
The resizing handles resize in the wrong directions for some parts with different orientations
The resizing handles resize on both directions instead of just one
Here’s the script:
(it’s a local script placed into a ScreenGui in the StarterGui, inside there’s also a ObjectValue named “Part” which I used for testing. Also the parts need to have a BoolValue inside called “CanMove” for them to work with the system)
local plr = game.Players.LocalPlayer
local m = plr:GetMouse()
m.Button1Down:Connect(function()
if m.Target:FindFirstChild("CanMove") then ----- can only use parts with a boolvalue inside called "CanMove"
script.Parent.Part.Value = m.Target
if script.Parent:FindFirstChild("handle") then
script.Parent.handle:Destroy()
end
local handle = Instance.new("Handles")
handle.Parent = script.Parent
handle.Style = "Resize"
handle.Name = "handle"
handle.Adornee = m.Target
local lastCFrame = nil
script.Parent.handle.MouseDrag:Connect(function(face,distance)
local Direction = Vector3.FromNormalId(face)
script.Parent.Part.Value.Size = lastCFrame + (Direction * distance)
print(distance)
end)
script.Parent.handle.MouseButton1Down:Connect(function()
lastCFrame =script.Parent.Part.Value.Size
end)
end
end)
Hey, NoobdyDev!
I solved the problem and optimized the script, and it now works perfectly fine.
What I fixed;
The biggest problem was that the script only changed the size of the part.
When you only use “Handles” to change the size of a Roblox part, it changes size from the center.
You have to do two things at the same time to make it look like you’re pulling one face outwards;
Increase the size.
Adjust the CFrame by half the size of the size increase.
local plr = game.Players.LocalPlayer
local m = plr:GetMouse()
m.Button1Down:Connect(function()
if not m.Target or not m.Target:IsA("BasePart") or not m.Target:FindFirstChild("CanMove") then -- can only use 'parts' with a 'boolvalue' inside called "CanMove"
return
end
script.Parent.Part.Value = m.Target
if script.Parent:FindFirstChild("handle") then
script.Parent.handle:Destroy()
end
local handle = Instance.new("Handles")
handle.Adornee = m.Target
handle.Style = Enum.HandlesStyle.Resize
handle.Name = "handle"
handle.Parent = script.Parent
local lastCFrame = nil
local lastSize = nil
handle.MouseButton1Down:Connect(function()
lastCFrame = script.Parent.Part.Value.CFrame
lastSize = script.Parent.Part.Value.Size
end)
handle.MouseDrag:Connect(function(face, distance)
if not lastCFrame or not lastSize then return end
local direction = Vector3.fromNormalId(face)
local sizeChange = Vector3.new(
math.abs(direction.X) * distance,
math.abs(direction.Y) * distance,
math.abs(direction.Z) * distance
)
local newSize = lastSize + sizeChange
newSize = Vector3.new(
math.max(0.2, newSize.X),
math.max(0.2, newSize.Y),
math.max(0.2, newSize.Z)
)
local actualSizeChange = newSize - lastSize
local positionOffset = direction * (actualSizeChange.X + actualSizeChange.Y + actualSizeChange.Z) / 2
script.Parent.Part.Value.Size = newSize
script.Parent.Part.Value.CFrame = lastCFrame * CFrame.new(positionOffset)
end)
handle.MouseButton1Up:Connect(function()
lastCFrame = nil
lastSize = nil
end)
end)