My game is in first person, and I’m trying to make the game unlock the camera lock in first person mode by making it a keybind. So when you press F, it lets you move your mouse freely, and when you press F again, it’s back to being locked to first person.
This is a localscript inside StarterPlayerScripts
Nothing happens in-game, no console errors.
I’ve looked through a few devforum posts for the option of unlocking your mouse, the only thing I was making your mouse unlock by making a GUI and putting in the “Modal” option to true. I’m trying to enable this option by pressing the letter “f”
local txtButton = game.StarterGui.CameraUnlock.Button
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
if key == "f" then
txtButton.Modal = true
if key == "f" then
txtButton.Modal = false
end
end
end)
That should work I believe but what you are making doesn’t make sense you are trying to get? Why not use UserInputService its so much better and KeyDown is deprecated.
Looks like the Modal property will always be set back to false since you are checking if the key is “F” within two conditionals in the same event listener.
What you could do is simply negate the value by doing the following:
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
if key == "f" then
txtButton.Modal = not txtButton.Modal
end
end)
Additionally, you should be accessing UI for the player through the Player’s PlayerGui
local PlayerGui = game.Players.LocalPlayer.PlayerGui
I also do highly recommend you use UserInputService to listen for key presses and other things, more efficient than using the mouse, also a lot more versatility is there.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
-- do whatever
end
end)
local UserInputService = game:GetService("UserInputService")
local PlayerGui = game.Players.LocalPlayer.PlayerGui
local txtButton = game.StarterGui.CameraUnlock.Button
local function onInputBegan(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.F then
txtButton.Modal = not txtButton.Modal
end
end
end
Looks like the issue is you aren’t correctly referring to where the button is, it should look like this:
local UserInputService = game:GetService("UserInputService")
local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui") -- where the player's actual gui is
local txtButton = PlayerGui:WaitForChild("CameraUnlock"):WaitForChild("Button") -- waitforchild because items inside of startergui are replicated to playergui
local function onInputBegan(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.F then
txtButton.Modal = not txtButton.Modal
end
end
end
Like I mentioned above, the GUIs that the user actually see are located inside their PlayerGui object, therefore you will need to edit UIs from there. This is because the contents inside of StarterGui are replicated to PlayerGui, meaning that the changes you make inside of StarterGui won’t actually replicate to the player. When doing it this way, WaitForChild() is used because there is a chance the script tries to access the Instances before they have loaded. Hope this helps!
Try using contextactionService instead and a value that changes each time you press F
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local txtButton = PlayerGui:WaitForChild("CameraUnlock"):WaitForChild("Button")
local unlocked = false
local function setMouseLockState(actionName, inputstate, object)
if actionName == "setMouseState" then
if inputstate == Enum.UserInputState.Begin then
if unlocked then
unlocked = false
else
unlocked = true
end
txtButton.Modal = unlocked -- if unlocked is set to true then modal is turned on, otherwise it will turn off modal
end
end
end
ContextActionService:BindAction("setMouseState", setMouseLockState, true, Enum.KeyCode.F)