My visible gui isn't working at expected?

So I have this script that when a keybind is pressed, it should open/close a gui, and it works, but it is delayed with its closing, any help? here is the script

local Player = game.Players.LocalPlayer 
local Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(k) 
k = k:lower() 
if k == "r" then 
        script.Parent.MENU.Visible = true
else
	if script.Parent.MENU.Visible == true then
	 script.Parent.MENU.Visible = false
    end 
end
end)

try changing your code to this:

local Player = game.Players.LocalPlayer 
local Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(k) 
k = k:lower() 
if k == "r" then 
        script.Parent.MENU.Visible = true
elseif script.Parent.MENU.Visible == true then
	 script.Parent.MENU.Visible = false

end
end)

Its still delayed. Character30

Use UserInputService instead Mouse.KeyDown is Deprecated

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


UserInputService.InputBegan:Connect(function(Input, Process)
	
	if Input.KeyCode == Enum.KeyCode.R and not Process then
		if Menu.Visible == false then -- if it's not visible then make it visible
			Menu.Visible = true
		else -- if it's visible then make it not visible
			Menu.Visible = false
		end	
	end
	
end)
1 Like

Works perfectly, thank you. Character30

1 Like

A neat trick is setting the visibility like so:

Menu.Visible = not Menu.Visible

This way you don’t need an if statement, just one line of code.

1 Like