Script not registering 'G' on the keyboard

I’m trying to open a GUI when ‘G’ is pressed. Although it doesn’t register or even fire the function.

local service = game:GetService("UserInputService")

local actionT = service:IsKeyDown(Enum.KeyCode.G)

local gui = script.Parent

local function inputInbound(input, _gameProcessedEvent)
	if actionT then
		if gui.Enabled then
			gui.Enabled = false
			print("Key registered. Closing.")
		else
			gui.Enabled = true
			print("Key registered. Opening.")
		end
	end
end

service.InputBegan:Connect(inputInbound)

I’ve tried multiple solutions, including making a local function, putting the function body into the event function, none worked. It doesn’t even print the message. And yes, my G key works…

just do this

game:GetService("UserInputService").InputBegan:Connect(function(Key, Processed)
	if Key.KeyCode == Enum.KeyCode.G and not Processed then 
		script.Parent.Enabled = not script.Parent.Enabled
	end
end)
2 Likes

your actionT variable is never updated