How do I make a first person game but when the player opens a menu the mouse moves freely?

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.

If you have any questions just ask!

1 Like

You might want to take a look at this article.

1 Like

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.

Not a single line of this code will work.

Sorry guy for not replying! I was afk for like 1 minute lol

No need to apologize for that!

Reading this now! Thanks! I need more characters to send this message!

Thank you so much! I didn’t realize that this article existed! Thanks you helped a ton!

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