Codectionary / Developer documentation / Luau

GUI buttons and user input

Player UI belongs on the client. Use GuiButton.Activated for a button that works across supported input methods, and avoid handling a keyboard shortcut when the interface has already consumed that input. A local UI action can request a server action, but cannot authorize it.

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