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

How do i make a GUI appear/disappear when i press a key?

Asked by 7 years ago
game.Players.PlayerAdded:connect(function(mouse)
mouse.KeyDown:connect(function(key)
if key == "t"  then
game.StarterGui.ScreenGui.Shop.Visible = true
end
end)
end)

That code i thought should work, but it didn't. Does anyone know why?

3 answers

Log in to vote
0
Answered by
KordGamer 155
7 years ago
Edited 7 years ago

KeyDown is deprecated. Use UserInputService instead.

function keyPressed(input)
    if input.KeyCode == Enum.KeyCode.T then
        --Code
    end
end

game:GetService("UserInputService").InputBegan:connect(keyPressed)

Also, you're trying to enable/disable the StarterGui GUI, not the Player's GUI. This won't do anything. Try with the Player's GUI instead:

game.Players.PlayerAdded:connect(function(plr)
    plr.PlayerGui.ScreenGui.Shop.Visible = true
end)
0
I don't really get it.. Im really new to scripting, so could you describe a little bit more in detail? DannzXD 7 — 7y
Ad
Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
7 years ago

You should use UserInputService as KeyDown is deprecated. You also have to use the player's individual PlayerGui instead of StarterGui, as StarterGui is just used to replicate the guis to the players when they spawn.

game:GetService("UserInputService").InputBegan:Connect(function(key,bool)
   if bool == false then
      if key.KeyCode == Enum.KeyCode.T then
         game.Players.LocalPlayer.PlayerGui.ScreenGui.Shop.Visible = true
      end
   end
end)

This also needs to be in a LocalScript, probably inside StarterGui.

Log in to vote
0
Answered by
oSyM8V3N 429 Moderation Voter
7 years ago
Edited 7 years ago

Although the 2 answer's above me is correct, he also said Appear/Disappear, which you both only answered one part of the question. In order to make it so if you click it and it's visible, and want it to make it not visible, you need to add a variable and check if it is by doing this:

LOCAL SCRIPT

local UIS = game:GetService("UserInputService")
local active = false
local p = game.Players.LocalPlayer
local mouse = p:GetMouse()

UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
local KeyCode = Input.KeyCode
if not gameProcessedEvent then -- checks that the player isn't typing

if KeyCode == Enum.UserInputType.MouseButton1 and active == false then 
active = true
p.PlayerGui.ScreenGui.Shop.Visible = true
else
if KeyCode == Enum.UserInputType.MouseButton1 and active == true then
active = false
p.PlayerGui.ScreenGui.Shop.Visible = false
end
end
end
end
end)

If you have anymore questions, then please feel free to ask, and i didn't get a chance to test if it works, so let me know if it does

Btw this makes it so it can go visible, and not visible when you press the T key, depending on if active == false or true

Answer this question