Cant find the problem in my script[Solved]

So if tryied to make my own script to unlock the mouse in first peson it should work fine but it dosent. I tryied to find the source of the problem but it seems like that the script is just fine. I hope some one can help me finding the problem why it isnt working

*information about the script:
-Location:StarterPlayerScripts
-its a Local Script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local input = game:GetService("UserInputService")
local MouseIsLocked = true --a boolen to check if its locked or not

local function MouseUnlock() --funtion that should unlock the cursor
	input.MouseBehavior = Enum.MouseBehavior.Default
end

local function MouseLock()--funtion that should lock the cursor
	input.MouseBehavior = Enum.MouseBehavior.LockCenter
end
	

input.InputBegan:Connect(function(inputobject,gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if inputobject.KeyCode == Enum.KeyCode.C and MouseIsLocked == true then-- if its locked and you press C it should unlock
		MouseUnlock()
		MouseIsLocked = false
	elseif inputobject.KeyCode == Enum.KeyCode.C and MouseIsLocked == false then-- if its Unlocked and you press C it should lock
		MouseLock()
		MouseIsLocked = true
	end
end)

I hope some one can help me
Thank You :slightly_smiling_face:

Nothing is wrong with your code, the problem is just the fact that Roblox always forces the cursor to be locked to the center in first person. There is a method however where if you set a TextButton Modal property to true, it’ll allow you to unlock your mouse freely in first person.
Mouse Lock Example.rbxm (4.8 KB)

Hi @Jumidummi1234

I rewrote your script that locks player’s camera in center and first person

local Players = game:GetService("Players")
local input = game:GetService("UserInputService")

local player = Players.LocalPlayer
local mouse = player:GetMouse()

local MouseIsLocked = true --a boolen to check if its locked or not

local DefaultMaxZoomDistance = player.CameraMaxZoomDistance
local DefaultMinZoomDistance = player.CameraMinZoomDistance

local function MouseUnlock() --funtion that should unlock the cursor
	input.MouseBehavior = Enum.MouseBehavior.Default
	player.CameraMaxZoomDistance = DefaultMaxZoomDistance
	player.CameraMinZoomDistance = DefaultMinZoomDistance
	player.CameraMode = Enum.CameraMode.Classic
end

local function MouseLock()--funtion that should lock the cursor
	input.MouseBehavior = Enum.MouseBehavior.LockCenter
	player.CameraMode = Enum.CameraMode.LockFirstPerson
	player.CameraMaxZoomDistance = 0.5
	player.CameraMinZoomDistance = 0.5
end

input.InputBegan:Connect(function(inputobject,gameProcessedEvent)
	if gameProcessedEvent then return end

	if inputobject.KeyCode == Enum.KeyCode.C and MouseIsLocked then-- if its locked and you press C it should unlock
		MouseUnlock()
		MouseIsLocked = false
	elseif inputobject.KeyCode == Enum.KeyCode.C and not MouseIsLocked then-- if its Unlocked and you press C it should lock
		MouseLock()
		MouseIsLocked = true
	end
end)

i found this i modified my script a littel and now it works.

The reason i dont use TextButton is that my cutom Cursor doset work any more.

But still thanks for trying to help me :saluting_face: