Turn of and on a GUI with E key

I’m trying to make an inventory GUI option.
When the E key is held for 3 seconds it makes the GUI visible to true
and when clicked again normally, it makes visible to false.

I don’t understand how to classify the e key clicked function.
can someone pls help?

1 Like

You can use UIS, just define your gui

local Gui = --your gui

UIS.InputBegan:Connect(function(input, Chat)
	if not Chat then
		if input.KeyCode == Enum.KeyCode.E then
			if Gui.Enabled == true then
				Gui.Enabled = false
			else
				Gui.Enabled = true
			end
		end
	end
end)
4 Likes
local UIS = game:GetService("UserInputService")
local GuiObj = GUI_HERE

UIS.InputBegan:Connect(function(Key)
    if Key.keyCode == Enum.KeyCode.E then
        GuiObj.Visible = not GuiObj.Visible
    end
end)
2 Likes

it doesn’t seem to work for me

Have you defined your gui correctly?

So you would need to define UIS then define your GUI (wherever it is).
Then I would use input.KeyCode == Enum.KeyCode.(Your key)
Then make the GUI enabled.
Else, you would make it disabled.

yea i have defined them correctly

Please show your output and your explorer :slight_smile:

You can just use UserInputService to do that.
This script may not work or work incorrectly.

local GUI = nil--replace nil with the path to your gui

local KeyHolding = false

function fakewait(num)
 if KeyHolding == true then
  wait(num)
 end
end

function Hold()
 coroutine.wrap(function()
  fakewait(3)
  if KeyHolding == true then
   GUI.Enabled = true
  else
   GUI.Enabled = false
  end
 end)()
end
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(io,gpe)
 if gpe then return end
  if io.KeyCode == Enum.Keycode.E then
   KeyHolding = true
   Hold()
  end
 end
end)
UIS.InputEnded:Connect(function()
 if io.KeyCode == Enum.KeyCode.E then
  KeyHolding = false
 end
end)

This kind of works, I think.

1 Like

it says its enabling but the gui does not pop up

What does your output say? Can you maybe send a screenshot?

Can I see where you define your gui @LR2IR try reading the reply’s already sent before making a new one

At the very top of the script, I said, to replace nil with the path to the gui.
Sorry if I misunderstood the question.
Have a nice day.

1 Like

i got this output?

sry for responding so late, my internet is bad

Oh, sorry. I forgot to mention the io in input ended.
Here:

UIS.InputEnded:Connect(function(io)
 if io.KeyCode == Enum.KeyCode.E then
  KeyHolding = false
 end
end)

oh, thank you.
I beginning to understand.
i started coding week ago, tryna study in my free time. ty

@LR2IR 's script isn’t toggle-able but you will have to hold down the key when you want to use the gui, unless I’m missing something.

Did Not define io. That’s the issue. Hope I could help! :slight_smile:

Thank you everyone for helping me in this code, have a nice day

This code makes a script run in a coroutine.
If key “e” is held, KeyHolding is set to true.
If not, it will be set to false.
It’s not in a loop.
After 3 seconds, it will check the value.
If it’s set to true, the gui will appear.
If not, it will not.
Have a nice day.