Alright, so I am trying to make a GUI that opens/closes when when the user presses the 'E' key. It looks to me I did everything how the wiki showed. Although when I press 'E' nothing happens. Here is my code.
local UserInputService = game:GetService("UserInputService") function onInputBegan(input, gameProcessed) if input.UserInputType == Enum.UserInputType.Keyboard then local keyPressed = input.KeyCode if keyPressed == 101 then if script.Parent.Open == false then script.Parent.Visible = true else script.Parent.Visible = false end end end end UserInputService.InputBegan:connect(onInputBegan)
This should work:
local UserInputService = game:GetService("UserInputService") function onInputBegan(input, gameProcessed) if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.E then if script.Parent.Visible == false then script.Parent.Visible = true else script.Parent.Visible = false end end end end UserInputService.InputBegan:connect(onInputBegan)
I've changed it to use "KeyCode.E" I also changed this line:
if script.Parent.Visible == false then
(I don't see why using a value "Open" would be any better than checking for visibility, but you can change it back if you want!)
If it works please "Accept Answer", if not, be sure to leave a comment.
Although this question has already been answered, there are still a few improvements I could make.
The first is that you don't need any if statements to do with the gui - it is very simple.
frame.Visible = not frame.Visible -- 'not' reverses bool values
If you want to have an 'open' value, just use the frame's visibility, since it's really the same thing.
The second is that you should not define parameters if you won't use them (I'm looking at you, gameProcessed
).
The thing is - you should be using it in this situation. Without it, the frame will toggle when typing in the chat or another TextBox
.
uis.InputBegan:connect(function(key,gp) if gp == false and key.KeyCode == Enum.KeyCode.E then -- code end end)
The third is that checking the input type is obsolete - the KeyCode
will just be nil if it is not a keyboard input.
To sum it up;
local uis = game:GetService("UserInputService") local hotkey = Enum.KeyCode.E local frame = script.Parent uis.InputBegan:connect(function(key,gp) if gp == false and key.KeyCode == hotkey then frame.Visible = not frame.Visible end end)
Hope I helped!
~TDP