RunService causing viewmodel to jitter when shooting

I am making a weapons framework for a game, and have just completed the recoil function. It works, but only when the player is standing still. if the player is shooting their gun and moves in any direction, or even just moves their camera, the viewmodel starts glitching around:

I presume this has something to do with the runservice function later down the script, which sets the viewmodel’s primary part to the camera’s cframe on every renderstep. I just dont know how to fix it.

Heres the script:

vmStep = runService.RenderStepped:Connect(function() -- viewmodel to camera CFrame
	local mouseDelta = UIS:GetMouseDelta()
	if mouseDelta.X > 50 or mouseDelta.X < -50 then 
		if mouseDelta.X > 50 then 
			local overshotX = mouseDelta.X - 50 
			mouseDelta = mouseDelta -, 0)
			local overshotX = mouseDelta.X + 50 
			mouseDelta = mouseDelta -, 0)		
	local swayCalc = / 250, mouseDelta.Y / 100, 0)
	viewModel.HumanoidRootPart.CFrame = cam.CFrame
	viewModel.HumanoidRootPart.CFrame *=

The recoil function has a .03 second tweentime for the camera tween, which I will also attach here:

function recoilFunction()
	local tI =,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)	
	local toPosCalc = {
		CFrame = cam.CFrame * CFrame.Angles(math.rad(recoilX),math.rad(math.random(recoilY1,recoilY2)),math.rad(zRot)) -- x to shake up, y to shake sideways.
	if zRot > 0 then
		zRot = math.random(recoilZ2*-1,recoilZ1*-1)
		zRot = math.random(recoilZ1,recoilZ2)
	local tween = tweenService:Create(workspace.CurrentCamera,tI,toPosCalc)

Seems like this is an issue with using tween service on the camera, I wouldn’t recommend using tween service on the camera, its causing the camera to lag behind. You would have to find an alternative way of doing recoil.

1 Like

Idk if the recoilFunction is constantly running in a loop or only runs when the weapon is fired, however if the recoilFunction is constantly running in like RenderStepped or something that might be a problem. So yeah I gotta agree with Jojo

Also I notice that you use MouseDelta for the VMsway → which means the VM will kinda move because when you rotate your camera, your mouse also move → there will be mouseDelta

  • mouseDelta can even be calculated or detected even when the mouse is locked

personally, instead of using MouseDelta to calculate the swayCalc, I would prefer to use the difference between of the current camera frame to the last camera frame, so it might look something like:

local lastcamera_CF =
local swayCalc =
local CameraSwaySmoothness = 0.25 -- the smaller the smoother and slower, the bigger the faster
local currentswayAMT = -0.2 -- currently negative (i mean you can play around and tweak so you know what it does)
vmStep = runService.RenderStepped:Connect(function() -- viewmodel to camera CFrame
    local frame_diff = camera.CFrame:ToObjectSpace(lastcamera_CF)
    local X,Y,Z = frame_diff:ToOrientation()
    swayCalc = swayCalc:Lerp(CFrame.Angles(math.sin(X) * currentswayAMT, math.sin(Y) * currentswayAMT, 0), CameraSwaySmoothness)

	viewModel.HumanoidRootPart.CFrame = cam.CFrame * swayCalc
    --save you less line ig?
       lastcamera_CF = camera.CFrame

try it and let me know if it works or not :ok_hand:. And of course this code is not made by me :sob:, I learnt it from another very talented, and big brain developer

alright, I will try it out ssssssssssssss

The function only runs when the weapon is fired, I have previously tried incasing the function in a renderstep function but it was unoptimized and also threw off all the math in the function

No, now i think there’s nothing wrong with the recoilFunction so you’re good. No need to change anything in recoilFunction

I mean… it kinda works? It just looks really akward, also I have no idea how any of this works so I cant really edit it or integrate it into other things

play around with CameraSwaySmoothness and currentswayAMT you will see how it works
and btw since that thing moving pretty slow, you might want to up the CameraSwaySmoothness value.
Also you might want to change the currentswayAMT from negative to positive since by looking at your video i can tell that they way the VM swaying is inversed

I see. Thanks for the script, but this doesnt answer my question still, I guess ill try doing recoil with lerp or something

1 Like

Did it fix the problem when you start shooting the gun or no?

Sorry, I dont understand the question

Did it fix the glitching problem or no?

No, im trying to replace the tween with a lerp instead

1 Like