Syntax
button.Activated:Connect(function()
panel.Visible = not panel.Visible
end)Examples
Build a small local toggle
LocalScript in StarterPlayerScripts. It creates its own ScreenGui and button in PlayerGui. ResetOnSpawn is disabled so this one-time setup is not removed on the next character spawn.
local Players = game:GetService("Players")
local gui = Instance.new("ScreenGui")
gui.Name = "LessonPanel"
gui.ResetOnSpawn = false
local button = Instance.new("TextButton")
button.Size = UDim2.fromOffset(220, 48)
button.Position = UDim2.fromOffset(20, 80)
button.Text = "Show lesson"
button.Parent = gui
gui.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
local open = false
button.Activated:Connect(function()
open = not open
button.Text = if open then "Lesson: tables" else "Show lesson"
end)Respect consumed keyboard input
Separate LocalScript in StarterPlayerScripts. H prints a help request only when no UI processed the input and no TextBox is focused. Keep the on-screen button available for touch users.
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, processed)
if processed or UserInputService:GetFocusedTextBox() then return end
if input.KeyCode == Enum.KeyCode.H then
print("Help shortcut requested")
end
end)Best practices
- Use readable labels and sufficient contrast; an icon alone may not explain the action.
- Test touch, keyboard and gamepad instead of assuming every player has a mouse.
- A disabled button is presentation, not authorization. Repeat eligibility checks on the server.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation