How to move mouse freely in first person locked whenever a specific keyboard button is pressed

recently i’ve been workin on a fps project, and i want it so when player presses a specific button ok their keyboard, the mouse moves freely, so here’s a local script that i’ve reached using UserInputService / MouseBehavior sat to defualt.

--<< Local script that im using >>--
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(Input,IsTyping)
    if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode.V then
        print("Test")
        uis.MouseBehavior = Enum.MouseBehavior.Default
    end
end)

that script doesnt work at all, may someone help me with how to Move mouse freely in first person locked whenever a specific keyboard button is pressed?..

8 Likes

Hey! I was having the same problem.

I know this was a while ago, don’t know if you still need it and I’m not 100% sure on my solution but, I think you need to set the CameraType to scriptable.

Link to documentation:
https://developer.roblox.com/en-us/api-reference/property/UserInputService/MouseBehavior

3 Likes

I was also having this issue, but I managed to make it work. It may not be the greatest solution, but it works. (Create a LocalScript in StarterPlayerScripts, and paste this into the LocalScript. I know this is pretty late but thought I would still post this here just in case.)

local plr = game.Players.LocalPlayer
local plrGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local ScreenGui = Instance.new("ScreenGui", plrGui)
local TextButton = Instance.new("TextButton")
local UIS = game:GetService("UserInputService")

TextButton.BackgroundTransparency = 1
TextButton.Size = UDim2.new(0, 0, 0, 0)
TextButton.Text = " "
TextButton.Parent = ScreenGui

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then 
		return end
	if Input.KeyCode == Enum.KeyCode.V then
		TextButton.Modal = true
		task.wait()
	else
		TextButton.Modal = false
		task.wait()
	end
end)

(Sorry if this isn’t the most optimized, not that good at scripting, but it works. You would press, “V” to unlock the Cursor. When you click back on the game, where there is no GUI, it will re-lock the Cursor.)

2 Likes

couldn’t you just make a modal text button and change the visible properly whenever you press a button?

Maybe wrap your current script in a InputChanged event?