Skip to content

useCount

Lucy.useCount animates a number smoothly towards value whenever value changes. It starts from from (0 by default) and eases out towards the new target.

Usage

local counter = Lucy.useCount(value, options?)
ArgumentTypeDefaultWhat it is
valueMaybeRef<number>requiredTarget number to count to
optionstable?{}Timing and starting value

Options

OptionTypeDefaultWhat it does
secondsnumber0.8How long the count takes, in seconds
fromnumber0Starting value on mount

Returns

NameTypeWhat it is
value()() -> numberCurrent animated number (unrounded)
counting()() -> booleantrue while counting
skip()() -> ()Jumps straight to the target value
doneLucy.SignalFires when the count reaches the target

Pass counter.value() through Lucy.Format.number to round it and add commas.

Example: coin reward

Count up won coins and play a chime when the count finishes:

local coins = app.state:ref("player.coins")
local displayedCoins = Lucy.useCount(coins, { seconds = 1, from = 0 })

displayedCoins.done:Connect(function()
    sounds.coinChime:Play()
end)

Lucy.new "TextLabel" {
    Text = function()
        return `{Lucy.Format.number(displayedCoins.value())} coins`
    end,
    Parent = hud,
}

Text has this built in via value and count: Lucy.Text.new({ value = coins, count = true }).