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
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)
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)
Input Handling: Use UserInputService to detect key presses.
GUI Management: Ensure you correctly reference the GUIs you want to show and hide.
KeyCodes: Use the correct KeyCode for the key you want to use.
Script
Setup Your GUI:
Create two Frames: main_frame and second_frame.
Place them in StarterGui.
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)
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
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.