Help with fixing bobblefunction bug

Hey guys,

I’ve ran into a bug with my code that I can’t seem to solve on my own. My situation is a little niche and so I could not find any relevant posts on the DevForum.

In my module, I have a bobble function that updates every frame. I want to scale the frequency of the bobble so that it matches the player’s speed, so I send a “speedFactor” variable that changes every renderstep based on the humanoid’s walkspeed.

The issue is, when the speedFactor changes, the bobble frequency changes erratically such that it appears super fast and glitchy before easing back to normal.

Here is the relevant part of the code, ignore the bobbleAlpha as I have tested it many times and it is not causing the issue:

local function bobble(speedFactor,bobbleAlpha)
	local X = (math.sin(tick() * 6.5 * speedFactor) * .02 )*bobbleAlpha
	local Y = (math.sin(tick() * 13 * speedFactor) * (.03)-.1 )*bobbleAlpha
	local XR = (math.cos(tick() * 6.5 * speedFactor) * .06 )*bobbleAlpha
	local YR = -(math.cos(tick() * 13 * speedFactor) * .06 )*bobbleAlpha
	
	local CF = CFrame.new(X/100,Y,X) * CFrame.Angles(math.rad(-YR * 15),math.rad(-X * 60), math.rad(XR * 30))
	return CF
end

function Module:Update()
	local speedFactor = math.clamp(humanoid.WalkSpeed / 22, 0, 1) 
	
	if humanoid.MoveDirection.Magnitude > 0 then
		self.bobbleAlpha = math.clamp(self.bobbleAlpha + 0.06, 0, 1.4)
	else
		self.bobbleAlpha = math.clamp(self.bobbleAlpha - 0.06, 0, 1.4)
	end

	self.viewmodel.PrimaryPart.CFrame = game.Workspace.CurrentCamera.CFrame * CFrame.new(0,0,0) * bobble(speedFactor,self.bobbleAlpha)
end

Any help is greatly appreciated. Thank you for your time.