Move and resize tool

Hello! How can I make a resize tool and move tool. Here’s the code I have for both.
Also, how can I make the increment 1 stud?

Resize tool:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local handles = tool:WaitForChild("Handles")

local draggingInProgress = false

handles.Parent = player.PlayerGui

tool.Activated:Connect(function()
	handles.Adornee = mouse.Target
	_G.PartSelected = mouse.Target
end)

handles.MouseButton1Down:Connect(function()
	local dragC, mouseUpC
	
	local prevDist = 0 --The distance from last time handles.MouseDrag fired
	
	dragC = handles.MouseDrag:Connect(function(face, distance)
		local deltaDist = (distance - prevDist) --How far the handle was dragged
		
		local resizeDir = Vector3.FromNormalId(face)
		if resizeDir.X == -1 or resizeDir.Y == -1 or resizeDir.Z == -1 then
			handles.Adornee.Size += -1 * resizeDir * deltaDist
		else
			handles.Adornee.Size += resizeDir * deltaDist
		end
		handles.Adornee.Position += resizeDir * deltaDist / 2
		
		prevDist = distance --Update for next time handle is dragged
	end)
	
	mouseUpC = handles.MouseButton1Up:Connect(function()
		dragC:Disconnect()
		mouseUpC:Disconnect()
	end)
end)

Move tool:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
local handles = tool:WaitForChild("Handles")

local draggingInProgress = false

handles.Parent = player.PlayerGui

tool.Activated:Connect(function()
	handles.Adornee = mouse.Target
	_G.PartSelected = mouse.Target
end)

handles.MouseButton1Down:Connect(function()
	local dragC, mouseUpC
	
	local prevDist = 0 --The distance from last time handles.MouseDrag fired
	
	dragC = handles.MouseDrag:Connect(function(face, distance)
		local deltaDist = (distance - prevDist) --How far the handle was dragged
		
		local resizeDir = Vector3.FromNormalId(face)
		if resizeDir.X == -1 or resizeDir.Y == -1 or resizeDir.Z == -1 then
			handles.Adornee.Position += -1 * resizeDir * deltaDist
		else
			handles.Adornee.Position += resizeDir * deltaDist
		end
		handles.Adornee.Position += resizeDir * deltaDist / 2
		
		prevDist = distance --Update for next time handle is dragged
	end)
	
	mouseUpC = handles.MouseButton1Up:Connect(function()
		dragC:Disconnect()
		mouseUpC:Disconnect()
	end)
end)

Note: I got the code from @ThanksRoBama here

Video of the weird behavior of the move tool:

2 Likes

By only making a change and updating prevDist if deltaDist has changed by at least 1 stud, and then only making the change to the nearest 1 whole stud. Change the handles.MouseDrag listener to something like this:

function(face, distance)
		local deltaDist = (distance - prevDist) --How far the handle was dragged
		local deltaDistMagnitude = math.abs(deltaDist)
                local deltaDistRounded = math.sign(deltaDist) * math.floor(deltaDistMagnitude)
                
                if deltaDistMagnitude >= 1 then
		local resizeDir = Vector3.FromNormalId(face)
		if resizeDir.X == -1 or resizeDir.Y == -1 or resizeDir.Z == -1 then
			handles.Adornee.Size += -1 * resizeDir * deltaDistRounded 
		else
			handles.Adornee.Size += resizeDir * deltaDistRounded 
		end
		handles.Adornee.Position += resizeDir * deltaDistRounded / 2
		
		prevDist = math.sign(distance) * math.floor(math.abs(distance)) --Update for next time handle is dragged
                end
	end

haven’t tested it, but I think it’s about right

1 Like

It increments about .5 studs, but it’s good enough. Thanks!

1 Like

I was going to ask you earlier, but I forgot. How could I turn that into a move tool?

I think you can just remove the lines where it changes the size. If that doesn’t work let me know and I’ll take a closer look

1 Like

Thanks, also one more thing (hopefully :laughing:). Whenever I deselect the tool, the handles are still there. I have to reselect them and click the sky. I’ve tried the following code, but it doesn’t seem to work.

tool.Unequipped:Connect(function()
	for _,v in pairs(player.PlayerGui:GetChildren()) do
		if v.Name == "Handles" then
			v:Destroy()
		end
	end
end)

Then I tried to do v.Disabled = not v.Disabled, but none of them worked.

try this

tool.Unequipped:Connect(function()
	for _,v in pairs(player.PlayerGui:GetChildren()) do
		if v.Name == "Handles" then
			v.Adornee = nil
		end
	end
end)