Make mouse be able to move when gui opened

hello! I tried making a admin panel but my game is in first person so i wanted to make the mouse be able to move when opened. though am getting the error Modal is not a valid member of ScreenGui “Players.Username.PlayerGui.AdminPanel”

local AdminFrame = script.Parent.AdminFrame
local UIS = game:GetService("UserInputService")
local GuiObject = script.Parent
local GuiOpen = false

UIS.InputBegan:Connect(function(input, gameProcessedEvent)

	if input.UserInputType == Enum.UserInputType.Keyboard then

		if input.KeyCode == Enum.KeyCode.F then
			if not GuiOpen then
				GuiOpen = true
				if AdminFrame.Visible == true then
					GuiObject.Modal = true
					AdminFrame.Visible = false
				end
			else
				GuiOpen = false
				if AdminFrame.Visible == false then 
					AdminFrame.Visible = true
					GuiOpen.Modal = false
				end
			end
		end
	end
end)
1 Like

I think Modal is a property of TextButtons and ImageButtons, not ScreenGui; Try setting the property on one of those.

A cool thing about the Modal property’s mouse unlocking is that it only happens if the button is visible. This means you can set the property in Studio (instead of scripting it), then Roblox will only let you move the cursor freely when the dialog is open, returning to first-person camera movement the moment it’s hidden.

am still learning about gui scripting and stuff. could you maybe explain more easier if its fine to ask?

I’ll try, but I actually just noticed what the problem is.

In your code, you create a boolean (true or false) variable named GuiOpen, which you use to check if any dialogs are on-screen. Later in the script, you try setting its “Modal” property. Modal doesn’t exist on variables, though.

So, basically, what I was saying was that “Modal” is a property that only can be used with button Gui objects like TextButtons.
image
If you check the checkbox shown here, “Modal”, your game will do what you’d like it to do without having to use that script to set the Modal property.

In this video, I show that the mouse cursor is only able to be freely moved around when the dialog appears. When it closes, the mouse controls looking around again.


This was done by just checking the “Modal” property’s checkbox.

Alternative method that I wouldn't recommend

If you’d like to enable Modal with conditions like in your script, you’ll have to reference a button instance. For example, if you have a TextButton placed within Frame within a ScreenGui like this:
image
…then you would reference it with

game:GetService("Players")[...].ScreenGui.Frame.TextButton.Modal = true

I’d recommend checking the “Modal” property on any buttons in the admin panel then removing the “GuiOpen.Modal” lines from your script; This should give you the results you’re after.

local AdminFrame = script.Parent.AdminFrame
local UIS = game:GetService("UserInputService")
local GuiObject = script.Parent
local GuiOpen = false

UIS.InputBegan:Connect(function(input, gameProcessedEvent)

	if input.UserInputType == Enum.UserInputType.Keyboard then

		if input.KeyCode == Enum.KeyCode.F then
			if not GuiOpen then
				GuiOpen = true
				if AdminFrame.Visible == true then
					AdminFrame.Visible = false
				end
			else
				GuiOpen = false
				if AdminFrame.Visible == false then 
					AdminFrame.Visible = true
				end
			end
		end
	end
end)

I hope I didn’t make this second post too complicated. If I did, I hope someone else replies to help out.

2 Likes