Help with viewmodel arm spinning

I am working on a viewmodel system, and I want to rotate the arms in the same script where I set the viewmodels CFrame to the camera’s each frame. This means I am updating the viewmodel’s rotation every frame, and with the conventional means of rotation by multiplying a CFrame with CFrame.Angles, it makes the arm start spinning since its rotation is increasing each frame.

Code:

local Players = game.Players
local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local char = plr.Character
local cam = workspace.CurrentCamera

local gun = script.Parent

local viewmodel = plr.PlayerGui.Viewmodel
local gunmodel: Model = plr.PlayerGui:FindFirstChild(gun.Name)

local head: Part = char:WaitForChild("Head")


RunService.Heartbeat:Connect(function(dt)	
	if Players:GetPlayerFromCharacter(gun.Parent) then
		viewmodel.Parent = cam
		viewmodel:PivotTo(cam.CFrame)
		
		gunmodel.Parent = cam
		gunmodel:PivotTo(viewmodel.RightArm.Hand.WorldCFrame)
			
		local rarm = viewmodel.RightArm:GetPivot() * CFrame.Angles(math.rad(0), math.rad(5), math.rad(0))
		--// ^ Issue ^								^ Arm is being rotated by 5 degrees each frame ^

		viewmodel.RightArm:PivotTo(rarm)
	else
		viewmodel.Parent = plr.PlayerGui
		gunmodel.Parent = plr.PlayerGui
	end
end)

The solution I came up with would be to set the CFrame’s rotation each frame, not multiply it, but the issue is I don’t know how to do that.

Thanks.

Essentially you’re redefining the pivot every single time you rotate it. Try defining rarm above the heartbeat function, and then multiply the cframe while setting it.

I’m trying that and the arm stays in one spot while the rest of the viewmodel moves with the camera.