Skip to main content

ReplicatedTableSingleton

This item only works when running on the client. Client

This class provides a system for creating easy access to a single TableReplicator that is guaranteed to exist. This is useful for when you want to access data, that may not have replicated yet, immediately. You provide a default schema to use if the TableReplicator is not ready yet.

Functions

new

ReplicatedTableSingleton.new(configConfig) → ()

Types

interface Config {
ClassTokenNamestring--

The name of the class token to listen for.

DefaultDataSchematable?--

The default schema to use if the replicator is not ready yet.

ConditionFn((replicatorTableClientReplicator) → boolean)?--

A function that returns whether or not the replicator is valid and should be bound.

}

Creates a new ReplicatedTableSingleton.

local ClientPlayerData = ReplicatedTableSingleton.new {
    ClassTokenName = "PlayerData";
    DefaultDataSchema = Import("PlayerDataSchema");
    ConditionFn = function(replicator)
        return replicator:GetTag("UserId") == LocalPlayer.UserId
    end;
}

return ClientPlayerData

Get

ReplicatedTableSingleton:Get(
pathPath,
indexnumber?
) → any?

Fetches the value at the path. An index can be provided to fetch the value at that index. If the value is not ready yet, it will return the value rom the default schema if one was given. If the path is untraversable, it will return nil.

local coins = ClientPlayerData:Get("Coins")
local thirdItem = ClientPlayerData:Get("Inventory", 3) -- Equivalent to `ClientPlayerData:Get("Inventory")[3]`

Observe

ReplicatedTableSingleton:Observe(
pathPath,
callback(newValueany?) → ()
) → () → ()

Called immediately and then whenever the value at the path changes. The callback will be called with the new value.

ClientPlayerData:Observe("Coins", function(newValue)
    print("Coins changed to", newValue)
end)

ListenToValueChange

ReplicatedTableSingleton:ListenToValueChange(
pathPath,
callback(...any) → ()
) → function--

A function that, when called, will disconnect the listener.

Called when the value at the path is changed. The callback will be called with the new value.

ClientPlayerData:ListenToValueChange("Coins", function(newValue)
    print("Coins changed to", newValue)
end)

ListenToAnyChange

ReplicatedTableSingleton:ListenToAnyChange(
pathPath,
callback(...any) → ()
) → () → ()

Called when the value at the path is changed through any means. This includes if the value is an array and a value in the array is changed, inserted, or removed.

ToFusionState

ReplicatedTableSingleton:ToFusionState(pathPath) → State<any>

Returns a Fusion State object that will automatically update when the value at the path changes. This is useful for when you want to use Fusion dependents to respond to changes in the value.

local coinsState = ClientPlayerData:ToFusionState("Coins")

New "TextLabel" {
    Text = coinsState;
}

GetTableManager

ReplicatedTableSingleton:GetTableManager() → TableManager

Gets the TableManager for the ReplicatedTableSingleton. This will error if the TableManager is not ready yet.

local TM = ClientPlayerData:GetTableManager()

GetTableReplicator

ReplicatedTableSingleton:GetTableReplicator() → TableClientReplicator

Gets the TableReplicator for the ReplicatedTableSingleton. This will error if the TableReplicator is not ready yet.

local TR = ClientPlayerData:GetTableReplicator()

PromiseTableManager

ReplicatedTableSingleton:PromiseTableManager() → Promise<TableManager>

Returns a promise that resolves with the TableManager when it is ready.

ClientPlayerData:PromiseTableManager():andThen(function(TM: TableManager)
    print("TableManager is ready!")
end)

PromiseTableReplicator

ReplicatedTableSingleton:PromiseTableReplicator() → Promise<TableClientReplicator>

Returns a promise that resolves with the TableReplicator when it is ready.

ClientPlayerData:PromiseTableReplicator():andThen(function(TR: TableClientReplicator)
    print("TableReplicator is ready!")
end)

IsReady

ReplicatedTableSingleton:IsReady() → boolean

Returns whether or not a valid Replicator has been found and hooked into.

if ClientPlayerData:IsReady() then
    print("We have a valid Replicator!")
end

OnReady

ReplicatedTableSingleton:OnReady() → Promise<()>

Returns a promise that resolves when the ReplicatedTableSingleton is ready.

ClientPlayerData:OnReady():andThen(function()
    print("Found a valid Replicator!")
end)
Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Creates a new ReplicatedTableSingleton.\n\n```lua\nlocal ClientPlayerData = ReplicatedTableSingleton.new {\n    ClassTokenName = \"PlayerData\";\n    DefaultDataSchema = Import(\"PlayerDataSchema\");\n    ConditionFn = function(replicator)\n        return replicator:GetTag(\"UserId\") == LocalPlayer.UserId\n    end;\n}\n\nreturn ClientPlayerData\n```",
            "params": [
                {
                    "name": "config",
                    "desc": "",
                    "lua_type": "Config"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 81,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "Get",
            "desc": "Fetches the value at the path. An index can be provided to fetch the value at\nthat index. If the value is not ready yet, it will return the value rom the\ndefault schema if one was given. If the path is untraversable, it will return\nnil.\n\n```lua\nlocal coins = ClientPlayerData:Get(\"Coins\")\nlocal thirdItem = ClientPlayerData:Get(\"Inventory\", 3) -- Equivalent to `ClientPlayerData:Get(\"Inventory\")[3]`\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "",
                    "lua_type": "Path"
                },
                {
                    "name": "index",
                    "desc": "",
                    "lua_type": "number?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any?\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 136,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "Observe",
            "desc": "Called immediately and then whenever the value at the path changes.\nThe callback will be called with the new value.\n\n```lua\nClientPlayerData:Observe(\"Coins\", function(newValue)\n    print(\"Coins changed to\", newValue)\nend)\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "",
                    "lua_type": "Path"
                },
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(newValue: any?) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 165,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "ListenToValueChange",
            "desc": "Called when the value at the path is changed.\nThe callback will be called with the new value.\n\n```lua\nClientPlayerData:ListenToValueChange(\"Coins\", function(newValue)\n    print(\"Coins changed to\", newValue)\nend)\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "",
                    "lua_type": "Path"
                },
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(...any) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "A function that, when called, will disconnect the listener.",
                    "lua_type": "function"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 183,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "ListenToAnyChange",
            "desc": "Called when the value at the path is changed through any means.\nThis includes if the value is an array and a value in the array is changed, inserted, or removed.",
            "params": [
                {
                    "name": "path",
                    "desc": "",
                    "lua_type": "Path"
                },
                {
                    "name": "callback",
                    "desc": "",
                    "lua_type": "(...any) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 205,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "ToFusionState",
            "desc": "Returns a Fusion State object that will automatically update when the value at\nthe path changes. This is useful for when you want to use Fusion dependents\nto respond to changes in the value.\n\n```lua\nlocal coinsState = ClientPlayerData:ToFusionState(\"Coins\")\n\nNew \"TextLabel\" {\n    Text = coinsState;\n}\n```",
            "params": [
                {
                    "name": "path",
                    "desc": "",
                    "lua_type": "Path"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "State<any>\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 242,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "GetTableManager",
            "desc": "Gets the TableManager for the ReplicatedTableSingleton. This will error if\nthe TableManager is not ready yet.\n\n```lua\nlocal TM = ClientPlayerData:GetTableManager()\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "TableManager\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 263,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "GetTableReplicator",
            "desc": "Gets the TableReplicator for the ReplicatedTableSingleton. This will error if\nthe TableReplicator is not ready yet.\n\n```lua\nlocal TR = ClientPlayerData:GetTableReplicator()\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "TableClientReplicator\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 276,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "PromiseTableManager",
            "desc": "Returns a promise that resolves with the TableManager when it is ready.\n\n```lua\nClientPlayerData:PromiseTableManager():andThen(function(TM: TableManager)\n    print(\"TableManager is ready!\")\nend)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<TableManager>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 292,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "PromiseTableReplicator",
            "desc": "Returns a promise that resolves with the TableReplicator when it is ready.\n\n```lua\nClientPlayerData:PromiseTableReplicator():andThen(function(TR: TableClientReplicator)\n    print(\"TableReplicator is ready!\")\nend)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<TableClientReplicator>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 309,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "IsReady",
            "desc": "Returns whether or not a valid Replicator has been found and hooked into.\n\n```lua\nif ClientPlayerData:IsReady() then\n    print(\"We have a valid Replicator!\")\nend\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 324,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        },
        {
            "name": "OnReady",
            "desc": "Returns a promise that resolves when the ReplicatedTableSingleton is ready.\n\n```lua\nClientPlayerData:OnReady():andThen(function()\n    print(\"Found a valid Replicator!\")\nend)\n```",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<()>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 339,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "Config",
            "desc": "",
            "fields": [
                {
                    "name": "ClassTokenName",
                    "lua_type": "string",
                    "desc": "The name of the class token to listen for."
                },
                {
                    "name": "DefaultDataSchema",
                    "lua_type": "table?",
                    "desc": "The default schema to use if the replicator is not ready yet."
                },
                {
                    "name": "ConditionFn",
                    "lua_type": "((replicator: TableClientReplicator) -> boolean)?",
                    "desc": "A function that returns whether or not the replicator is valid and should be bound."
                }
            ],
            "source": {
                "line": 60,
                "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
            }
        }
    ],
    "name": "ReplicatedTableSingleton",
    "desc": "This class provides a system for creating easy access to a single TableReplicator\nthat is guaranteed to exist. This is useful for when you want to access data, that\nmay not have replicated yet, immediately. You provide a default schema to use if\nthe TableReplicator is not ready yet.",
    "realm": [
        "Client"
    ],
    "source": {
        "line": 12,
        "path": "src/tablereplicator/src/Client/ReplicatedTableSingleton.lua"
    }
}