Open gui script with keyboard doesn't work

gui script doesn’t work.
I was making a script where if you press G on your keyboard, the Gui opens and closes.
This script is in StarterPlayerScripts.

local UIS = game:GetService("UserInputService")
local scrollingframe = game.StarterGui.ShopGui. Background
local ImageButton = game.StarterGui.ShopGui. ImageButton
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.G then
			ImageButton.Activated:Connect(function()
				scrollingframe.Visible = not scrollingframe.Visible
			end)
		end
	end
end)

screenshot:

so for some reason, it doesn’t work, and no errors come out in output.
Please help.

1 Like

game.StarterGui is where guis are cloned from. This isn’t what the player is seeing currently.

You would modify LocalPlayer.PlayerGui instead

if input.UserInputType == Enum.UserInputType.Keyboard then this check is redundant, it would have to be a keyboard for the keycode to be G

ImageButton.Activated:Connect(function()
move this outside of the inputbegan, otherwise it will connect an event every time (memory leak)

1 Like

Well first of all I don’t think you should have a capital letter in your variabke names, that’s really bad style.
And second of all you connected to the InputBegan event, which is good, but then you connected again to another event inside of it,
which is also good, but then you used a nested if statement that was redundant and that’s why it didn’t work. You neded to use an else if
statement.

1 Like

i took both your guys’ tips and this is what happened.

local UIS = game:GetService("UserInputService")
local scrollingframe = game.LocalPlayer.PlayerGui.ShopGui. Background
local ImageButton = game.LocalPlayer.PlayerGui. ImageButton
ImageButton.Activated:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Keyboard then
		elseif input.KeyCode == Enum.KeyCode.G then
			
		end
		scrollingframe.Visible = not scrollingframe.Visible
	end)
end)

output:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players") -- Get players

local player = Players.LocalPlayer -- Get local player
local playerGui = player:WaitForChild("PlayerGui") -- get gui

local shopGui = playerGui:WaitForChild("ShopGui")
local imageButton = shopGui:WaitForChild("ImageButton")
local scrollingFrame = shopGui:WaitForChild("Background")

-- make a function that changes the visibility
local function toggleVisibility()
	scrollingFrame.Visible = not scrollingFrame.Visible
end

-- when button is clicked call the function
imageButton.Activated:Connect(toggleVisibility)

UIS.InputBegan:Connect(function(input, processed) -- processed is whether the input has already been registered by a core script, like typing in a textbox
	-- when G is pressed call the function
	if not processed and input.KeyCode == Enum.KeyCode.G then
		toggleVisibility()
	end
end)

Edit: assuming the button is in the ShopGui, this should work

no errors, except it says:

Where is the image button located?

1 Like

PlayerGui.ShopGui.ImageButton, it’s there.

Screen Shot 2023-03-15 at 10.32.12 AM

I’ve edited the code to use that now

2 Likes

Didn’t know Rick Ashley was this big of a goat.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.