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

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

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?

3 comments

0

Please use https://developer.roblox.com/api-reference/class/UserInputService KeyDown should not be used in new code

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.

0

Thank you you two for the feedback, it as helped a lot.

1 answer

✓ Accepted answer
--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

1 comment

0

Thank you, it won't let me upvote, It says "You need at least 5 reputation before you can upvote"

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