On-Key GUI Function not working

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!

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) 
1 Like

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

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

1 Like

I did and nothing seems to show up in the output

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

1 Like

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

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

1 Like

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

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

1 Like

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

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

1 Like

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

1 Like

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

1 Like

I do? Which should I get rid of

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.

1 Like
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?

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) 
1 Like

It worked, thank you a ton!

Huge thanks to @EmeraldSlash as well.

2 Likes

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

1 Like