How do I interpolate this sway value?

Hey,

I’ve been trying to interpolate a weapon sway value for my new fps project and I can’t seem to find any way to do so because it is under RenderStepped. Here’s an example of my current code:

local function CosineInterpolate(a, b, t)
	local t2 = (1 - math.cos(t * math.pi)) / 2;
	return(a * (1 - t2) + b * t2);
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
	cf = workspace.CurrentCamera.CFrame
	ox, oy, oz = cf:ToOrientation()
	pox, poy, poz = oldcf:ToOrientation()
	for i = 1, 5 do
		offsety = CosineInterpolate(oldoffsety, oy - poy, i)
		offsetx = CosineInterpolate(oldoffsetx, ox - pox, i)
		vm.HumanoidRootPart.CFrame = (workspace.CurrentCamera.CFrame*CFrame.Angles(0,math.rad(180),0))*CFrame.new(Vector3.new(0,-.2,-.9))*CFrame.Angles((offsetx), (offsety)*-1, 0)
	end
	oldoffsety, oldoffsetx = offsety, offsetx
	oldcf = cf
	
end)

And here’s a video of the script working:

So I just want to smooth out the jittery bits in the swaying. How would I accomplish this?

Try using a while loop or Runservice.Hearbeat?