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

Gui (KeyDown)not working?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

In StarterGui I have a ScreenGui called "KeyDownGui". In "KeyDownGui" I have a frame called "Box" and a LocalScript. Inside the LocalScript:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
gui = script.Parent
box = gui.Box
Open = false


Mouse.KeyDown:connect(function(key)
 if (key == "q") then
  if(Open == false) then
   box.Visible = true
   Open = true
 elseif (Open == true) then
  box.Visible = false
  Open = false
  end
 end
end)

I want to be able to press "q" to open the frame(Box) in-game, but in-game the frame does not open up, nothing happens! When I am in Roblox Studio and I go to "Test" at the top and then "Play" it works in there...! If yall would help w/ getting it to work NOT IN ROBLOX STUDIO, that would be awesome! Thanks!

0
Is your hierarchy correct? alphawolvess 1784 — 9y

2 answers

Log in to vote
2
Answered by 9 years ago

First make sure it is a localscript and is a child of the Player object or Character (PlayerGui).

Your code can be shortened considerably:

Game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(Key))
    if Key == "q" then
        script.Parent.Box.Visible = not script.Parent.Box.Visible
    end
end)

Or the same using UserInputService

Game:GetService("UserInputService").InputBegan:connect(function(Input)
    if Input.KeyCode= "q" then
        script.Parent.Box.Visible = not script.Parent.Box.Visible
    end
end
0
Aw man, you beat me SurVur 86 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Make a localscript and put it in StarterGui. Then copy and paste this code in it.

thiskey = game.Players.LocalPlayer:GetMouse()
box = script.Parent.Box

thiskey.KeyDown:connect(function(k)
    local key = string.lower(k)
    if key == "q" then
        if box.Visible == false then
            box.Visible = true
        else
            box.Visible = false
        end
    end
end

Answer this question