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

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

Asked by 10 years ago

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

Log in to vote
0
Answered by 10 years ago

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)
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) ZincPlays 0 — 10y
0
All GUI objects have a .Visible property. Setting it to false will hide it and setting it to true will make it visible. Basswobble 165 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

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)

Log in to vote
0
Answered by
blocco 185
10 years ago

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)
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Answer this question