Skip to main content

Replicator

This is the main class of SimplyReplicate Replicators are used to replicate data from the server to the client with ease

Here is an example implementation of a replicator:

Server:

local Replicator = require(path.to.module)

local GameStateReplicator = Replicator.new("GameState", {
	Status = "Waiting",
	RoundStarted = false,
})

task.wait(3)

GameStateReplicator:changeStates({
	Status = "Game starts soon",
})

task.wait(3)

GameStateReplicator:changeStates({
	Status = "Game started",
	RoundStarted = true,
})	

Client:

local Replicator = require(path.to.module)

local GameStateReplicator = Replicator.new("GameState")

GameStateReplicator.StateChanged:Connect(function(state, value)
	print("State changed", state, value)
end)

Properties

StateChanged

Replicator.StateChanged: Signal<string,any>

A signal that fires when a state has changed

Functions

new

This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. Yields
Replicator.new(
identifierstring | Instance,
data{[string]any}--

The data that will be replicated to clients

) → Replicator

Constructs a new replicator

warning

When creating a replicator on the client, this function will yield until the initial data has been fetched from the server

info

Note that if you are using strict luau, you will want to specify a type for the data parameter, if you let Luau infer the type, when you go to change the state, it will want you to include every state as it wont have an option to be nil.

changeStates

Replicator:changeStates(
changedStates{[string]any?},--

The states that have been changed and their new values

players({Player} | Player)?
) → ()

Changes states in the replicator and replicate whatever changes to the clients

Errors

TypeDescription
"Invalid state"Thrown when the state does not exist in the data structure

syncPlayer

Replicator:syncPlayer(playerPlayer) → ()

Syncs a player's state with the server's state

get

Replicator:get(
indexstring?--

If provided, returns the state given, otherwise returns all states

) → {[string]any} | any

Returns the current state stored in the replicator

Errors

TypeDescription
"Invalid state"Thrown when the state does not exist in the data structure

getMutable

Replicator:getMutable(
players{Player} | Player?--

If provided, the mutable data will only be able to change states for the given players

) → {[string]any}--

A mutable version of the current state stored in the replicator

Returns a mutable version of the current state stored in the replicator, when a state is changed in the mutable data, it will automatically replicate the change to the client

caution

Do not use table.clear on a table in the mutable data, it will not replicate the change to the clients

getForPlayer

Replicator:getForPlayer(
playerPlayer,
indexstring?--

If provided, returns the state given, otherwise returns all states

) → {[string]any} | any

Returns the current state stored in the replicator for a specific player

Errors

TypeDescription
"Invalid state"Thrown when the state does not exist in the data structure

getStateChangedSignal

Replicator:getStateChangedSignal(
statestring--

The state to listen for changes

) → Signal<any>

Returns a signal that fires when a specific state has changed

Destroy

Replicator:Destroy() → ()

This function should be called when you are done using the replicator

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new replicator\n\n\n:::warning\nWhen creating a replicator on the client, this function will yield until the initial data has been fetched from the server\n:::\n\n:::info\nNote that if you are using strict luau, you will want to specify a type for the data parameter,\nif you let Luau infer the type, when you go to change the state, it will want you to include every state as it wont have an option to be nil.\n:::",
            "params": [
                {
                    "name": "identifier",
                    "desc": "",
                    "lua_type": "string | Instance"
                },
                {
                    "name": "data",
                    "desc": "The data that will be replicated to clients",
                    "lua_type": "{ [string]: any }"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Replicator"
                }
            ],
            "function_type": "static",
            "yields": true,
            "source": {
                "line": 149,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "changeStates",
            "desc": "Changes states in the replicator and replicate whatever changes to the clients",
            "params": [
                {
                    "name": "changedStates",
                    "desc": "The states that have been changed and their new values",
                    "lua_type": "{ [string]: any? }"
                },
                {
                    "name": "players",
                    "desc": "",
                    "lua_type": "({ Player } | Player)?"
                }
            ],
            "returns": [],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"Invalid state\"",
                    "desc": "Thrown when the state does not exist in the data structure"
                }
            ],
            "source": {
                "line": 215,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "syncPlayer",
            "desc": "Syncs a player's state with the server's state",
            "params": [
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 232,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "get",
            "desc": "Returns the current state stored in the replicator",
            "params": [
                {
                    "name": "index",
                    "desc": "If provided, returns the state given, otherwise returns all states",
                    "lua_type": "string?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ [string]: any } | any\r\n"
                }
            ],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"Invalid state\"",
                    "desc": "Thrown when the state does not exist in the data structure"
                }
            ],
            "source": {
                "line": 517,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "getMutable",
            "desc": "Returns a mutable version of the current state stored in the replicator, when a state is changed in the\nmutable data, it will automatically replicate the change to the client\n\n:::caution\nDo not use `table.clear` on a table in the mutable data, it will not replicate the change to the clients\n:::",
            "params": [
                {
                    "name": "players",
                    "desc": "If provided, the mutable data will only be able to change states for the given players",
                    "lua_type": "{ Player } | Player?"
                }
            ],
            "returns": [
                {
                    "desc": "A mutable version of the current state stored in the replicator",
                    "lua_type": "{ [string]: any }"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 544,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "getForPlayer",
            "desc": "Returns the current state stored in the replicator for a specific player",
            "params": [
                {
                    "name": "player",
                    "desc": "",
                    "lua_type": "Player"
                },
                {
                    "name": "index",
                    "desc": "If provided, returns the state given, otherwise returns all states",
                    "lua_type": "string?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ [string]: any } | any\r\n"
                }
            ],
            "function_type": "method",
            "errors": [
                {
                    "lua_type": "\"Invalid state\"",
                    "desc": "Thrown when the state does not exist in the data structure"
                }
            ],
            "source": {
                "line": 629,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "getStateChangedSignal",
            "desc": "Returns a signal that fires when a specific state has changed",
            "params": [
                {
                    "name": "state",
                    "desc": "The state to listen for changes",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Signal<any>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 659,
                "path": "lib/Replicator.luau"
            }
        },
        {
            "name": "Destroy",
            "desc": "This function should be called when you are done using the replicator",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 674,
                "path": "lib/Replicator.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "StateChanged",
            "desc": "A signal that fires when a state has changed\n\t",
            "lua_type": "Signal<string, any>",
            "source": {
                "line": 166,
                "path": "lib/Replicator.luau"
            }
        }
    ],
    "types": [],
    "name": "Replicator",
    "desc": "This is the main class of SimplyReplicate\nReplicators are used to replicate data from the server to the client with ease\n\nHere is an example implementation of a replicator:\n\n**Server:**\n```lua\nlocal Replicator = require(path.to.module)\n\nlocal GameStateReplicator = Replicator.new(\"GameState\", {\n\tStatus = \"Waiting\",\n\tRoundStarted = false,\n})\n\ntask.wait(3)\n\nGameStateReplicator:changeStates({\n\tStatus = \"Game starts soon\",\n})\n\ntask.wait(3)\n\nGameStateReplicator:changeStates({\n\tStatus = \"Game started\",\n\tRoundStarted = true,\n})\t\n```\n\n**Client:**\n```lua\nlocal Replicator = require(path.to.module)\n\nlocal GameStateReplicator = Replicator.new(\"GameState\")\n\nGameStateReplicator.StateChanged:Connect(function(state, value)\n\tprint(\"State changed\", state, value)\nend)\n```",
    "source": {
        "line": 124,
        "path": "lib/Replicator.luau"
    }
}