Skip to content

Change Listener Functions

Read-only client subscriptions to replicated profile data. See the Change listeners guide for ChangeInfo vs path arrays.

OnChanged

OnChanged(pathOrKey, callback) → ReplicaClient.Connection?

Listens for changes to a specific data path or key.

Parameters: pathOrKey: string, callback: (newValue: any, oldValue: any, changeInfo: ChangeInfo | {string}?) -> ()
Returns: ReplicaClient.Connection? - Connection to disconnect the listener

lua
local coinsConnection = PlayerState.OnChanged("Coins", function(newValue, oldValue)
    updateCoinsUI(newValue)
end)
lua
PlayerState.OnChanged("Plot.Likes", function(newValue, oldValue, pathInfo)
    updateLikesDisplay(newValue)
end)
lua
PlayerState.OnChanged(".", function(newValue, oldValue, changeInfo)
    if changeInfo and changeInfo.action == "TableInsert" then
        print("Item added at index:", changeInfo.index)
    end
end)

Note

Prefer specific paths over "." in production. Disconnect listeners when UI is destroyed.

Managing connections
lua
local connections = {}
connections.coins = PlayerState.OnChanged("Coins", updateCoinsUI)

for _, connection in connections do
    if connection then connection:Disconnect() end
end

PlayerState - High-Performance Roblox Data Management