Appearance
Change Listener Functions ​
OnChanged ​
OnChanged(pathOrKey, callback) → ReplicaClient.Connection?
Listens for changes to a specific data path or key with enhanced change information.
Parameters: pathOrKey: string, callback: (newValue: any, oldValue: any, changeInfo: ChangeInfo | {string}?) -> ()
Returns: ReplicaClient.Connection? - Connection to disconnect the listener
lua
-- Listen to top-level changes
local coinsConnection = PlayerState.OnChanged("Coins", function(newValue, oldValue)
print("Coins changed from", oldValue, "to", newValue)
updateCoinsUI(newValue)
end)lua
-- Listen to nested changes
local likesConnection = PlayerState.OnChanged("Plot.Likes", function(newValue, oldValue, pathInfo)
print("Likes changed:", newValue)
if pathInfo then
print("Full path:", table.concat(pathInfo, "."))
end
updateLikesDisplay(newValue)
end)lua
-- Listen to ALL changes with detailed action info
local allChangesConnection = PlayerState.OnChanged(".", function(newValue, oldValue, changeInfo)
if changeInfo and changeInfo.action then
local pathString = table.concat(changeInfo.path, ".")
print(`{changeInfo.action}: {pathString} = {newValue} (was {oldValue})`)
-- Handle different action types
if changeInfo.action == "TableInsert" then
print("Item added at index:", changeInfo.index)
elseif changeInfo.action == "TableRemove" then
print("Item removed from index:", changeInfo.index)
end
end
end)Enhanced Change Information:
ChangeInfotype provides detailed action information- Support for
"Set","SetValues","TableInsert","TableRemove"actions - Index information for array operations
- Full path information for all changes
Note
Callbacks fire immediately when data changes with automatic cache invalidation
Managing Connections
lua
-- Store connections for cleanup
local connections = {}
connections.coins = PlayerState.OnChanged("Coins", updateCoinsUI)
connections.level = PlayerState.OnChanged("Level", updateLevelUI)
-- NEW: Global listener with action handling
connections.all = PlayerState.OnChanged(".", function(newValue, oldValue, info)
if info and info.action == "TableInsert" then
print("Array item added!")
end
end)
-- Cleanup when done
for name, connection in pairs(connections) do
if connection then
connection:Disconnect()
end
end