Problem with code

Hello, I created a code where you open a frame with the “Q” key on the keyboard, however when you enter the game the frame opens automatically, I wanted help to find where the error is.

Code:

local frame = script.Parent.Parent.Frame
local tecla = Enum.KeyCode.Q

local UIS = game:GetService(“UserInputService”)
local abrir = false

UIS.InputBegan:Connect(function(key)
if key.KeyCode == tecla then
if UIS:GetFocusedTextBox() == nil then
if abrir == false then
abrir = true
frame.Visible = abrir
elseif abrir == true then
abrir = false
frame.Visible = abrir
end
end
end
end)

Maybe the frame is visible because you set it up at the start.

Try adding

frame.Visible = false

after the connection function.

It’s likely that you have it set to be visible at the start cause you might’ve forgot to make it invisible when testing. Best thing to do if you still want to see it in studio and not ahve to have to the hassle of making it visible again afterwards would be to do what @Anurag_UdayS mentioned.

But just to add a bit of advice for your code, it be done simpler and can be made nicer

local frame = script.Parent.Parent.Frame
local tecla = Enum.KeyCode.Q

local UIS = game:GetService("UserInputService")
local abrir = false

frame.Visible = false

UIS.InputBegan:Connect(function(key, gpe)
	if gpe or UIS:GetFocusedTextBox() then
		return
	end
	
	if key.KeyCode == tecla then
		abrir = not abrir
		frame.Visible = abrir
	end
end)

Automatically makes it invisible when the script is ran, and then when you press a key, it checks if the game processed it, such as when you type in a chatbox for example or if a Textbox is focused (which I think gpe would process as well so I don’t think it’s needed)

Then you can simply invert the boolean in abrir via the not operation and then set the visible property to that

Thanks, I was stuck in this code for a while and as I am still studying programming I couldn’t find a way for the code to work properly.

1 Like