Can Anyone Help me make my script Smoother?

Hello i have a head bobbing script but its very unsmooth, when someone stops running/Walking it instantly goes back to original instead of slowly Transition back, also I’m trying to make it where it changes speed of bobble by speed of player but as u can see it only changes speed by a specific number, any help making it slowly get faster as the player gets faster?


local playerModel = script.Parent
local humanoid = playerModel:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid1 = character.Humanoid


local function updateBobbleEffect()
	humanoid.CameraOffset = Vector3.new(0, 0, -0.59)
	local now = tick()
	if humanoid.MoveDirection.Magnitude > 0 then -- Are we walking?
		local velocity = humanoid.RootPart.Velocity
		if humanoid.WalkSpeed == 16 then 
			print("Walking")
		local bobble_X = math.cos(now * 3.5) / 1 -- changed speed of bobble
		local bobble_Y = nil -- this changed if the bobble goes up and down, i put it nil cuz i only want right and left bobble

		local bobble = Vector3.new(bobble_X,bobble_Y,-0.59) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
			humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble,.25)
		else
			if humanoid.WalkSpeed == 24 then 
				print("Running")
				local bobble_Y = nil -- this changed if the bobble goes up and down, i put it nil cuz i only want right and left bobble
				local Runbobble_X = math.cos(now * 5.5) / 1 -- changed speed of bobble
				local Runbobble = Vector3.new(Runbobble_X,bobble_Y,-0.59) * math.min(1, velocity.Magnitude / humanoid.WalkSpeed)
				humanoid.CameraOffset = humanoid.CameraOffset:lerp(Runbobble,.25)
	else
		-- Scale down the CameraOffset so that it shifts back to its regular position.
		
				humanoid.CameraOffset = humanoid.CameraOffset * 0.94 -- speed of change(90-99 are big big changed of speed)
			end
		end
	end
end

-- Update the effect on every single frame.
RunService.RenderStepped:Connect(updateBobbleEffect)

If yant it to smoothly go back then you could tween it back

I dont know for sure but did you try to use ez camera shake? its the same thing but it has some presets hope this helps EZ Camera Shake ported to Roblox