Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a GUI invisible when you press a key?

Asked by 6 years ago

I am trying to make a command bar and I am trying to make it disappear when I press a certain key code I used:

1game:service'UserInputService'.InputBegan:connect(function(inputObject, gameProcessedEvent)
2    if (inputObject.KeyCode == Enum.KeyCode.E) then
3    game.StarterGui.ScreenGui.Input.Visible = true
4    print("Test")
5    end;
6    end);

Also it's a local script in Replicated First

0
Didnt you already post this? DinozCreates 1070 — 6y
0
Its been like 10 minutes, maybe try longer before asking for help. DinozCreates 1070 — 6y
0
The print function works and yes I did but I needed more help VoidKeyword 111 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The issue here is that you're changing the Gui object in StarterGui. Once the character loads, contents of StarterGui are cloned into the respective player's PlayerGui folder. Therefore, we need to access the Gui from there. Additionally, :connect() and :service() are deprecated. Use :Connect() and :GetService(), respectively.

01local UIS = game:GetService("UserInputService")
02local Players = game:GetService("Players")
03local player = Players.LocalPlayer
04 
05UIS.InputBegan:Connect(function(key,gameProc)
06   if not gameProc then
07      if key.KeyCode == Enum.KeyCode.E then
08         local gui = player.PlayerGui:FindFirstChild("ScreenGui")
09         if gui then
10            gui.Input.Visible = true
11         end
12      end
13   end
14end)

Accept and upvote if this helps!

Ad

Answer this question