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

How can i make a gui open on pressing a button E.G. "Q" ?

How can i make a gui open on pressing a button E.G. "Q" so that a gui to spawn items would open when the user presses a button on thier keyboard

4 answers

Use LocalPlayer:GetMouse() in a LocalScript to get the mouse object of the local player.

Then use LocalPlayer:GetMouse().KeyDown event to detect key presses like so:

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

mouse.KeyDown:connect(function(k)
	print(k.." has been pressed")
end)

2 comments

0

Just one other thing sorry to ask this but how would i use this in making an gui visible (this is the last part of my game and im not great on this part of it)

0

All GUI objects have a .Visible property. Setting it to false will hide it and setting it to true will make it visible.

gui is the name of the GUI you want to show or hide. if k == "q" checks if the user pressed Q or something else. gui.Visible = not gui.Visible is a neat shorthand for inverting (making a value what it isn't, e.g making false true and vice versa which is what we do here) a value.

local mouse = game.Players.LocalPlayer:GetMouse()
local gui = game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame -- replace Frame to the name of the frame your gui is in

mouse.KeyDown:connect(function(key)
    if key ==  "q" then
        gui.Visible = not gui.Visible
    end
end)

If for any reason you would want to do this without having a reference to the PlayerMouse (which I approve of and would do myself), then here is an example using UserInputService.

Note that using this method requires that you ignore any input that occurs while a TextBox object is being typed into. This means that you will have to use, besides the InputEnded event to determine when a user is finished pressing a key, both the TextboxFocused and TextboxFocusEnded events to determine when it is safe to accept the user's input.

Give it a shot, I implore you!

local UserInputService = Game:GetService("UserInputService");
local acceptUserInput = true;

UserInputService.TextboxFocused:connect(
	function()
		acceptUserInput = false;
	end
)
UserInputService.TextboxFocusReleased:connect(
	function()
		acceptUserInput = true;
	end
)
UserInputService.InputEnded:connect(function( inputObject, gameProcessedEvent )
	if (gameProcessedEvent) then
		return
	end
	if (acceptUserInput) and (inputObject.UserInputType == Enum.UserInputType.Keyboard and inputObject.KeyCode == Enum.KeyCode.Q) then
		-- open/close GUI
	end
end)

player = game.Players.LocalPlayer


player:GetMouse().KeyDown:connect(function(key) if string.lower(key) == "e" then -- Set keybind here if script.Parent.ItemHolder.Visible == false then script.Parent.ItemHolder.Visible = true script.Parent.Title.Visible = true script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 0

> if script.Parent:FindFirstChild("Notify") ~= nil then
if script.Parent.Notify.Visible == true then
	script.Parent.Notify.Visible = false
end
end

elseif script.Parent.ItemHolder.Visible == true then script.Parent.ItemHolder.Visible = false script.Parent.Title.Visible = false script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 14 end end end)

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