Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Mouse lock for inventory system?

Asked by 3 years ago

Im attempting to make a inventory system, when you click TAB the gui will be brought up and then i want your mouse to be able to move freely so you can mess with the inventory and things, and when you click TAB again the gui will be no longer visible then the mouse lock will be back.

Heres the script i have so far i just need the mouse part figured out.

local hotkey = Enum.KeyCode.Tab

local UIS = game:GetService("UserInputService") local open = false

UIS.InputBegan:Connect(function(key, gp) if key.KeyCode == hotkey then if UIS:GetFocusedTextBox() == nil then if script.Parent.Visible == true then script.Parent.Visible = false

            --Mouse unlock

        else
            script.Parent.Visible = true

            --Mouse lock

        end
    end
    end

end)

0
Please reformat your code. radiant_Light203 1166 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

You can lock the mouse using UserInputService's MouseBehavior property.

It goes like so:

local UserInputService = game:GetService("UserInputService")

UserInputService.MouseBehavior = Enum.MouseBehaviour.LockCenter -- mouse locked to middle of screen
Ad

Answer this question