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)
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.
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.