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

Why won't the GUI open when I press G?

Asked by 5 years ago

The code is in a LocalScript

Player - game.Players.LocalPlayer
mouse - player:GetMouse()
gui - script.Parent
frame - gui.frame
open - false

function.PressG(key)
    if (key == "g") then
        if (Open == false) then
            frame.Visible = true
            open = true
        elseif (open == true) then
            frame.Visible = false
            open = false
        end
    end
end

Mouse.KeyDown:Connect(PressG)

Have I done something wrong?

0
Please use https://developer.roblox.com/api-reference/class/UserInputService KeyDown should not be used in new code User#5423 17 — 5y
0
You're using two different open functions. Lua relies on capitalization so change line 8 to a lowercase "O". Also you must add the key parameter to when you call the function as well else it will be marked as nil. Dashes are not a viable way to set a variable, you must use an equal sign. jmbooth10 20 — 5y
0
Thank you you two for the feedback, it as helped a lot. Luke2323244 17 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
--use equal marks not dashes
local UIS = game:GetService("UserInputService") --Mouse.KeyDown deprecated
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gui = script.Parent
local frame = gui.Frame


--gameProcessed event determines whether a core gui, like the chat, is active at the time the key is pressed. therefore, this event will not fire if you are typing in the roblox chat

UIS.InputBegan:Connect(function(key,gameProc)
   if not gameProc and key.KeyCode == Enum.KeyCode.G then
      frame.Visible = not frame.Visible --sets to opposite value
   end
end)

Accept and upvote if this helped

0
Thank you, it won't let me upvote, It says "You need at least 5 reputation before you can upvote" Luke2323244 17 — 5y
Ad

Answer this question