Camera stops being affected by script, but it still runs with no errors

Hi, I have a LocalScript placed in StarterCharacterScripts and it makes the camera wobble and lean in the direction the player’s character is moving. It is active constantly while the player is alive, and after death a new one is placed in the new character instance.

The script works fine most of the time, but at seemingly random times (only when the player is in first person though) the wobbling and leaning completely stop, yet no errors are printed, and the script still runs. The only way to get it to work again is to disable and re-enable the script, which makes me think it’s something to do with the variables getting changed.

Can someone please help me with this, I’m genuinely clueless as to what the problem is, since the output remains completely clear and the function continues running after the effect stops working.

Any help is appreciated.

Here’s the script:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")

local Mouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TouchEnabled = UserInputService.TouchEnabled

local Tilt = 0
local AngleX = 0
local X = 0
local Y = 0
local VX = 0
local VY = 0
local SX = 10
local SY = 10

local function Lerp(V1, V2, T)
	return V1 + (V2 - V1) * T
end

local function OnRenderStepped(DT)
	DT *= 60
	
	local WalkSpeed = Humanoid.WalkSpeed
	local Velocity = Vector3.new(RootPart.Velocity.X, 0, RootPart.Velocity.Z).Magnitude
	
	if DT > 2 then
		VX = 0
		VY = 0
	else
		VX = Lerp(VX, math.cos(tick() * 0.5 * math.random(10, 15)) * (math.random(5, 20) / 200) * DT, 0.05 * DT)
		VY = Lerp(VY, math.cos(tick() * 0.5 * math.random(5, 10)) * (math.random(2, 10) / 200) * DT, 0.05 * DT)
	end
	
	Camera.CFrame *= CFrame.fromEulerAnglesXYZ(0, 0, math.rad(AngleX))
		* CFrame.fromEulerAnglesXYZ(math.rad(math.clamp(X * DT, -0.15, 0.15)), math.rad(math.clamp(Y * DT, -0.5, 0.5)), Tilt)
		* CFrame.fromEulerAnglesXYZ(math.rad(VX), math.rad(VY), math.rad(VY * 10))
	
	Tilt = math.clamp(Lerp(Tilt, -Camera.CFrame:VectorToObjectSpace((RootPart and RootPart.Velocity or Vector3.new())
		/ math.max(WalkSpeed, 0.01)).X * 0.05, 0.1 * DT), -0.05, 0.05)
	
	if not TouchEnabled and DT < 2 then
		AngleX = Lerp(AngleX, math.clamp(UserInputService:GetMouseDelta().X / DT * 0.15, -2.5, 2.5), 0.25 * DT)
	end
	
	X = Lerp(X, math.sin(tick() * SX) / 5 * math.min(1, SY / 10), 0.25 * DT)
	
	if Velocity > 1 then
		Y = Lerp(Y, math.cos(tick() * 0.5 * math.floor(SX)) * (SX / 200), 0.25 * DT)
	else
		Y = Lerp(Y, 0, 0.05 * DT)
	end
	
	if Velocity > 12 then
		SX = 20
		SY = 18
	elseif Velocity > 0.1 then
		SX = 12
		SY = 14
	else
		SY = 0
	end
end

RunService.RenderStepped:Connect(OnRenderStepped)
1 Like