Skip to content

Board

Server only. Requiring this from a client fails at the require, naming the module.

local Board = require("@game/ServerScriptService/Twill").Board
local wins = Board.new("Wins", { Size = 10 }, bag)
wins:Increment(player.UserId, 1)
wins.OnUpdated:Connect(showTop)

The top of the board is read on a timer and kept, so Top never yields and costs nothing. A read that fails keeps the last one. A value written appears only after it has been written and the next read has happened, and an ordered data store may take a few seconds to show a new value.

Set and Increment do not write. They gather changes per entry, and the board writes them together on a timer, one write per entry.

  • An Increment after a Set adds to the value being set.
  • A Set replaces anything gathered before it.
  • Writing stops when the engine reports no ordered write budget left. The rest waits for the next flush.
  • A write that fails is kept and merged with anything asked for since. A Set asked for since the failure wins.

Whatever is still waiting is written when the server closes, for up to 25 seconds, and in the background when the board is destroyed.

Values are whole numbers below 2^53 in size, because ordered data stores hold nothing else.

[Server]

Opens a leaderboard over an ordered data store.

function Board.new(name: string, options: Options?, owner: Bag?): Board

Parameters

NameTypeDescription
namestringThe ordered data store’s name. 1 to 50 characters.
optionsOptions?How many entries to show, which way round, and how often to read and write.
ownerBag?A bag that destroys the board when it closes.

Returns

Board - The board, reading its first page in the background.

Throws on a name or an option out of range.

FieldDefaultMeaning
Size10Entries shown. 1 to 100.
AscendingfalseLowest first instead of highest first.
Refresh60Seconds between reads. At least 10.
Flush60Seconds between writes. At least 6.

[Server]

Sets an entry to a value, replacing whatever it held.

function Board:Set(key: number | string, value: number)

Parameters

NameTypeDescription
keynumber | stringA user id, or a key of your own of up to 50 characters.
valuenumberA whole number.

Throws on a key or value that cannot be stored.

[Server]

Adds to an entry, or takes away with a negative amount.

function Board:Increment(key: number | string, delta: number)

Throws on a key or amount that cannot be stored.

[Server]

Returns the top of the board as it was last read. Never yields.

function Board:Top(): { Entry }

Returns

{ Entry } - Frozen entries, best first.

FieldTypeMeaning
KeystringThe stored key.
UserIdnumber?The key as a number, when it is one.
ValuenumberThe stored value.
RanknumberPosition on the board, from 1.

[Server]

Writes what is waiting now. A write already under way is waited out first.

function Board:Flush()

Yields.

[Server]

Reads the top of the board now. A read already under way is waited out first.

function Board:Refresh()

Yields.

[Server]

Stops reading and writing on a timer, and writes whatever is waiting in the background. Safe to call more than once.

function Board:Destroy()

Fires with the new entries after every read.

Board.OnUpdated: Signal<(entries: { Entry }) -> ()>
LimitValueSet by
Entries shown100Roblox
Store name and key length50 charactersRoblox
Largest valuebelow 2^53Roblox
Shortest time between reads10 secondsFixed
Shortest time between writes6 secondsFixed
Time to finish writing on shutdown25 secondsFixed