Gun system's aiming messed up

I would like to get aiming inside of this script to work correctly, but the script ends up breaking or not working, I cannot figure out why the attached script refuses to aim in. I have looked around quite a bit, to try to resolve said issue.
(This is supposed to be a gun engine. The code provided is a client required module.)

local RS = game:GetService('RunService')
local UIS = game:GetService('UserInputService')

local plrs = game.Players
local plr = plrs.LocalPlayer
local cam = workspace.CurrentCamera
local mouse = plr:GetMouse()

local models = game.ReplicatedStorage["BGS Replicated"].vpModels

local mb2DOWN = false
local mb1DOWN = false

local connection = nil

local equipped = false

local module = {}

local function positionViewModel(g, conf)
	local gunOffset = conf.offset
	local lastcamcf = cam.CFrame
	local targetcf = CFrame.new()
	local swayOffset = CFrame.new()
	local swaySize = 1
	local walking = false
	local camPart = Instance.new('Part',cam)
	camPart.Size = Vector3.new(1,1,1)
	camPart.Anchored = true
	--camPart.Transparency = 1
	camPart.CanCollide = false
	
	

	local char = plr.Character or plr.CharacterAdded:Wait()
	if char then
		local hum = char:FindFirstChildWhichIsA('Humanoid')

		if hum then

			local shirt = char:FindFirstChildWhichIsA('Shirt')
			local bodyColors = char:FindFirstChildWhichIsA('BodyColors')

			shirt:Clone().Parent = g.ArmModel
			bodyColors:Clone().Parent = g.ArmModel

			connection = RS.RenderStepped:Connect(function(dt)
				camPart.CFrame = cam.CFrame
				
				for i,v in pairs(g:GetDescendants()) do
					if v:IsA('BasePart') then
						v.CanCollide = false
					end
				end

				if hum.MoveDirection.Magnitude > 0.1 then
					walking = true
				else
					walking = false
				end

				local mainCamPart = g.GUN.POINTS:FindFirstChild("MAINCAM")
				--local camPart = g.GUN.POINTS:FindFirstChild("CAM")
				local aimPart = g.GUN.POINTS:FindFirstChild("AIM")
				local aimOffset = aimPart.CFrame:Inverse() * camPart.CFrame --* CFrame.new(-0.2473459243774414,0,0)
				--aimOffset = aimOffset:Inverse()

				if not mainCamPart or not aimPart then
					warn("MAINCAM or AIM part not found in POINTS")
					return
				end
				--[[if mb2DOWN then
					aimPart.CFrame = cam.CFrame
				else
					mainCamPart.CFrame = cam.CFrame
				end]]

				local walkSin = math.sin(time() * 2 * 4) / 25
				local walkCos = math.cos(time() * 3 * 4) / 20
				local walkSinRot = math.sin(time() * 2 * 4) / 25
				local walkCosRot = math.sin(time() * 3 * 4) / 20

				local x, y, z = (cam.CFrame:ToObjectSpace(lastcamcf)):ToOrientation()
				swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x / 2) * swaySize, math.sin(y / 2) * swaySize, 0), .1)

				local walkCFOffset = CFrame.new(walkSin, walkCos, 0) * CFrame.fromEulerAnglesXYZ(0, 0, walkSinRot)

				if mb2DOWN then
					walkCFOffset = CFrame.new(walkSin / 10, walkCos / 10, 0)
				end

				if walking and not mb2DOWN then
					targetcf = targetcf:Lerp(gunOffset * walkCFOffset * swayOffset, 0.1)
				elseif not walking and not mb2DOWN then
					targetcf = targetcf:Lerp(gunOffset * swayOffset, 0.1)
				end

				if walking and mb2DOWN then
					targetcf = targetcf:Lerp(walkCFOffset * swayOffset * aimOffset, 0.1)
				elseif not walking and mb2DOWN then
					targetcf = targetcf:Lerp(swayOffset * aimOffset, 0.1)
				end

				lastcamcf = cam.CFrame

				if mb2DOWN then
					print(aimOffset.Position)
					mainCamPart:PivotTo(cam.CFrame*targetcf)
				else

					mainCamPart:PivotTo(cam.CFrame*targetcf)
				end


				--[[if mb2DOWN then
					g:PivotTo(targetcf * aimPart.CFrame:ToObjectSpace(mainCamPart.CFrame))--g:PivotTo(aimPart.CFrame * targetcf * aimPart.CFrame:ToObjectSpace(mainCamPart.CFrame))
				else
					g:PivotTo( targetcf)--g:PivotTo(mainCamPart.CFrame * targetcf)
				end]]
			end)
		end
	end
end

function module.Equip(gName)
	local gun = models:FindFirstChild(gName)
	mouse.Icon = ' '

	if not gun then
		return
	end

	local gun = gun:Clone()
	gun.Parent = cam

	game.ReplicatedStorage["BGS Replicated"].Remotes["equip tool"]:FireServer()

	plr.CameraMode = Enum.CameraMode.LockFirstPerson
	
	equipped = true

	positionViewModel(gun, require(gun.conf))
	
	while equipped do
		task.wait()
		if mb1DOWN then
			game.ReplicatedStorage["BGS Replicated"].Remotes["fire tool"]:FireServer(gun.GUN.POINTS.SHOOT.CFrame)
		end
	end
end

function module.Unequip()
	connection:Disconnect()
	mouse.Icon = ''

	for i,v in pairs(cam:GetChildren()) do
		v:Destroy()
	end

	equipped = false

	plr.CameraMode = Enum.CameraMode.Classic
end

UIS.InputBegan:Connect(function(k, gpe)
	if gpe then
		return
	end

	if k.UserInputType == Enum.UserInputType.MouseButton2 then
		mb2DOWN = true
	end

	if k.UserInputType == Enum.UserInputType.MouseButton1 then
		mb1DOWN = true
	end
end)

UIS.InputEnded:Connect(function(k, gpe)
	if gpe then
		return
	end

	if k.UserInputType == Enum.UserInputType.MouseButton2 then
		mb2DOWN = false
	end
	
	if k.UserInputType == Enum.UserInputType.MouseButton1 then
		mb1DOWN = false
	end
end)

return module

I am attempting to get this line working:

local aimOffset = aimPart.CFrame:Inverse() * camPart.CFrame

(It kinda works? It just moves around 1/2 of the way towards the camera.)

Either way, if you can help, that would be great!

Is this just too difficult to help with? Did I type something wrong?

Maybe showing a video could help us see the problem clearer

Maybe try:

local aimOffset = aimPart.CFrame:ToObjectSpace(camPart.CFrame)


Here is an example of the gun failing to zoom fully.

Okay, I will attempt to add that in.

This code snippet has no effect. The gun still moves incorrectly towards the aim part.

I have looked into this, and have not managed to get the system to work correctly.
If anybody can help, please say something.

(bump)

I still require assistance on this topic. I have no idea what I’m doing wrong. If anybody can help that would be great.

I solved something similar before, but it uses CameraOffset which COULD be inconvenient depending on what you Want, but it’s REALLY easy to change that.

The code itself is pretty straight forward, basically it’ll get the CFrame offset from the AimPart to the Head to then set the Humanoid.CameraOffset to the Position of the returned CFrame Offset. It shouldn’t be hard to edit this, you can use the same method and get the CFrame Offset between the Camera and the AimPart. And after that, you could just set the ViewModel Offset to it.