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

Can i close gui with keybind "T" ?

I need a script to open and close gui with keybind "T" pls

3 answers

local UrGui =    --Put in ur Gui

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
if key == "t" then
if UrGui.Enabled == true then
UrGui.Enabled= false
else
UrGui.Enabled= true
end
end
end)

1 comment

0

make sure it's local

The above answer works but Roblox is doing away with Keydown, Keyup events connected to the mouse object.

A better way to Future proof this is to use the UserInputService from within a Local script inside your Gui.

Example on how to use the UserInputService

local UIS = game:GetService("UserInputService")
local Gui = script.Parent

--// this event fires as soon as you use an input device like mouse,keyboard etc...
UIS.InputBegan:connect(function(input) 
	--//make sure we only listen to the keystrokes from a keyboard and ignore anythign else
	if(input.userInputType.Name == "Keyboard") then
		--// make the Key useable and easy to remember.
		local Key = input.KeyCode.Name
		--// Press T to disable Gui
		if(Key == "T") then
			Gui.Enabled = false
		end
		--// Press G to enable Gui
		if(Key == "G") then
			Gui.Enabled = true
		end
	end
end)
--[[
--// this is commented out as its not used but its here for reference. this fires as you stop using an input device
UIS.InputEnded:connect(function(input)
	--// listen to only the keyboad
	if(input.userInputType.Name == "Keyboard") then
		--// pressT for Blah!... woot!
		if(input.KeyCode.Name == "T") then
			print("Blah!")
		end
	end
end)]]

Hope this helps! :)

local UIS = game:GetService("UserInputService")
local gui = script.Parent

UIS.InputBegan:Connect(function(input)

if input.UserInputType == Enum.UserInputType.Keyboard then
  local keypressed = input.KeyCode

  if keypressed == "T" then
    gui.Enabled = true
    if keypressed == "F" then
      gui.Enabled = false
    end
  end
end
end)

Try that and see if it works.

1 comment

0

Make sure its a local script inside of your GUI.

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