Headbobbing script is very performance heavy

So I’m making a game with headbobbing involved and I want to make the game look nice so I had the headbobbing rotate as well but the problem is that this is VERY performance heavy

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local running = false
local speed = character.Humanoid.WalkSpeed/2
local RunService = game:GetService("RunService")

local camerabobcf = CFrame.new()

local movingModifier = 0.05

RunService.RenderStepped:Connect(function(dt)
	camera.CFrame = camera.CFrame
		* camerabobcf
	
	local currTime = tick()
	
	if running then
		local bobbleX = math.cos(currTime * speed) * .25
		local bobbleY = math.abs(math.sin(currTime * speed)) * .25
		
		local bobble = Vector3.new(bobbleX,bobbleY,0)
		
		local bobbleRotate = CFrame.Angles(0, 0,
			math.sin(currTime * speed) * 0.25 * movingModifier
		)
		
		camerabobcf = camerabobcf:Lerp(CFrame.new(bobble) * bobbleRotate, 0.1)
	else
		local idleBobble = Vector3.new(
			math.cos(currTime * 0.3) * .25,
			math.abs(math.sin(currTime * 0.3)) * .25,
			0
		)

		local idleBobbleRotation = CFrame.Angles(0, 0, 
			math.rad(math.sin(currTime * 2) * .25)
		)
		
		camerabobcf = camerabobcf:Lerp(CFrame.new(idleBobble) * idleBobbleRotation, 0.05)
	end
end)

character.Humanoid.Running:Connect(function(WalkSpeed)
	if WalkSpeed > 0.1 then
		running = true
	else
		running = false
	end
end)

character.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	speed = character.Humanoid.WalkSpeed/2
end)

Try making this into heartbeat instead.

It stills cause a considerable amount of lag after changing it

Rewrote your script and baked as much math as I could while still preserving the desired head bobble effect. Also added in an optimization feature where if the player stops moving, it stops calculating the default head bobble.

local CurrentCamera = game:GetService("Workspace").CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Running = false
local Speed = Character.Humanoid.WalkSpeed/2
local CameraBobCFrame = CFrame.new()
local StillCount = 0

game:GetService("RunService").RenderStepped:Connect(function()
	local CurrentTime = tick()
	CurrentCamera.CFrame *= CameraBobCFrame
	
	if Running then
		local RelativeSpeed = CurrentTime * Speed
		CameraBobCFrame = CameraBobCFrame:Lerp(CFrame.new(Vector3.new(math.cos(RelativeSpeed) * .25, math.abs(math.sin(RelativeSpeed)) * .25, 0)) * CFrame.Angles(0, 0, math.sin(RelativeSpeed) * 0.0125), 0.1)
		StillCount = 0
	else
		if StillCount > 25 then
			return
		end

		local RelativeSpeed = CurrentTime * 0.3
		StillCount += 1
		CameraBobCFrame = CameraBobCFrame:Lerp(CFrame.new(Vector3.new(math.cos(RelativeSpeed) * .25, math.abs(math.sin(RelativeSpeed)) * .25, 0)) * CFrame.Angles(0, 0, math.rad(math.sin(CurrentTime * 2) * .25)), 0.05)
	end
end)

Character.Humanoid.Running:Connect(function(WalkSpeed)
	if WalkSpeed > 0.1 then
		Running = true
	else
		Running = false
	end
end)

Character.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
	Speed = Character.Humanoid.WalkSpeed / 2
end)