What do you want to achieve? The mouse to be unlocked and visible when key pressed
*What is the issue? mouse behavior won’t change
What solutions have you tried so far? disabling and re-enabling the mouse using the script by using game:GetService("UserInputService").MouseIconEnabled
showing the ui and hiding it works perfectly fine.
local plr = game.Players.LocalPlayer
local ui = script.Parent
plr:GetMouse() .KeyDown:Connect(function(Keycode)
if Keycode == "u" then
ui.Visible = true
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
game:GetService("UserInputService").MouseIconEnabled = true
else
ui.Visible = false
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
game:GetService("UserInputService").MouseIconEnabled = false
end
end)
You are using the mouse to check for keyboard input.
Try this code:
local plr = game.Players.LocalPlayer
local ui = script.Parent
local uis = game:GetService("UserInputService")
uis.InputEnded:Connect(function(inputObject,gameProcessedEvent)
if gameProcessedEvent then
--This means if the player is chatting or something like that
return
end
if inputObject.KeyCode == Enum.Keycode.U then
ui.Visible = true
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
game:GetService("UserInputService").MouseIconEnabled = true
else
ui.Visible = false
game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
game:GetService("UserInputService").MouseIconEnabled = false
end
local plr = game:GetService("Players")
local ui = script.Parent
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, gpc)
if gpc then return end
if input.KeyCode == Enum.KeyCode.U then
ui.Visible = true
uis.MouseBehavior = Enum.MouseBehavior.Default
uis.MouseIconEnabled = true
else
ui.Visible = false
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
uis.MouseIconEnabled = false
end
end)