On-Key GUI Function not working


#1

Hello there,

I tried to make my GUI open on key instead of on click but for some reason it’s not working. Can anyone tell me what I did wrong?

Code
local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.H

UIS.InputBegan:connect(function(Key,GPE)
    if not GPE then
        if Key.KeyCode == Key then
    if script.Parent.Parent.Frame.Visible == false then 
        script.Parent.Text = "Close"
        script.Parent.Parent.Frame.Visible = true 
    else 
        script.Parent.Text = "Store" 
        script.Parent.Parent.Frame.Visible = false 
    end 
end 
end
end) 

All help is appreciated!


#2

You should capitalise connect like :Connect(), as the capitalised version is deprecated and may not work properly.

Also try using prints to see where your code gets to.

Finally I recommend fixing up your formatting so you can read it easily:

local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.H

UIS.InputBegan:connect(function(Key,GPE)
    if not GPE then
        if Key.KeyCode == Key then
            if script.Parent.Parent.Frame.Visible == false then 
                script.Parent.Text = "Close"
                script.Parent.Parent.Frame.Visible = true 
            else 
                script.Parent.Text = "Store" 
                script.Parent.Parent.Frame.Visible = false 
            end 
        end 
    end
end) 

#3

I tested this out and it didn’t seem to work unfortunately. Was there something else I have to do?


#4

You changed Connect and the formatting right? Are there any errors in the output?


#5

I did and nothing seems to show up in the output


#6

Have you made sure your script is actually running? Also, UserInputService can only run in LocalScripts.


#7

I think it’s running? And yes it is a LocalScript


#8

Don’t think it’s running, prove it’s running!


#9

I don’t see anything in the output and it doesn’t work. I don’t believe it’s running :C


#10

Have you made a print right at the start of the script? This is weird – maybe you accidentally disabled your script?


#11

I do have a print at the start and my GUI is enabled. Is there something that broke it?


#12

Is your GUI being put into the player? Can you see it? Try manually tracking it and the script’s location during the test.


#13

I am going to get off for the night but will try that first thing in the morning.


#14

Excuse me, but you have two Key variables, that’s most likely your problem.


#15

I do? Which should I get rid of


#16

You should just rename the Key in the parameter to something like Input, anything but Key, because it was referenced before, and when the input begins, it’ll think you’re referencing to the variable, not the parameter.


#17
local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.H

UIS.InputBegan:connect(function(Input,GPE)
    if not GPE then
        if Key.KeyCode == Input then
            if script.Parent.Parent.Frame.Visible == false then 
                script.Parent.Text = "Close"
                script.Parent.Parent.Frame.Visible = true 
            else 
                script.Parent.Text = "Store" 
                script.Parent.Parent.Frame.Visible = false 
            end 
        end 
    end
end) 


Like that?

#18

Like this:

local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.H

UIS.InputBegan:connect(function(Input,GPE)
    if not GPE then
        if Input.KeyCode == Key then
            if script.Parent.Parent.Frame.Visible == false then 
                script.Parent.Text = "Close"
                script.Parent.Parent.Frame.Visible = true 
            else 
                script.Parent.Text = "Store" 
                script.Parent.Parent.Frame.Visible = false 
            end 
        end 
    end
end) 

#19

It worked, thank you a ton!

Huge thanks to @EmeraldSlash as well.


#20

:man_facepalming: I looked over it too quickly haha sorry.