Made a resizing and scaling thing and it isnt working well

I am making a game like Mashables. There is a building, rotating, moving, and sizing system you can do with objects in Mashables to make robots. I am trying to replicate this in my game. I have decided to start with the sizing system, and it is acting odd.

Here is the problem: It seems like the block is scaling from both sides, and when I scale it to the edge of the screen, it scales very largely. I want to implement an increment.

Video:


Location:
6cbc582c846eed9690b1eb1f5e6f6193b17c0ffb
Code:

local scaleHandle = script.Parent
local block = workspace[script.Parent.Adornee.Name]
scaleHandle.MouseDrag:Connect(function(face, distance)
	if face == Enum.NormalId.Left or face == Enum.NormalId.Right then
		block.Size += Vector3.new(distance, 0, 0)
	end
	if face == Enum.NormalId.Top or face == Enum.NormalId.Bottom then
		block.Size += Vector3.new(0, distance, 0)
	end
	if face == Enum.NormalId.Front or Enum.NormalId.Back then
		block.Size += Vector3.new(0, 0, distance)
	end
end)

The problem:

When I move the handle in the x or z direction, the block gets size in the y as well.

When I move the handle in the y direction, the block gets size in the x as well.

When I move the handle, the handle itself changes position.

I need to add a check in the code to make sure the block doesn’t have a size less than the handle’s size, so the handle stays in the block.
For some reason, I can’t get the handle to rotate with the block. I know how to do this, but I was trying to see if I could get the sizing to work first.
The block is a part. The handle is a cylinder. I made the cylinder invisible, but I sized it to be the size of the block. I hope this is enough information.

I was able to solve the problem.
local scaleHandle = script.Parent
local block = workspace[script.Parent.Adornee.Name]
scaleHandle.MouseDrag:Connect(function(face, distance)
if face == Enum.NormalId.Left or face == Enum.NormalId.Right then
block.Size += Vector3.new(distance, 0, 0)
end
if face == Enum.NormalId.Top or face == Enum.NormalId.Bottom then
block.Size += Vector3.new(0, distance, 0)
end
if face == Enum.NormalId.Front or Enum.NormalId.Back then
block.Size += Vector3.new(0, 0, distance)
end
scaleHandle.Size = block.Size
end)

I think that the problem was that the handle was staying the same size as the block was growing. So, I made the handle the same size as the block using the code above.

The Handles instance does not have the Size property.

This is very urgent. I need a response or a solution for this post.

The code shown above scales the part in both directions.
I suggest you try to piece of code from a scale tool I made not too long ago:

local lastDistance = 0

function scaleSnap(distance, step)
	return math.floor(distance / step + 0.5) * step
end

function onScale(face, distance)
	local delta = distance - lastDistance

	if math.abs(delta) >= block.ResizeIncrement then
		local scale = scaleSnap(delta, block)

		if block:Resize(face, scale) then
			lastDistance = distance
		end
	end
end

The block:Resize allows you to resize any part from one face without it scaling on both sides (This is what the scale tool uses to resize parts).

Obviously, connect the onScale function to the scaleHandle.MouseDrag event.
And when you hold on one of the scale handles, you reset the lastDistance to 0.

Hope this helps! :smiley:

1 Like

Oh, and if you’re interested, here’s a model I’ve made that contains the basic building tools:
Studio Tools

1 Like

How would I detect if the player held on the handle?

Sorry for the late response, but it would be something like this:

scaleHandle.MouseButton1Down:connect(function()
    lastDistance = 0
end

What is the ‘if block:Resize() then’ thing?

To check if the part has been resized.

limit 300

Heres some more info about Resize():
Resize

Ah, my fault :sweat_smile:, It’s supposed to be:

local scale = scaleSnap(delta, block.ResizeIncrement)

not:

local scale = scaleSnap(delta, block)

Would I just make do ‘local ResizeIncrement = 1’ or something then put that in the place of block.ResizeIncrement?

No, the ResizeIncrement is already a member of block, but if you want to resize the part in smaller / bigger increments, you can do something like:

local increment = 0.5 -- Any increment you want

And replace the ResizeIncrement with increment.

Really? I never knew all these things.

Lol, me too. I just found out about these things last summer :joy:.

By the way, how would I also make a barrier, where if the player tried to scale a part into it, it wouldn’t scale? Like collisions.

I’m not sure. I suggest you make the block CanCollide off, and when you release one of the handles, you can change it’s collision back to it’s original.

How would I do something like this with position instead?

Edit:
Ended up changing the function with just changing the scale of it normally when its dragged, then subtracting the opposite face with the negative of the scale.

Certainly not the best way, at all, but it works.

function onScale(face, distance)
	local delta = distance - lastDistance

	if math.abs(delta) >= block.ResizeIncrement then
		local scale = scaleSnap(delta, block.ResizeIncrement)

		if face == Enum.NormalId.Left then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Right, -scale) then
					lastDistance = distance
				end
			end
		end
		if face == Enum.NormalId.Right then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Left, -scale) then
					lastDistance = distance
				end
			end
		end
		if face == Enum.NormalId.Top then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Bottom, -scale) then
					lastDistance = distance
				end
			end
		end
		if face == Enum.NormalId.Bottom then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Top, -scale) then
					lastDistance = distance
				end
			end
		end
		if face == Enum.NormalId.Front then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Back, -scale) then
					lastDistance = distance
				end
			end
		end
		if face == Enum.NormalId.Back then
			if block:Resize(face, scale) then
				if block:Resize(Enum.NormalId.Front, -scale) then
					lastDistance = distance
				end
			end
		end
	end
end