First person camera sensitivity increases every time I respawn?

So i’ve been making my custom first person camera and overall, It’s gone quite well. I’ve just encountered one issue and that is that every time I respawn, the sensitivity increases. This is my code:

local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local camera = game.Workspace.Camera
local mouse = player:GetMouse()
local userInputService = game:GetService("UserInputService")
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(0.3)

local mouseX
local mouseY

local xRotation = 0
local yRotation = 0

local dampenedMouseX = 0
local dampenedMouseY = 0

local Sensitivity = 20

local character

local canMoveCam = true

player.CharacterAppearanceLoaded:Connect(function(char)
	canMoveCam = true
	character = char
	character.Humanoid.AutoRotate = false
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	for i, v in pairs(char:GetChildren()) do
		if v:IsA("BasePart") and v.Name ~= "Head" then
			v.LocalTransparencyModifier = v.Transparency
			v:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
				v.LocalTransparencyModifier = v.Transparency
			end)
		end
	end
	
	for _, v in pairs(char:GetChildren()) do
		if v:IsA("Accessory") then
			v:WaitForChild("Handle").Transparency = 1
		end
	end
	
	character.Humanoid.Died:Connect(function()
		canMoveCam = false
	end)
	
	RunService.RenderStepped:Connect(function(deltaTime)
		local mousePos = userInputService:GetMouseDelta()
		
		mouseX = mousePos.X * deltaTime
		mouseY = mousePos.Y * deltaTime
		
		xRotation -= mouseY
		xRotation = math.clamp(xRotation, -4, 5)
		
		yRotation -= mouseX
		yRotation = math.clamp(yRotation, -3, 3)

		camera.CFrame = CFrame.new(character.Head.CFrame.Position)
		
		if canMoveCam then
			camera.CFrame = (camera.CFrame * ((character.HumanoidRootPart.CFrame.Rotation ) * CFrame.Angles(math.rad(xRotation * Sensitivity),0, 0)))
			character.HumanoidRootPart.CFrame = (character.HumanoidRootPart.CFrame * CFrame.Angles(0, -mouseX / 2, 0))
		else
			camera.CFrame = (camera.CFrame * ( character.Head.CFrame.Rotation * CFrame.Angles(0, math.rad(yRotation * Sensitivity), 0)))
		end
			 



	end)
end)

mouse.Button1Down:Connect(function()
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	userInputService.MouseIconEnabled = false
end)

mouse.Button2Down:Connect(function()
	userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	userInputService.MouseIconEnabled = false	
end)

Now I’ve tried only loading the camera once when the player joins but that doesn’t work (The behaviour just stops working as expected) but now i’m left clueless as to what is increasing the sensitivity every time I respawn. Here are some examples:

First time spawning:

10th time:

Although not perfect, I’ve moved my mouse about the same distance in both clips. I’ve looked over the code and haven’t been able to find anything. Can someone help me?

It’s probably because the script is running twice the first time you die.

Where are you putting the script. Put it in StarterPlayer.StarterPlayerScripts


Or that could be your issue aswell

Disconnect the renderstep connection after you die.

To do that turn it into a variable:

local loopConn = RunService.RenderStepped:Connect() -- etc

humanoid.Died:Connect(function()
loopConn:Disconnect()
end)

This will most likely fix it because every time your characterAppearance loads in its creating a new connection to renderStepped. It doesn’t automatically disconnect or stop it when you die, so you will have to do it manually.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.