Skip to main content

TableReplicator

TableReplicator is a library that enables the easy replication of tables between the server and client. It takes a TableManager on the server and creates a syncronized copy on specified clients. This system was based off of Loleris's Replica system.

BASIC USAGE


[SERVER]

You must first create a TableManager object on the server containing the data table you want to replicate.

local TableManager = require(Packages.TableManager)

local myManager = TableManager.new({
    Age = 18,
})

Then we create a new TableReplicator to handle the replication of our TableManager. In order to do so we must first require the module and get the .Server index of it since we are accessing the Server side.

local TableReplicator = require(Packages.TableReplicator).Server

Next we need a Token in order to identify our Replicator on the Client so we use TableReplicator.Token to construct a unique token. Tokens are unique and can only be created once per string. This is to prevent collisions.

local dataToken = TableReplicator.Token("PlayerData")

Finally we can construct our replicator; passing in the token, the table manager, and setting our ReplicationTargets to All.

local replicator = TableReplicator.new({
    Token = dataToken,
    TableManager = manager,
    ReplicationTargets = "All",
})

The ReplicationTargets are the Player or Players who the Replicator will replicate to. Setting it to All tells the Replicator to replicate to all current and future players. Once the replicator has been constructed it will begin trying to replicate to the appropriate clients.


[CLIENT]

In order to set up the Client we have to get access to the Client side of the package similarly to how we did on the Server.

local TableReplicator = require(Packages.TableReplicator).Client

There are many ways to get the replicators on the Client, but the easiest way is with the forEach function. This will take a Token name and run a function for all existing and future replicators with that Token name.

TableReplicator.forEach("PlayerData", function(replicator)
    local manager = replicator:GetTableManager()

    manager:Observe("Age", function(age)
        print("Age is:", age)
    end)
end)

Inside of the function we can fetch the syncronized TableManager and listen to changes on it. In this case we are observing the Age index.

Finally, somewhere in your client code you must request the existing data from the server. This will begin replication to your client.

TableReplicator.requestServerData()

Properties

Client

This item only works when running on the client. Client
TableReplicator.Client: ClientTableReplicator

Server

This item only works when running on the server. Server
TableReplicator.Server: ServerTableReplicator
Show raw api
{
    "functions": [],
    "properties": [
        {
            "name": "Client",
            "desc": "",
            "lua_type": "ClientTableReplicator",
            "realm": [
                "Client"
            ],
            "source": {
                "line": 98,
                "path": "lib/tablereplicator/src/init.luau"
            }
        },
        {
            "name": "Server",
            "desc": "",
            "lua_type": "ServerTableReplicator",
            "realm": [
                "Server"
            ],
            "source": {
                "line": 105,
                "path": "lib/tablereplicator/src/init.luau"
            }
        }
    ],
    "types": [],
    "name": "TableReplicator",
    "desc": "TableReplicator is a library that enables the easy replication of tables between the server and client.\nIt takes a TableManager on the server and creates a syncronized copy on specified clients. This system\nwas based off of Loleris's Replica system.\n\nBASIC USAGE\n----\n\n----\n**[SERVER]**\n\nYou must first create a `TableManager` object on the server containing the data table you want to replicate.\n```lua\nlocal TableManager = require(Packages.TableManager)\n\nlocal myManager = TableManager.new({\n    Age = 18,\n})\n```\n\nThen we create a new TableReplicator to handle the replication of our TableManager. In order to do so we must first\nrequire the module and get the `.Server` index of it since we are accessing the Server side.\n```lua\nlocal TableReplicator = require(Packages.TableReplicator).Server\n```\nNext we need a `Token` in order to identify our Replicator on the Client so we use `TableReplicator.Token` to \nconstruct a unique token. Tokens are unique and can only be created once per string. This is to prevent collisions.\n```lua\nlocal dataToken = TableReplicator.Token(\"PlayerData\")\n```\n\nFinally we can construct our replicator; passing in the token, the table manager, and setting our ReplicationTargets to\n`All`.\n```lua\nlocal replicator = TableReplicator.new({\n    Token = dataToken,\n    TableManager = manager,\n    ReplicationTargets = \"All\",\n})\n```\nThe `ReplicationTargets` are the Player or Players who the Replicator will replicate to. Setting it to `All` tells\nthe Replicator to replicate to all current and future players. Once the replicator has been constructed it will begin\ntrying to replicate to the appropriate clients.\n\n----\n**[CLIENT]**\n\nIn order to set up the Client we have to get access to the Client side of the package similarly to how we did on the Server.\n```lua\nlocal TableReplicator = require(Packages.TableReplicator).Client\n```\n\nThere are many ways to get the replicators on the Client, but the easiest way is with the `forEach` function. This\nwill take a Token name and run a function for all existing and future replicators with that Token name. \n```lua\nTableReplicator.forEach(\"PlayerData\", function(replicator)\n    local manager = replicator:GetTableManager()\n\n    manager:Observe(\"Age\", function(age)\n        print(\"Age is:\", age)\n    end)\nend)\n```\nInside of the function we can fetch the syncronized TableManager and listen to changes on it. In this case we are observing\nthe Age index.\n\nFinally, somewhere in your client code you must request the existing data from the server. This will begin replication to your\nclient.\n```lua\nTableReplicator.requestServerData()\n```",
    "source": {
        "line": 78,
        "path": "lib/tablereplicator/src/init.luau"
    }
}