Recoil Script Bug

Hello ROBLOX DevFourm. I am making a recoil script for my gun. It works well while the player is standing still, however when I walk and shoot everything breaks. Help would be greatly appreciated.

Recoil Script

local function applyRecoil()
	local elapsed = 0
	local initialRotation = camera.CFrame

	coroutine.wrap(function()
		while elapsed < recoilDuration do
			local deltaTime = task.wait()
			elapsed = elapsed + deltaTime

			local angleX = math.rad(math.random() * recoilIntensity+0.4)
			local angleY = math.rad(math.random() * recoilIntensity)
			local angleZ = math.rad(math.random() * recoilIntensity)

			local recoilRotation = CFrame.Angles(angleX, angleY, angleZ)
			camera.CFrame = initialRotation * recoilRotation

			task.wait()
		end
	end)()

Video

1 Like

This might work:

 local function applyRecoil()
	local elapsed = 0

	coroutine.wrap(function()
		while elapsed < recoilDuration do
			local initialRotation = camera.CFrame
			local deltaTime = task.wait()
			elapsed = elapsed + deltaTime

			local angleX = math.rad(math.random() * recoilIntensity+0.4)
			local angleY = math.rad(math.random() * recoilIntensity)
			local angleZ = math.rad(math.random() * recoilIntensity)

			local recoilRotation = CFrame.Angles(angleX, angleY, angleZ)
			camera.CFrame = initialRotation * recoilRotation

			task.wait()
1 Like

That works. Thank you very much!

1 Like

No problem! You’re welcome. I’m surprised it worked, haha.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.