Help simplifying/optimizing this monster of a function

I have this monster of a function here:

function ResizeOneAxisAsymmetrically(part:PartOperation, normalId, deltaAmount)

    local origExtentsCFrame = part.ExtentsCFrame

	if normalId == Enum.NormalId.Top        then -- 1
		
		if part.Size.Y + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.Y	
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(0, deltaAmount, 0)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(0, deltaAmount / 2, 0)

	elseif normalId == Enum.NormalId.Bottom then -- 4

		if part.Size.Y + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.Y
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(0, deltaAmount, 0)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(0, -deltaAmount / 2, 0)

	elseif normalId == Enum.NormalId.Front  then -- 5

		if part.Size.Z + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.Z
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(0, 0, deltaAmount)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(0, 0, -deltaAmount / 2)

	elseif normalId == Enum.NormalId.Back   then -- 2

		if part.Size.Z + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.Z
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(0, 0, deltaAmount)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(0, 0, deltaAmount / 2)

	elseif normalId == Enum.NormalId.Left   then -- 3

		if part.Size.X + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.X
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(deltaAmount, 0, 0)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(-deltaAmount / 2, 0, 0)

	elseif normalId == Enum.NormalId.Right  then -- 0

		if part.Size.X + deltaAmount < GridSize.Value then	
			deltaAmount = GridSize.Value - part.Size.X
			if boxes[part] ~= nil then	
				boxes[part].Color3 = Color3.new(1,1,0)
			end
		else
			if boxes[part] ~= nil then	
				boxes[part].Color3 = selectionBoxDefaultColor
			end
		end
		part.Size   += Vector3.new(deltaAmount, 0, 0)
		part.CFrame *= origExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(deltaAmount / 2, 0, 0)

	end

end

Can y’all help me simplify it with functions and/or optimize it? Most of it repeats except for a few changes here and there.

Thanks!

1 Like

I made this shorter version of your function.

local directions: {[Enum.NormalId] : Vector3} = {
	[Enum.NormalId.Top] = Vector3.new(0,1,0),
	[Enum.NormalId.Bottom] = Vector3.new(0,-1,0),
	[Enum.NormalId.Back] = Vector3.new(0,0,1),
	[Enum.NormalId.Front] = Vector3.new(0,0,-1),
	[Enum.NormalId.Right] = Vector3.new(1,0,0),
	[Enum.NormalId.Left] = Vector3.new(-1,0,0),
}

local function ResizeOneAxisAsymmetrically(part:PartOperation, normalId:Enum.NormalId, deltaAmount:number)

	local direction: Vector3 = directions[normalId]
	local absDirection: Vector3 = direction:Abs()

	local partSize: Vector3 = part.Size * absDirection

	local deltaVec3: Vector3


	if partSize.Magnitude + deltaAmount < GridSize.Value then

		deltaVec3 = GridSize.Value * absDirection - partSize

		if boxes[part] then
			boxes[part].Color3 = Color3.new(1,1,0)
		end

	else

		deltaVec3 = deltaAmount * absDirection

		if boxes[part] then
			boxes[part].Color3 = selectionBoxDefaultColor
		end
	end


	part.Size   += deltaVec3
	part.CFrame *= part.ExtentsCFrame * part.ExtentsCFrame:Inverse() * CFrame.new(direction * deltaVec3 / 2)

end

But it’s probably not faster.

2 Likes

How can I check if it’s faster or not? Can I put prints somewhere?

1 Like

You could do something along the lines of:

local start = os.clock()
...
print(os.clock() - start)