Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to allow mouse to move in a GUI while in first person? [UNSOLVED]

For a place i am making, which is in first person, i need to allow my mouse to move while the GUI is open

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

 local open = false

mouse.KeyDown:connect(function(key)
	if key == "q" then --replace this with the key you want
		if open == false then
			open = true
				script.Parent.ShopFrame.Visible = true
		elseif open == true then
			open = false
				script.Parent.ShopFrame.Visible = false
		end
	end
end)

what would I have to add or change to allow this?

1 comment

0

forgot to say, the mouse needs to be able to move when the shop is open, but lock in the middle again when it closes

4 answers

GUI buttons have a field known as "Modal" that, when true, will unlock the mouse in first person as long as that button has its "Visible" field set to true. I would set it manually in Studio, then you will not have any problems. Hope I helped!

The mouse position can be locked to the center of the screen using MouseBehavior. Setting this property to Enum.MouseBehavior.LockCenter will lock the mouse to the center of the screen, Enum.MouseBehavior.Default will unlock the mouse.

If the mouse is locked, InputChanged will still fire when the player moves the mouse and will pass in the distance the mouse has moved.

local UserInputService = game:GetService("UserInputService")
 
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		print("delta is (" .. tostring(inputObject.Delta.x) .. ", " ..  tostring(inputObject.Delta.y) .. ")")
	end
end)
--All of this will lock the mouse in the center and will tell you the delta

If you want the mouse to be unlocked then use :

local UserInputService = game:GetService("UserInputService")
 UserInputService.MouseBehavior = Enum.MouseBehavior.Default

Unfortunately, as I know, ROBLOX has a mouse-lock-in feature when you go into first person, therefor you are not able to select any screen GUIs. You must be in third person to select and move screen GUIs.

1 comment

0

is there any way i could switch it to third person in the script?

This discussion is preserved as it was. New answers, comments, and votes are closed.