Turn the shop panel into a real menu with a toggle button, a close button, the Escape key, and a click-outside backdrop.
A shop that's glued to the screen is a billboard, not a menu. A menu appears when you ask it to and goes away when you're done — and it never eats clicks when it's not up. In the last lesson you built the panel. This lesson gives it the behaviors every interface needs: a button that opens it, a button that closes it, the Escape key, and a click-outside backdrop.
Every GUI element has a Visible . false makes it — and everything inside it — invisible, unclickable, and skipped when rendering. Crucially, it stays built: flip Visible back to true and the menu reappears exactly as it was, with all its labels and buttons intact.
shopPanel.Visible = false -- start the game with the menu closed
shopButton.Activated:Connect(function()
shopPanel.Visible = true -- open on demand
end)
You want to keep a menu around so it can be reopened many times. What is the right way to close it?
A toggle button reads the panel's current state and flips it. The same button opens the menu when it's closed and closes it when it's open — no bookkeeping, no separate handlers.
shopButton.Activated:Connect(function()
shopPanel.Visible = not shopPanel.Visible
end)
not flips a boolean: not true is false, not false is true. That one word turns "open" and "close" into a single line.
Make the shop button flip the menu's state, so the same button both opens and closes it.
local function toggleMenu()
shopPanel.Visible =
A menu also gets a close button — the familiar X in the corner. Its job is simple and one-way:
closeButton.Activated:Connect(function()
shopPanel.Visible = false
end)
Close is idempotent: pressing it when the menu is already closed changes nothing. The toggle is not — a stray click while the menu is in a weird state can re-open it. That's why menus use a dedicated close button: it can never disagree with reality.
Close buttons are 'idempotent'. What does that mean in practice?
Some live on the keyboard — pressing Escape should always be able to bail out of a menu. You've seen UserInputService; this is the shape of the handler:
local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.Escape and shopPanel.Visible then
shopPanel.Visible = false
end
end)
Two guards matter. gameProcessed says "the UI already swallowed this input" — like typing in a TextBox — so you skip it. And input.KeyCode == Enum.KeyCode.Escape checks which key, because InputBegan fires for every key on the keyboard.
The Escape handler should only close a menu that is currently .
The friendliest way to close a menu is to click anywhere outside it. That needs a backdrop: a full-screen, invisible button sitting behind the panel. Because GUI elements drawn later sit on top, create the backdrop first, then the panel — that puts the panel in front.
local backdrop = Instance.new("TextButton")
backdrop.Size = UDim2.new(1, 0, 1, 0) -- whole screen
backdrop.BackgroundTransparency = 1 -- invisible
backdrop.Text = "" -- no label
backdrop.Parent = screenGui -- created BEFORE the panel, so it sits behind
backdrop.Activated:Connect(function()
shopPanel.Visible = false
backdrop.Visible = false
end)
An invisible backdrop that stays Visible = true is a silent click-blocker: the menu looks closed, but the button underneath is eating every click on screen. This is one of the most common "why won't my game respond?" bugs in Roblox — and it's fixed by hiding the backdrop together with the panel.
The menu is closed. Which line must also run so the invisible backdrop doesn't block the game?
By now the menu has four ways to change state: the toggle, the X, Escape, and the backdrop. If each one sets Visible on its own, they can disagree — and the moment two flags get out of sync is the moment the bug appears.
Route every change through one that decides what "open" means:
local function setMenu(open)
shopPanel.Visible = open
backdrop.Visible = open
end
shopButton.Activated:Connect(function() setMenu(not shopPanel.Visible) end)
closeButton.Activated:Connect(function() setMenu(false) end)
backdrop.Activated:Connect(function() setMenu(false) end)
uis.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.Escape then setMenu(false) end
end)
Now there is exactly one place that knows how to open and close the whole menu — the panel and the backdrop can't drift apart, and Escape, the X, and the backdrop all call the same close.
Why is it better to route every open/close through one setMenu function?
Visible.Visible is a client GUI — menus are built and toggled in LocalScripts, on the 's own screen.gameProcessed guard. Skipping it means Escape also fires while the is typing in a TextBox, kicking them out of the menu mid-keystroke.Menus are pure client UI. Building or toggling them in a server either does nothing or errors — always build into PlayerGui from a LocalScript.
A menu is just a frame that knows how to show and hide:
Visible = false.not.setMenu(open) .Run these five habits and any panel — shop, settings, pause menu — behaves like a real interface instead of a billboard.