ReplicatedTableSingleton
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
Types
interface
Config {
ClassTokenName:
string
--
The name of the class token to listen for.
DefaultDataSchema:
table?
--
The default schema to use if the replicator is not ready yet.
ConditionFn:
(
(
replicator:
TableClientReplicator
)
→
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
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
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
(
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
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
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
Gets the TableManager for the ReplicatedTableSingleton. This will error if the TableManager is not ready yet.
local TM = ClientPlayerData:GetTableManager()
GetTableReplicator
Gets the TableReplicator for the ReplicatedTableSingleton. This will error if the TableReplicator is not ready yet.
local TR = ClientPlayerData:GetTableReplicator()
PromiseTableManager
Returns a promise that resolves with the TableManager when it is ready.
ClientPlayerData:PromiseTableManager():andThen(function(TM: TableManager)
print("TableManager is ready!")
end)
PromiseTableReplicator
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
Returns a promise that resolves when the ReplicatedTableSingleton is ready.
ClientPlayerData:OnReady():andThen(function()
print("Found a valid Replicator!")
end)