Toggle

On or off. The control you will use most, and the one with the fewest options.

The smallest toggle#

Luau
main:Toggle({
    Text = "God mode",
    Callback = function(on)
        godmode = on
    end,
})

on is true or false. That is the entire control.

Live preview

My Script

v1.0

Enabled

FireOnStart runs the callback once at build time.

Building it up#

Start it on:

Luau
main:Toggle({
    Text = "God mode",
    Default = true,
    Callback = function(on) godmode = on end,
})

Explain what it does:

Luau
main:Toggle({
    Text = "God mode",
    Description = "Blocks all incoming damage.",
    Icon = "shield",
    Default = true,
    Callback = function(on) godmode = on end,
})

Remember it between sessions — add a Save key and Ember writes it to disk:

Luau
main:Toggle({
    Text = "God mode",
    Save = "godmode",
    Callback = function(on) godmode = on end,
})

Every option#

OptionTypeDefaultWhat it does
TextreqstringThe label.
Callbackfunction(boolean)Called with the new state whenever it changes.
DefaultbooleanfalseThe starting state.
DescriptionstringA quieter second line.
IconstringAn icon shown before the label.
FireOnStartbooleanfalseAlso fire the callback once at creation, with the starting value.
SavestringPersist this toggle under the given key.

Methods#

MethodReturnsWhat it does
Get()booleanThe current state
Set(value)Flips the switch and fires Callback
Luau
local godmode = main:Toggle({ Text = "God mode" })
 
godmode:Get()        --> false
godmode:Set(true)    -- animates the switch and fires the callback

FireOnStart, and why saved toggles need it#

Creating a toggle draws it, but does not run your callback. That matters once you add Save: a restored value makes the switch look on while your script still thinks it is off.

Luau
main:Toggle({
    Text = "Fullbright",
    Save = "fullbright",
    FireOnStart = true,   -- applies the restored value on load
    Callback = function(on) setFullbright(on) end,
})

Keep the startup callback cheap

FireOnStart runs while the window is being built, so a slow callback delays the window appearing. If yours walks the whole workspace, wrap the body in task.spawn and let the window draw first.

A mistake worth avoiding#

Set fires the callback, so calling it from inside that same toggle's callback loops forever:

Luau
-- Wrong: this recurses
local t
t = main:Toggle({
    Text = "Loop",
    Callback = function(on)
        t:Set(not on)   -- fires Callback again, which calls Set again…
    end,
})

If you need to refuse a change, track the value yourself and only call Set when it actually differs.