Skip to content

Performance Tips

Client-side patterns for responsive UI without fighting replication or leaking listeners.

Readiness

  • Get / GetPath wait until data is ready—no manual wait loop required for normal UI.
  • Use IsReady() when you want to skip work (hide UI, show placeholder) before data exists.
  • For bulk UI setup, check IsReady() once, then read paths—not once per label.

See Status — IsReady.

Listen on specific paths

Avoid a global "." listener unless you are debugging.

  • One listener per UI concern: "Coins", "Inventory", "Plot.Likes".
  • In a global listener, branch on info.action and info.path—do not repaint the whole HUD every time.
lua
-- Narrow
PlayerState.OnChanged("Coins", updateCoinsLabel)

-- Debug only
PlayerState.OnChanged(".", onAnyChange)

See Change listeners.

Disconnect listeners

Every OnChanged (and shared-session listener) should be disconnected when:

  • The local player leaves.
  • The UI instance is destroyed.
  • The feature is torn down (shop closed, plot unloaded).
lua
local conn = PlayerState.OnChanged("Coins", updateCoins)
gui.Destroying:Connect(function()
    if conn then conn:Disconnect() end
end)

See Best practices — memory.

Safe reads

  • Use or fallbacks: PlayerState.Get("Coins") or 0.
  • Validate shape before iterating: typeof(inventory) == "table".
  • Do not assume nested tables exist—check GetPath for nil.
lua
local inventory = PlayerState.GetPath("Inventory")
if typeof(inventory) ~= "table" then
    inventory = {}
end

Do not mutate returned tables

Get / GetPath can return references to replicated data.

  • Mutating the table locally can change shared state unintentionally.
  • Use Clone() when you need a local copy to edit offline in UI code.
  • To change saved data, use your server remotes—the client API is read-only.
lua
-- Risky
local t = PlayerState.GetPath("Settings")
t.MusicEnabled = false

-- Safer for local UI logic
local t = PlayerState.Clone(PlayerState.GetPath("Settings"))

See Common pitfalls.

Cache

  • ClearCache() is for debugging or rare recovery—not routine use.
  • The client rebuilds cache on access after a clear.

See Cache.

Shared session

See Shared session.

PlayerState - High-Performance Roblox Data Management