Hiding or showing a UI on button press?

Hi. I’m trying to make it so a UI appears when a key is pressed. I tried using the UserInputService but it didn’t detect the key input. How would I go about doing this?

local UserInputService = game:GetService("UserInputService") -- We get UserInputService

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then -- Enum.KeyCode.E Replace E with the key you want
			print("You've pressed E") -- Your code here if the player does press E
		end
	end
end)

And UserInputService link here! Hope this helps!

2 Likes

Suppose you have a local script parented to a “ScreenGui” instance inside the “StarterGui” directory, here’s a script which would toggle the “Enabled” property of that “ScreenGui” instance each time the keyboard’s “F” key is pressed.

Here’s the model file for testing purposes.
repro.rbxm (2.6 KB)

1 Like