Resize along with position

So I made a script that resizes with the mouse distance and remains on its position. Everything works fine till I noticed that if I move the mouse fast then the position changes…

https://gyazo.com/d188671b715164731537eefbaa6a3fb6

MY CODE

local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	local part = Instance.new("Part",workspace)
	part.Anchored = true
	part.Name = "Part"
	part.Position = Mouse.Hit.Position
	local originPart = Instance.new("Part",workspace)
	originPart.Name = "OriginPart"
	originPart.Position = Mouse.Hit.Position
	originPart.Transparency = 1
	originPart.Anchored = true
	Mouse.Move:Connect(function()
		local CurrentXSize = part.Size.X --Recent X Size
		local distance = (Mouse.Hit.Position - originPart.Position).magnitude
		local PosDistance = (Mouse.Hit.Position - part.Position).magnitude
		local XSubtract = (Mouse.Hit.Position - originPart.Position).X
		local Direction = (Mouse.Hit.Position.X - originPart.Position.X)
		if Direction < 0 then
		    part.Size = Vector3.new(-XSubtract,part.Size.Y,part.Size.Z)
		    part.Position = part.Position - Vector3.new((part.Size.X - CurrentXSize)/2,0,0)
		elseif Direction > 0 then
		    part.Size = Vector3.new(XSubtract,part.Size.Y,part.Size.Z)
		    part.Position = part.Position + Vector3.new((part.Size.X - CurrentXSize)/2,0,0)			
		end
	end)
end)

I was just practicing trying to make formulas without help since I want to improve on it but this is the point that I gave up.

You have to make axis on which is moving your part constant and shouldn’t be changing.

1 Like