Button Modal Trick Not Working For Unlocking Cursor

I’m Having An Issue With My Cursor Unlocking System On My Game.
I did that trick where you have a button and you use the modal system so you can unlock and lock the cursor on the press of a button. Although i just did this in my game and it doesn’t seem to be working, the modal value is changing from true to false but nothing is happening.

Here is a video of me pressing Z to unlock the cursor and the print statement showing the value changing (ignore all the errors, they are irrelevant. also the modal is starting on true)

This Is My Code:

local UserInputService = game:GetService("UserInputService")
local Modal = script.Parent.Modal

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Z then
		if Modal == false then
			Modal = true
		else
			Modal = false
		end
		print("Modal:",Modal)
	end
end)

I don’t think you should be defining properties in variables, since setting a variable to something else would overwrite the variable entirely. An example would be:

local x = 0
x = 5 -- Variable x is now 5, the original value 0 is now garbage collected.

Have you tried removing .Modal from your variable and instead check the property itself in your condition, then set it from there? Something like:

local UserInputService = game:GetService("UserInputService")
local Text_Button = script.Parent

UserInputService.InputBegan:Connect(function(input: KeyCode, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Z then
		if Text_Button.Modal == false then -- Referencing .Modal outside the variable might be more precise.
			Text_Button.Modal = true
		else
			Text_Button.Modal = false
		end

		print("Modal:", Text_Button.Modal)
	end
end)

Hope this helps.

It Didn’t Seem To Change Anything, although i didn’t think this would change anything anyway since modal starts true anyway and if it didn’t unlock the cursor the second i spawned in clearly something else is going on.

You’re setting the Modal variable to true, not the property. Use script.Parent.Modal = true/false instead.

You can also add local ModalButton = script.Parent and ModalButton.Modal = true/false

Edit: To avoid the if statements use ModalButton.Modal = not ModalButton.Modal

From what I remember when I set something similar up, it needs to have Visible= true as well (even if it is fully transparent)

1 Like

As I Said Before, this didn’t change anything. refer to this reply:Button Modal Trick Not Working For Unlocking Cursor - #3 by Khookins

the button is fully visible as shown in the video

If something else is going on, then your code isn’t the problem!

In your video, I noticed you have a LocalScript that handles first person camera movement. Does that script contain any sort of code that edits anything about the mouse at all?

UserInputService has a property called MouseBehaviour, which can influence how the Mouse behaves. You can lock the mouse via the MouseBehavior.LockCenter enumeration, but I’m unsure if your first person camera script uses it at all.

local UserInputService = game:GetService("UserInputService")

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter

You are setting the variable Modal (which used to reference the button itself) to a boolean.
You set the variable Modal to true, because it just read the variable of the button and set itself to true, it didn’t actually affect the button

I read the code wrong the first time xd

So really what you should be doing is this:

local UserInputService = game:GetService("UserInputService")
local ModalButton = script.Parent :: TextButton

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Z then
		ModalButton.Modal = not ModalButton.Modal
	end
end)

Also, try naming things differently next time to avoid confusion!

this was actually the first thing i tried! i tried to basically the same script as i have now but instead switching the UserInputService.MouseBehavior from Enum.MouseBehaviour.LockCenter to Enum.MouseBehaviour.Default although this didn’t work whatsoever. I checked what was happening by printed the UserInputService.MouseBehaviour but it always stayed Enum.MouseBehaviour.LockedCenter. Also i did check all the other scripts involving firstPerson and ui but nothing stood out.

A visible Modal button should override mouse behaviour regardless whether it was set via user input service or LockFirstPerson.

So is there any other script which sets the mouse cursor position to be centred and moves the camera? As this would need to be disabled manually.

It only unlocks the mouse if the mouse is actually over it. Just make it cover the entire screen

i attempted this and it didn’t work still.


old code from a camera lock function I wrote a little while ago, hopefully it’s helpful

how is this locking the camera?

UserInputService.MouseBehavior = self.LIGHTSABER_ACTIVATED and Enum.MouseBehavior.LockCenter or Enum.MouseBehavior.Default