How do you Open and Close another GUI Frame

Hello,

I am currently making a new Menu GUI and i am new to doing scripts, i seem to be in a issue where i cannot get a keycode to open another GUI and close the already opened one, I have looked through YT Videos and such but i cannot seem to find how to get keycodes to work besides of doing MouseButton1Click

this is the script i used on another forum: How to open and close a GUI

local userinputService = game:GetService("UserInputService")
local Frame = script.Parent.Parent.Parent.main_frame


userinputService.InputBegan:Connect(function(Key,Processed)
	if Processed then return end
	if Key.KeyCode == Enum.KeyCode.ButtonA then 
		Frame.Visible = not Frame.Visible
	end
end)
1 Like

try Visible = false
ex)

local userinputService = game:GetService("UserInputService")
local Frame = script.Parent.Parent.Parent.main_frame


userinputService.InputBegan:Connect(function(Key,Processed)
	if Processed then return end
	if Key.KeyCode == Enum.KeyCode.ButtonA then 
		Frame.Visible = false
	end
end)
2 Likes

i think the issue you’re having is that your doing Enum.Keycode.ButtonA which if i’m not mistaken is the button for controllers

unless you are on a controller, this won’t work. try doing Enum.KeyCode.A and see if that works!

2 Likes

well i must have mistaken but im trying to add controller controls for the menu

2 Likes

ah sorry, haven’t worked with controllers so i’m not entirely certain

2 Likes
  1. Input Handling: Use UserInputService to detect key presses.
  2. GUI Management: Ensure you correctly reference the GUIs you want to show and hide.
  3. KeyCodes: Use the correct KeyCode for the key you want to use.

Script

  1. Setup Your GUI:

    • Create two Frames: main_frame and second_frame.
    • Place them in StarterGui.
  2. LocalScript: Place a LocalScript in StarterGui.

here’s

local userinputService = game:GetService("UserInputService")
local frame1 = script.Parent:WaitForChild("main_frame")
local frame2 = script.Parent:WaitForChild("second_frame")

-- Set initial visibility
frame1.Visible = true
frame2.Visible = false

userinputService.InputBegan:Connect(function(input, processed)
    if processed then return end

    if input.KeyCode == Enum.KeyCode.X then  -- Change "X" to the desired key
        frame1.Visible = not frame1.Visible
        frame2.Visible = not frame2.Visible
    end
end)
2 Likes

It seems to be not working, I have made it coded to do exactly what its supposed to do when the keycode is pressed and Nothing seems to be doing anything, not even in the console that theres an error

1 Like

I assume you are trying to set the keycode to the A key on the keyboard. If it is then do Enum.KeyCode.A instead.

2 Likes

trust me its not im trying to add console inputs into my game for like xbox and playstation

2 Likes

My mistake. I saw that you are trying to set up a keybind for a controller. I think you can find the key code in this documentation.

1 Like

i am very aware how to use keycodes i just dont know how to script anything, im only good at doing modifications to the script itself

3 Likes

I think the issue you may be running into has to do with the if processed then return end because this is not a keyboard, I think you’re preventing the code from running at all because it thinks when you press A it’s a game event. (ButtonA is usually connected to Jump on consoles) So I think that’s the issue. If you really want A to be the button to open the menu then you need to remove if processed then return end, and it should work.

2 Likes

Let me know if this works! ^^^

(30 asydasd character asd asd asminimum text)