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

How To Make A Gui Toggle Using A key?

I know how to make a Gui toggle from visible to not visible using one button, but I am unfamiliar with the onKeyDown function. Here is the script:

function onClick()
Gui = script.Parent.Parent.Parent.ChatGui.Frame.TextBox

if Gui.Visible == false then
Gui.Visible = true
else
Gui.Visible = false
end
end

script.Parent.MouseButton1Down:connect(onClick)

How do you turn this into an onKeyDown function?

3 answers

-- in a LocalScript, in StarterGui

local plr = game.Players.LocalPlayer;
mouse = plr:GetMouse ();

function onPress(key)
	local Gui = script.Parent.Parent.Parent.ChatGui.Frame.TextBox
	if  key:lower () == "m" then
		 Gui.Visible  = not Gui.Visible -- So much better, yes?
	end
end

mouse.KeyDown:connect(onPress)

1 comment

0

Thank you so much!

For this, you can use key:lower(), or key:byte(). It depends whether you want any key, or a letter, and whatnot.

Go here for more information on keys.

This has to be in a LocalScript, inside the StarterGui, or StarterPack, or any of their children for it to work!

local mouse = game.Players.LocalPlayer:GetMouse()
local gui = script.Parent.Parent -- direct the script to the gui, or frame here
local open = false

mouse.KeyDown:connect(function(key)
	if open == false then
		open = true
		if key:lower() == "k" then
			gui.Visible  = false
		end
	else
		open = false
		if key:lower() == "k" then
			gui.Visible = true
		end
	end
end)

Both these scripts above are more complex then it acc is its so much easier

local plr = game.Players.LocalPlayer:GetMouse() plr.KeyDown:Connect(function(key) if key=="-" then if script.Parent.FRAMENAME.Visible == true then script.Parent.FRAMENAME.Visible = false elseif script.Parent.FRAMENAME.Visible == false then script.Parent.FRAMENAME.Visible = true end end end)

it so much easier and what it does is whenever the key which in this case is "-" it makes the frame visibility off and when pressed again it goes back on it saves alot of lines of code hope it helps. :)

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