Viewmodel swaying that pivots around the handle?

I have been trying to make a sway system similar to COD (The one where the sway pivots around the handle if it is not COD and I’m wrong), but I can never get it to work. I have looked on websites, asked AI, but nothing seemed to work and my math is really bad.

The viewmodel when repositioning is offsetted and moves either forward or backwards when swaying side to side. The PrimaryPart is where the viewmodel pivots to the camera instead of the ACS framework where the handle is the primary part.

Here is the script I have been working on:

local function UpdateSway(delta)
	-- // Framerate
	local Speed = math.min(1, 60 * delta)

	-- // Rot Calculation
	local rot = CurrentCamera.CFrame:ToObjectSpace(LastCamCF)
	local X, Y, Z = rot:ToOrientation()
	local Max = 0.025

	-- // Update spring
	local swayX = math.clamp(math.sin(Y * 1.5), -Max, Max) * Speed
	local swayY = math.clamp(math.sin(X * 1.5), -Max, Max) * Speed
	local swayZ = 0

	local sway = Vector3.new(swayY, swayX, swayZ)

	local JointOffset = Vector3.new(4.409621715545654, 0.8226121664047241, 1.5469993352890015)

	-- Calculate the sway rotation
	local swayRotation = CFrame.Angles(sway.X, sway.Y, sway.Z)

	-- Apply rotation to joint offset
	local rotatedOffset = swayRotation:VectorToWorldSpace(JointOffset)

	-- Calculate the offset CFrame
	local OffsetCF = CFrame.new(rotatedOffset) * swayRotation * CFrame.new(-JointOffset)

	if SwaySpringPos and SwaySpringRot then
		SwaySpringPos.shove(OffsetCF.Position)
		SwaySpringRot.shove(sway)

		local SpringRot = SwaySpringRot.update(delta) * (IsAiming and 1/Framework.AimSpringDampening or 1)
		local SpringPos = SwaySpringPos.update(delta) * (IsAiming and 1/Framework.AimSpringDampening or 1)

		-- Calculate the final ViewmodelSwayCF
		local finalSwayCF = CFrame.Angles(SpringRot.X, SpringRot.Y, SpringRot.Z)
		ViewmodelSwayCF = finalSwayCF * CFrame.new(SpringPos)
	end

	LastCamCF = CurrentCamera.CFrame
end

I found the solution, to those who want to know what it was, here is is:

local function GetOffsetFromHandle(Rotation : Vector3)
	local HandleOffset = Framework.Settings.ViewmodelPhysics.HandleOffset

	return -(CFrame.new(HandleOffset) * CFrame.Angles(Rotation.X, Rotation.Y, Rotation.Z) * CFrame.new(-HandleOffset)).Position
end

local function UpdateSway(delta)
	-- // Framerate
	local Speed = math.min(1, 60 * delta)

	-- // Rot Calculation
	local rot = CurrentCamera.CFrame:ToObjectSpace(LastCamCF)
	local X, Y, Z = rot:ToEulerAnglesXYZ()
	local MaxX, MaxY, MaxZ = 0.025, 0.025, 0

	-- // Update spring
	local swayX = math.clamp(X * 1, -MaxX, MaxX) * Speed
	local swayY = math.clamp(Y * 1, -MaxY, MaxY) * Speed
	local swayZ = math.clamp(Z * 0, -MaxZ, MaxZ) * Speed

	local sway = Vector3.new(swayX, swayY, swayZ)

	if SwaySpringPos and SwaySpringRot then
		SwaySpringRot.shove(sway)

		local SpringRot = SwaySpringRot.update(delta) * (IsAiming and 1/Framework.AimSpringDampening or 1)
		local rotationCF = CFrame.fromEulerAnglesXYZ(SpringRot.X, SpringRot.Y, SpringRot.Z)

		local offsetPosition = GetOffsetFromHandle(SpringRot)
		ViewmodelSwayCF = CFrame.new(offsetPosition) * rotationCF
	end

	LastCamCF = CurrentCamera.CFrame
end