Mathematical scripting problem

Hello devs!
I’m having an issue. I don’t know how to remake the resize tool, but the template is already done with handles.

local handle = workspace.Handles

handle.Parent = game.Players.LocalPlayer.PlayerGui

handle.Adornee = workspace.Part

handle.MouseDrag:Connect(function(Side,Distance)

end)

I want to figure it out how to make it move 2 studs and resize it 4 studs if you pull the handle by 1 stud each time.

All I need is just the math.

There is a function that is used for resizing parts,

Part:Resize(normalenum, sizeDelta)

If there is a part infront of the resize process it will not resize. We can capture this by using an if function.

if Part:Resize(normalenum, sizeDelta) then
    print("Resize Success!")
else
    print("Resize Failed.")
end

A little note here, you will need to do some extra scripting if you just want to hop this function on a RemoteEvent as it will act very buggy.

1 Like

You can try on mouse drag to check for the delta distance and then scale it

if handles.Adornee then -- the part you want to scale exists
		local delta = distance - previousDistance 
		if math.abs(delta) >= 1 then -- if it's over 1 stud
			
			local resizeDelta -- how much you want to scale

			if delta > 0 then -- it's positive
				resizeDelta = 4
			elseif delta < 0 then -- it's negative
				resizeDelta = -4
			end
			
			if handles.Adornee:Resize(side, resizeDelta) then
				previousDistance = distance -- resets the distance
			end
		end
	end
1 Like

You can also try looking at the scripts in an old roblox resize tool. Resize Tool - Roblox

1 Like

I don’t know how to look at the gear’s scripts

I have also created my own custom handles a while back who look much better than the roblox’s in-game handles:

If you’d wait for me to write some documentation for it I would be happy to share it

1 Like

Yea I want this one, the handle that follows ur cursor and resizes.

I uploaded a copy of Roblox’s old scale gear.

NOTE: DO NOT USE THIS IN YOUR GAME as it is 100% going to fail on serverside conversion. This tool was made somewhere around 2009 to 2012 and was designed with filtering disabled in mind which means it is client sided. So you should only use this tool for learning how it works.

1 Like

What is the previous distance?

The previous distance should be just a variable to nest how much the mouse traveled

local previousDistance

function onHandlesDown(side)
	previousDistance = 0
end

It should reset when the handles are selected

1 Like

How did you make the handle follow mouse like in your previous video?

You cast a ray in the direction of the mouse from the camera, and another ray from the part you want to scale in the direction you want to scale it eg: From the part to Enum.NormalID.Top

If you check for the closest points on the 2 rays you can determine how much the mouse traveled:

local handleDirection = (part.Position - handle.Position).Unit
	

local cameraPosition = workspace.CurrentCamera.CFrame.Position
local mouseDirection = (cameraPosition - mouse.Hit.Position).Unit

	
local a = handleDirection:Dot(handleDirection)
local b = handleDirection:Dot(mouseDirection)
local e = mouseDirection:Dot(mouseDirection)

local d = a*e - b*b
	
local mousePosition
	
if d ~= 0 then
	local r = part.Position - cameraPosition
	local c = handleDirection:Dot(r)
	local f = mouseDirection:Dot(r)

	local s = ( b * f - c * e ) / d
	local t = (a * f - c * b ) / d
	mousePosition = part.Position + handleDirection * s
end

local distance = (mousePosition - handle.CFrame.Position).Magnitude
1 Like

Handle doesn’t has a position, what it is all about?

I have managed to publish the custom handles on roblox, please check:

I don’t recommend reading the script to learn how to code since it’s really poorly written but it should do the trick for your need.

To change how many studs you want to scale at once change the “increment” variable

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.