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

Can i close gui with keybind "T" ?

Asked by 3 years ago

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

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
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)
0
make sure it's local A_Mp5 222 — 3y
Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

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! :)

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

0
Make sure its a local script inside of your GUI. wizz_zz 3 — 3y

Answer this question