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.
At a glance
- Purpose
- Typed scripting and Roblox development
- File extension
- .luau
- Runs in
- Luau host; Roblox engine examples require Roblox Studio
- Usually used with
- Roblox APIs and Studio
Specifications & further reading
Related Luau documentation
RemoteEvent requests and notifications
RemoteEvent sends a message without waiting for a return value. A client calls FireServer; Roblox supplies the sending Player to OnServerEvent. The server can target one client with FireClient or broadcast with FireAllClients. This example requests a fixed help message, not a reward.Tools and equipped behaviour
A Tool moves between a player’s Backpack and character when equipped. Activated is an input signal, not proof that a hit or purchase occurred. Separate the local presentation from any server action, and release temporary UI or connections when the tool is unequipped.TweenService and transitions
TweenService interpolates supported properties over time. Creating a tween does not start it; call Play. Completion callbacks can also occur after cancellation, so inspect the playback state before treating the animation as a successful finish.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.
RemoteEvent sends a message without waiting for a return value. A client calls FireServer; Roblox supplies the sending Player to OnServerEvent. The server can target one client with FireClient or broadcast with FireAllClients. This example requests a fixed help message, not a reward.Tools and equipped behaviour
A Tool moves between a player’s Backpack and character when equipped. Activated is an input signal, not proof that a hit or purchase occurred. Separate the local presentation from any server action, and release temporary UI or connections when the tool is unequipped.TweenService and transitions
TweenService interpolates supported properties over time. Creating a tween does not start it; call Play. Completion callbacks can also occur after cancellation, so inspect the playback state before treating the animation as a successful finish.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.