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
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. YieldsConstructs 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
) →
(
)
Changes states in the replicator and replicate whatever changes to the clients
Errors
Type | Description |
---|---|
"Invalid state" | Thrown when the state does not exist in the data structure |
syncPlayer
Syncs a player's state with the server's state
get
Replicator:
get
(
index:
string?
--
If provided, returns the state given, otherwise returns all states
) →
{
[
string
]
:
any
}
|
any
Returns the current state stored in the replicator
Errors
Type | Description |
---|---|
"Invalid state" | Thrown when the state does not exist in the data structure |
getMutable
Replicator:
getMutable
(
) →
{
[
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
(
index:
string?
--
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
Type | Description |
---|---|
"Invalid state" | Thrown when the state does not exist in the data structure |
getStateChangedSignal
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