Resize tool issue with custom incrementation

Hello there, I have an issue with my resize tool. It works perfectly when the part has a rotation of Vector3.new(0, 0, 0) but as soon as it changes, it messes and does not resize as I wish.

1 - When there is no problems
Here, you can see that my part is being resized properly, note its orientation property.
https://gyazo.com/b934127a1c3538c9838a2fc2d74b1c8f

2 - When there are some issues
In this case, my part has an orientation ~= Vector3.new(0, 0, 0), and it does conflict with the current system, I have this as a result:
https://gyazo.com/a109b2b84290a8321b9c51ae1ea3df5f

3 - The script I used to make this system alive
Well, I don’t know how to explain my script. I am not using Basepart:Resize() since I don’t know how to add an incrementation system with it. Instead, I am using the Part.Size property, but you may know that when doing that, it resizes the part from its center. So, to fix this, I had to change the position of the part so as to have the desired system.

You can ask me some deeper pieces of information if you want (or need), here is the script:
(note that I tried my best to explain what I intended to do)

--[Varonex]--
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Mouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local commonS = PlayerGui:WaitForChild("Common") --All the handle adornments are stored in this

local rEvents = game.ReplicatedStorage:WaitForChild("rEvents")
local partComm = rEvents:WaitForChild("partInfoCommunicator") --RemoteEvent to transfer data to server

local sizeHandle = commonS:WaitForChild("sizeHandle") --The handler that is used to resize any part

local currentTool = script.Parent:WaitForChild("currentTool") --There are other tools such as rotate, ... but I removed useless parts

local selectedInstance = nil --Instance selected when clicking
local mouseOnAdornment = false --Is used to see if the mouse is on any adornment so as to prevent the part selection to fire
local tempInstance = nil --I don't remember what this variable is for but whatever

local moving = false
local resize = false
local rotate = false

local increment = script.Parent:WaitForChild("studs").Value --Incrementation, in studs
local degree = script.Parent:WaitForChild("degree").Value --Rotation, in degree

local Pos, sze, CF --Variables used to store stuff

local previousSizeDistance = 0 --I don't really know how to explain this one

--Convert NormalId
function convertNID(NormalId, part) --This is to convert the NormalId to the different vectors (LookVector, ...)
	if NormalId == Enum.NormalId.Front then
		return part.CFrame.LookVector, true
	elseif NormalId == Enum.NormalId.Back then
		return -part.CFrame.LookVector, false
	elseif NormalId == Enum.NormalId.Right then
		return part.CFrame.RightVector, true
	elseif NormalId == Enum.NormalId.Left then
		return -part.CFrame.RightVector, false
	elseif NormalId == Enum.NormalId.Top then
		return part.CFrame.UpVector, true
	elseif NormalId == Enum.NormalId.Bottom then
		return -part.CFrame.UpVector, false
	end
end

function convertVectorLength(Vector) --Convert the Vector that we optained with the upper function with the only possibility to have 1 or 0 for each axis
	if Vector.X and Vector.X ~=1 and Vector.X ~= 0 then
		Vector = Vector3.new(1, Vector.Y, Vector.Z)
	end
	if Vector.Y and Vector.Y ~= 1 and Vector.Y ~= 0 then
		Vector = Vector3.new(Vector.X, 1, Vector.Z)
	end
	if Vector.Z and Vector.Z ~= 1 and Vector.Z ~= 0 then
		Vector = Vector3.new(Vector.X, Vector.Y, 1)
	end
	return Vector
end

--resize
sizeHandle.MouseButton1Down:Connect(function() --Store these pieces of information so as to make the resizer work properly
	if selectedInstance ~= nil then
		Pos = selectedInstance.Position
		sze = selectedInstance.Size
	end
end)
sizeHandle.MouseButton1Up:Connect(function() --Transfer the new size, ... to the server
	if selectedInstance ~= nil then
		partComm:FireServer("Size", selectedInstance, selectedInstance.Size.X, selectedInstance.Size.Y, nil, selectedInstance.Position.X, selectedInstance.Position.Y, nil)
	end
end)
sizeHandle.MouseDrag:Connect(function(NormalId, distance)
	print(NormalId, distance)
	if selectedInstance ~= nil then
		if increment ~= 0 then
			distance = distance - (distance%increment)
		end
		if previousSizeDistance ~= distance then
			local normalVector, ratio = convertNID(NormalId, selectedInstance) --Getting the vector from the NormalId
			local vectorUnited = convertVectorLength(normalVector) --Making the upper vector with a length of 1 for each axis
			
			selectedInstance.Size = sze + vectorUnited * distance --I am resizing the part from its center. So it growths on both sides
			
			selectedInstance.Position = Pos + (vectorUnited * distance) / 2 --As we resize from the center, we have to reposition the half of the recize so as to
			
			previousSizeDistance = distance
		end
	end
end)
--[Varonex]--

Note that the script is not fully put, some other functions are present but I decided to remove them since they don’t have any influence on the resize tool.

I have an idea of what can be the problem but I tried some other solutions that did not work. I guess it glitches since I am using LookVector, UpVector and RightVector but I don’t really know what to do to fix that.

I tried to find some solutions on this forum, but they were either speaking about Basepart:Resize() or had the same issue. I even tried to take some samples of solutions that was given here, but unfortunately the mistake is still present, even with this solution !

Thanks for having read this thread,

Varonex_0

1 Like

convertVectorLength doesn’t seem right to me. It outputs garbage.

Consider this: to resize the part properly, you need to add 1 to the axis that you’re resizing (in local space), and move the part half a unit in the direction it is being resized (in world space)

You already have the direction in the world it is being resized toward! It is normalVector. You can replace the vectorUnited in selectedInstance.Position with normalVector right now to see it going in the correct direction.

To “add 1 to the axis that you’re resizing”, you will need to turn the NormalId into a vector that faces that face (in local space). So for Front, you would use Vector3.new(0, 0, 1). This is your real vectorUnited.

If the part is not rotated (Orientation is 0, 0, 0), then both the local and world space directions will be the same.

By “world space” vs “local space” I mean “relative to the world origin” vs “relative to the part”. So the forward direction in local space would be (0, 0, 1), but vary wildly in world space, but if you took the lookVector (world space forward direction) and added it to the Position (world position) of the part, you would get a Vector3 that’s exactly 1 stud in front of the part’s center point. If you added (0, 0, 1) to the Position instead, you would get something unrelated, unless the Part’s Orientation were (0, 0, 0) anyway.

EDIT: I fixed the sample I gave in the first post according to what you’ve said @Eestlane771 and it works properly !

I don’t know how to thank you !!