How do I make a first person game but when the player opens a menu the mouse moves freely?
For example when making a game you can go into starter player then go into camera mode and lock first person gives you first person but how do I make the mouse move freely when you open a gui or when they press alt they have a free moving mouse or something like that.
One way to achieve this is to use a combination of UserInputService and ContextActionService.
Here is a code example:
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouseLocked = true
local mouseIcon = -- replace with your own id
-- function to lock/unlock the mouse
local function toggleMouseLock()
mouseLocked = not mouseLocked
if mouseLocked then
-- lock the mouse
mouse.Icon = ""
mouse.Locked = true
camera.CameraType = Enum.CameraType.Custom
else
-- unlock the mouse
mouse.Icon = mouseIcon
mouse.Locked = false
camera.CameraType = Enum.CameraType.Scriptable
end
end
-- listen for the "ToggleMouseLock" action
local toggleAction = game:GetService("ContextActionService"):BindAction("ToggleMouseLock", toggleMouseLock, false, Enum.UserInputType.Keyboard, Enum.KeyCode.LeftAlt)
-- listen for mouse click events to unlock the mouse
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 and mouseLocked then
toggleMouseLock()
end
end)
-- listen for gui events to unlock the mouse
player.PlayerGui.ChildAdded:Connect(function(child)
if child:IsA("GuiObject") and mouseLocked then
toggleMouseLock()
end
end)
In this example, we define a function toggleMouseLock that toggles between locking and unlocking the mouse. When the mouse is locked, we hide the mouse cursor and set the camera type to Custom. When the mouse is unlocked, we show the mouse cursor and set the camera type to Scriptable.
We then bind the ToggleMouseLock action to the Left Alt key using ContextActionService. We also listen for mouse click events and gui events to unlock the mouse.
Note that you may need to adjust this code depending on the specifics of your game and gui.
You could create a TextButton, clear the text, set the BackgroundTransparency to 1 and set the Modal property to true. Just make that button visible whenever you need the mouse to move.