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 5 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:

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

Also it's a local script in Replicated First

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

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 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.

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

UIS.InputBegan:Connect(function(key,gameProc)
   if not gameProc then
      if key.KeyCode == Enum.KeyCode.E then
         local gui = player.PlayerGui:FindFirstChild("ScreenGui")
         if gui then
            gui.Input.Visible = true
         end
      end
   end
end)

Accept and upvote if this helps!

Ad

Answer this question