Skip to content

Number Field

A number in a field, with a minus button before it and a plus button after it, for counts and amounts: dancers in a party, a track’s BPM, emotes to buy. Press the buttons, or hold them to keep going faster, or type a number. On keyboard and gamepad the field is one stop: left and right change the number, like a slider, and the confirm button starts typing.

Number Field exampleNumber Field example
Lucy.Stack.new({
    direction = "horizontal",
    gap = "Space4",
    align = "center",
    children = {
        Lucy.Text.new({ text = "Party size", variant = "large", autoWidth = true }),
        Lucy.NumberField.new({
            value = 4,
            min = 1,
            max = 16,
            width = UDim.new(0, 160),
            onValueCommit = function(dancers)
                app.toast(`Room for {dancers} dancers`)
            end,
        }),
    },
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
valuenumber0yesThe current number
minnumberno limityesThe lowest number
maxnumberno limityesThe highest number
stepnumber1yesHow much the buttons and arrows change it; typed numbers snap to it
placeholderstring""yesGrey text shown while the field is empty
disabledbooleanfalseyesGreys it out and ignores input
visiblebooleantrueyesShows or hides it
formatfunctionTurns the number into the shown text; whole numbers have no point by default
widthUDimfills the parentHow wide the whole field is, buttons included
onValueChangefunctionCalled with (value, handle) on every change, while a button is held too
onValueCommitfunctionCalled with (value, handle) once per press, held arrow or typed number that changed it

It also takes the props every component takes.

A typed number counts when the player presses Enter or leaves the field. It is snapped to the step and kept between min and max; anything that is not a number puts the old value back. Steps count from min when there is one, from zero when not. Values are cleaned up after each step, so 0.1 steps give 0.3, not 0.30000000000000004. A button turns grey at the end it moves towards.

The buttons draw the lucy:minus and lucy:plus icons. To use your own, see Icons Lucy draws.

Methods

NameWhat it does
field:setValue(value)Sets the number, snapped to the step and kept in range

Examples

Keep the Booth’s BPM in app state and save it once the player has picked:

Number Field exampleNumber Field example
local bpm = app.state:ref("booth.bpm")

Lucy.NumberField.new({
    value = bpm,
    min = 60,
    max = 180,
    onValueCommit = function(value)
        remotes.SaveSetting:FireServer("bpm", value)
    end,
    Parent = booth,
})

A track’s playback speed in quarter steps, shown with a suffix:

Number Field exampleNumber Field example
Lucy.NumberField.new({
    value = 1,
    min = 0.25,
    max = 4,
    step = 0.25,
    width = UDim.new(0, 200),
    format = function(value)
        return string.format("%.2fx", value)
    end,
    Parent = booth,
})

Preview

In Studio, from the command bar: Lucy.Preview.show("NumberField").