Toggle
On or off. The control you will use most, and the one with the fewest options.
The smallest toggle#
main:Toggle({
Text = "God mode",
Callback = function(on)
godmode = on
end,
})on is true or false. That is the entire control.
My Script
v1.0
Enabled
FireOnStart runs the callback once at build time.
Building it up#
Start it on:
main:Toggle({
Text = "God mode",
Default = true,
Callback = function(on) godmode = on end,
})Explain what it does:
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:
main:Toggle({
Text = "God mode",
Save = "godmode",
Callback = function(on) godmode = on end,
})Every option#
| Option | Type | Default | What it does |
|---|---|---|---|
Textreq | string | — | The label. |
Callback | function(boolean) | — | Called with the new state whenever it changes. |
Default | boolean | false | The starting state. |
Description | string | — | A quieter second line. |
Icon | string | — | An icon shown before the label. |
FireOnStart | boolean | false | Also fire the callback once at creation, with the starting value. |
Save | string | — | Persist this toggle under the given key. |
Methods#
| Method | Returns | What it does |
|---|---|---|
Get() | boolean | The current state |
Set(value) | — | Flips the switch and fires Callback |
local godmode = main:Toggle({ Text = "God mode" })
godmode:Get() --> false
godmode:Set(true) -- animates the switch and fires the callbackFireOnStart, 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.
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:
-- 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.