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)
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.
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)
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