Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

1 comment

0

I don't really get it.. Im really new to scripting, so could you describe a little bit more in detail?

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.