Skip to content

Search documentation

Enter at least two characters.

    Preferences

    Legacy address for preview.2 documentation. Use the versioned page. The API is the same.

    Optional app library · SDK 0.1.0-preview.2.

    Read and write small, ordinary app settings through scoped StorageService. Data is unencrypted; do not use it for passwords or tokens.

    From the extracted SDK folder, create a sibling app with ./lui.cmd new ../MyWorkbench --template workbench. Keep its Vendor files, lui.json, module lock and SDK pin together. See library setup.

    Import require("@LuiWorkbench/Preferences") after the project declares that library. An import copied into a basic starter is not enough.

    This example needs Storage and "Storage": {"ApplicationId": "com.example.notes"} in lui.json. The workbench starter already supplies app-private storage. Use the same project folder when testing persistence.

    Replace Src/Main.luau in that prepared project with this complete file.

    local Window = Instance.new("Window", {Title = "Library example", Size = UDim2.fromOffset(640, 300)})
    local Status = Instance.new("TextLabel", {Parent = Window, Text = "Ready",
    Position = UDim2.fromOffset(16, 190), Size = UDim2.fromOffset(600, 72)})
    local Preferences = require("@LuiWorkbench/Preferences")
    local Store = Preferences.New(app:GetService("StorageService"), "example_theme")
    local Button = Instance.new("TextButton", {Parent = Window, Text = "Save theme", Enabled = false, Size = UDim2.fromOffset(220, 44)})
    Store:Read(function(Bytes, Failure)
    Status.Text = if Failure then "Read failed" elseif Bytes then buffer.tostring(Bytes) else "No theme saved yet"
    Button.Enabled = true
    end)
    Button.Activated:Connect(function()
    Button.Enabled = false
    Store:Write(buffer.fromstring("Dark"), function(_, Failure)
    Status.Text = if Failure then "Save failed" else "Theme saved"
    Button.Enabled = true
    end)
    end)
    Window.Destroying:Connect(function() Store:Dispose() end)
    Window.Visible = true

    Expected result: Reads the saved theme, then enables a Save theme button. Save, close and reopen the same project to read Dark. A copied project has separate storage.

    Preferences.New(Service: StorageService?, Name: string): Store

    Construction is lazy. Keys must contain 1–64 lowercase ASCII letters, digits, underscores or hyphens. Read/Write admit one operation at a time per store; inputs are copied at admission and bounded to 8192 bytes. Missing content is nil without error; an empty buffer is a present empty value. Decode and validate application schemas before applying stored bytes. Oversized reads return ByteLimit; native failure codes pass through, and synchronous unavailable service calls return Unavailable. Completion exceptions produce only the fixed [LUI:Preferences] CompletionFailed diagnostic.

    Operations expose idempotent Cancel (true only for the current operation). Cancel/Dispose retire callbacks before canceling the native request and closing the owned handle. Late Open completions close their handle. No implicit retries occur. Cancellation racing a native write commit does not establish rollback; the native scoped storage contract applies. Dispose is idempotent and starts no new work. The application must cancel independently owned document save receipts.

    This is ordinary, unencrypted preference storage. It cannot store credentials securely or provide transactions across keys. The app needs the Storage capability and an app-private scope. SDK preview.2 desktop project hosts provision one when the manifest includes both the Storage capability and "Storage": {"ApplicationId": "com.example.notes"}. The ID and project directory identity determine the scope: another project with the same ID gets separate data. Packaged and development copies also have separate data. See native storage for locations, failure behavior and migration limits. Other embedders still install the provider explicitly. Published preview.1 does not support this manifest field.

    Exported type declarations — advanced reference

    These types describe returned models/controllers and their options. They are not additional classes to create.

    export type Completion = (Bytes: buffer?, Failure: string?) -> ()
    export type Operation = {read Cancel: (Self: Operation) -> boolean}
    export type Store = {
    Read: (Self: Store, Complete: Completion) -> Operation,
    Write: (Self: Store, Bytes: buffer, Complete: Completion) -> Operation,
    Dispose: (Self: Store) -> (),
    }

    The library source is useful when extending the framework or understanding cleanup. Private support modules such as Scope/State are implementation details, not additional supported app APIs.