Mouse Lock Script Problem

I’m using a recycled mouse lock script that’s similar to shift locking on roblox. It works perfectly for the most part, the only issue is that the sensitivity gradually increases whenever the player dies. The first video is the players sensitivity when they first join. The second video is the players sensitivity after they’ve died 3 times, and continues to increase the more you die.

Default Sensitivity:

Sensitivity After Dying A Few Times:

Code Snippet:

local function FocusToggle()
	wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = xAngle-input.Delta.x*0.4
		--Clamp the vertical axis so it doesn't go upside down or glitch.
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
	end
end)

runService.Heartbeat:Connect(function()
	local Character = localPlayer.Character
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart then
		--Set rotated start cframe inside head
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
		
		--Set camera focus and cframe
		local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
		
		Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)

		
	end
	end)
	end
	
localPlayer.CharacterAdded:Connect(function(char)
	RunServ.Stepped:wait()
	FocusToggle()
		
	end)

You are calling your function FocusToggle() every time the character respawns, and you are not disconnecting the last function when the player dies/character disappears.

Wrap your FocusToggle() in a variable and then disconnect it when the character model is deleted.

2 Likes

Wrapping the function in a variable worked perfectly thanks so much :smiley: