Any help with head bobbing issue?

So I have this script that you put into a tool, and when equipped it moves your camera with your mouse, but when you unequip it, it does this head bobbing thing, and also after a few minutes, the script stops working entirely, no output errors either, any help?

local tool = script.Parent
local service = game:GetService("RunService")

local success = false

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")

local camera = game.Workspace.CurrentCamera
local mouse = player:GetMouse()

local offset, magnitude, vector3, cFrame, direcction = nil
local shoulder, arm, torso, body, neck = nil
local defaultNeckCO, defaultShoulderCO = nil
local animate = nil

local shoulderPositionC0, shoulderCFramePositionC1 = nil
local turnAngle = CFrame.fromEulerAnglesXYZ(math.pi/2, 0, 0)
local rightangle = CFrame.fromEulerAnglesXYZ(0, 0, math.pi)

local unequipListener

function updateOffset()
	offset = (body.Position.y - mouse.Hit.p.y) / 100
	magnitude = (body.Position - mouse.Hit.p).magnitude / 80
	return offset/magnitude
end

function updateBodyRotation()
	
	if (camera.Focus.p - camera.CoordinateFrame.p).magnitude > 1 then
		humanoid.AutoRotate = false
		
		vector3 = Vector3.new(mouse.Hit.p.x, body.Position.y, mouse.Hit.p.z)
		
		cFrame = CFrame.new(body.Position, vector3)
		
		body.CFrame = cFrame
	else
		humanoid.AutoRotate = true
	end
	
end

function animateR15()
end


function animateR6()
end

function getBodyParts()
	
	body = character.HumanoidRootPart
	
	if humanoid.RigType == Enum.HumanoidRigType.R15 then
		shoulder = character.RightUpperArm.RightShoulder
		arm = character.RightUpperArm
		neck = character.Head.Neck
		torso = character.UpperTorso

		animate = animateR15
		
		defaultNeckCO = CFrame.new(0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
		shoulderCFramePositionC1 = CFrame.new(shoulder.C1.p)
		shoulderPositionC0 = shoulder.C0.p
	else
		shoulder = character.Torso["Right Shoulder"]
		arm = character["Right Arm"] 
		neck = character.Torso.Neck
		
		animate = animateR6
		
		defaultNeckCO = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
		defaultShoulderCO = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
	end

end

function stopAllFunctions()
	
	pcall(function()
		unequipListener:Disconnect()
	end)
	
	pcall(function()
		service:UnbindFromRenderStep(script.Name)
	end)
		
	pcall(function()

		neck.C0 = defaultNeckCO
	end)
		
	pcall(function()
		humanoid.AutoRotate = true
	end)
	
	pcall(function ()
		if humanoid.RigType == Enum.HumanoidRigType.R15 then
			shoulder.Part1 = arm
			arm.Anchored = false
		else
			shoulder.C0 = defaultShoulderCO
		end
	end)

end

function characterFollowTool()

	success = pcall(function()
		updateBodyRotation()
		
		animate()
	end)
	
	if not success then
		stopAllFunctions()
	end
	
end

function onEquip()
	unequipListener = tool.Unequipped:connect(stopAllFunctions)

	success = pcall(getBodyParts)
	if not success then
		stopAllFunctions()
	end

	service:BindToRenderStep(script.Name, Enum.RenderPriority.Input.Value - 1, characterFollowTool)
end

tool.Equipped:connect(onEquip)