Skip to content

API Reference โ€‹

PlayerState provides separate APIs for server and client environments with enhanced type-safe interfaces and advanced features.

Overview โ€‹

  • Server Functions - Data manipulation, batch operations, and management (server-only)
  • Client Functions - Data access, change listeners, and caching (client-only)
  • Types - Enhanced TypeScript-style type definitions

๐Ÿ†• Latest Features โ€‹

Server Enhancements โ€‹

  • ๐Ÿš€ Batch Operations - BatchSetValues() and FlushBatch() for high-performance bulk updates
  • ๐Ÿ† Automatic Leaderstats - Integrated leaderstats creation and real-time synchronization
  • ๐Ÿ“Š Enhanced Status Checking - IsPlayerDataReady() for reliable validation
  • ๐Ÿ”ง Cache Management - ClearPathCache() with automatic size limits
  • ๐Ÿ“ฆ Array & Dictionary Operations - Full CRUD operations with validation

Client Enhancements โ€‹

  • โšก Advanced Caching - Automatic value and path caching with cleanup
  • ๐Ÿ“ˆ Enhanced Change Listeners - Detailed action information (Set, TableInsert, etc.)
  • ๐ŸŽฏ Global Change Monitoring - Listen to all changes with "." path
  • โœ… Readiness Checking - IsReady() for reliable data status
  • ๐Ÿงน Manual Cache Control - ClearCache() for debugging and optimization

Architecture โ€‹

PlayerState follows a server-authoritative model with enhanced real-time synchronization:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Server Module โ”‚โ”€โ”€โ”€โ”€โ”‚ ProfileStore     โ”‚ (Data Persistence)
โ”‚                 โ”‚    โ”‚ (Automatic Save) โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Replica โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚              (Real-time Sync)     โ”‚
         โ”‚                                   โ–ผ
         โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Batch System    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚              (Performance)    โ”‚  Client Module  โ”‚
         โ”‚                               โ”‚                 โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ Leaderstats โ”€โ”€โ”€โ”€โ”‚  โ–บ UI Updates   โ”‚
                        (Player List)    โ”‚  โ–บ Game Logic   โ”‚
                                         โ”‚  โ–บ Caching      โ”‚
                                         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Enhanced Data Flow โ€‹

  1. Server: All data modifications with batch processing and validation
  2. ProfileStore: Enhanced persistence with automatic cleanup
  3. Replica: Real-time sync with detailed change information
  4. Client: Cached read access with advanced change detection
  5. Leaderstats: Automatic integration and real-time updates

No Built-in Client Requests

PlayerState does NOT include built-in RemoteEvents or RemoteFunctions. Clients cannot directly request data changes for security reasons. You must create your own RemoteEvents with proper server-side validation for any client-initiated modifications.

Module Locations โ€‹

lua
-- Server Module (Server-side only)
local PlayerStateServer = require(ServerScriptService.Libraries.PlayerState.PlayerStateServer)

-- Client Module (Client-side only)  
local PlayerStateClient = require(ReplicatedStorage.Libraries.PlayerState.PlayerStateClient)

-- Default Data Schema (with leaderstats configuration)
local DefaultData = require(ReplicatedStorage.Libraries.PlayerState.DefaultData)

-- Configuration Module (server and client settings)
local Config = require(ReplicatedStorage.Libraries.PlayerState.PlayerStateConfig)

Enhanced Usage Examples โ€‹

lua
-- Initialize player (enhanced with leaderstats)
local success = PlayerState.Init(player)

-- Check data readiness
if PlayerState.IsPlayerDataReady(player) then
    -- Basic operations with return values
    local success = PlayerState.Set(player, "Coins", 1000)
    local coins = PlayerState.Get(player, "Coins")
    
    -- Batch operations for performance
    PlayerState.BatchSetValues(player, {
        {path = "Coins", value = 2000},
        {path = "Level", value = 5},
        {path = "Plot.Likes", value = 100}
    })
    
    -- Array & Dictionary operations
    PlayerState.AddToArray(player, "Inventory", {Id = "sword", Name = "Iron Sword"})
    PlayerState.SetInDict(player, "Plot.Buildings", "House", {Level = 2})
end
lua
-- Check readiness before operations
if PlayerState.IsReady() then
    -- Read data (with caching)
    local coins = PlayerState.Get("Coins")
    local likes = PlayerState.GetPath("Plot.Likes")
    
    -- Enhanced change listeners with action info
    local connection = PlayerState.OnChanged("Inventory", function(newValue, oldValue, info)
        if info and info.action == "TableInsert" then
            print(`Item added at index {info.index}`)
        elseif info.action == "TableRemove" then
            print(`Item removed from index {info.index}`)
        end
    end)
    
    -- Global change monitoring
    PlayerState.OnChanged(".", function(newValue, oldValue, info)
        if info and info.action then
            local pathString = table.concat(info.path, ".")
            print(`{info.action}: {pathString}`)
        end
    end)
end

-- Manual cache management
PlayerState.ClearCache() -- For debugging or memory optimization

Enhanced Performance

  • Server batch operations process up to 20 changes in 30ms for optimal performance
  • Client caching reduces repeated data access overhead
  • Automatic cache cleanup prevents memory leaks

Leaderstats Integration โ€‹

PlayerState now automatically creates and manages leaderstats:

lua
-- In DefaultData.lua
local DefaultData = {
    Coins = 0,
    Level = 1,
    Plot = { Likes = 0 },
    
    -- Automatic leaderstats configuration
    leaderstats = {
        ["๐Ÿ’ฐ Coins"] = "Coins",
        ["โญ Level"] = "Level", 
        ["๐Ÿ‘ Likes"] = "Plot.Likes"
    }
}

Features:

  • ๐Ÿ† Automatic Creation - Leaderstats appear when player data loads
  • ๐Ÿ”„ Real-time Sync - Updates instantly when data changes
  • ๐Ÿ›ก๏ธ Protection - Prevents direct leaderstats modification
  • ๐ŸŽฏ Path Support - Works with nested data paths

Enhanced Path Notation โ€‹

PlayerState supports dot notation with improved performance:

Enhanced Path Examples
lua
-- Basic paths (cached for performance)
PlayerState.SetPath(player, "Settings.MusicEnabled", false)
PlayerState.SetPath(player, "Plot.Buildings.House.Level", 3)

-- Complex batch operations
PlayerState.BatchSetValues(player, {
    {path = "Stats.Combat.Weapons.Sword.Damage", value = 25},
    {path = "Inventory.Tools.Pickaxe.Durability", value = 100},
    {path = "Settings.Graphics.Quality", value = "High"}
})

-- Array operations with enhanced feedback
PlayerState.AddToArray(player, "Achievements", "first_win") -- Returns success
PlayerState.UpdateArrayItem(player, "Inventory", 1, newItemData) -- Index-based update

-- Dictionary operations
PlayerState.SetInDict(player, "Plot.Buildings", "Windmill", buildingData)
PlayerState.RemoveFromDict(player, "Settings", "ObsoleteSetting")

::: note Automatic Path Creation Paths automatically create nested structures when setting values, with enhanced validation! :::

Enhanced Error Handling & Status โ€‹

All functions now include comprehensive error handling:

lua
-- Server functions return detailed success indicators
local success = PlayerState.Init(player) -- boolean
local ready = PlayerState.IsPlayerDataReady(player) -- boolean
local added = PlayerState.AddToArray(player, "Inventory", item) -- boolean

-- Batch operations with validation
local success = PlayerState.BatchSetValues(player, operations) -- boolean
PlayerState.FlushBatch(player) -- Force immediate processing

-- Client functions with readiness checking
if PlayerState.IsReady() then
    local coins = PlayerState.Get("Coins") -- Safe access
end

-- Enhanced connection management
local connection = PlayerState.OnChanged("Coins", callback)
-- Remember to disconnect: connection:Disconnect()

Performance Optimizations โ€‹

๐Ÿš€ Server Performance โ€‹

  • Batch Processing: Intelligent grouping of operations
  • Path Caching: Reusable path parsing (max 1000 entries)
  • Automatic Cleanup: Memory management for long-running servers
  • Leaderstats Protection: Prevents accidental modification

โšก Client Performance โ€‹

  • Value Caching: Frequently accessed data cached with timestamps
  • Nested Path Caching: Complex path resolution optimization
  • Connection Management: Automatic cleanup and validation
  • Cache Control: Manual ClearCache() for memory management

Migration Guide โ€‹

From Previous Versions โ€‹

Server Changes:

  • Functions now return boolean success status
  • Use IsPlayerDataReady() instead of manual checks
  • Batch operations recommended for multiple changes
  • Leaderstats are automatic (remove manual creation)

Client Changes:

  • Use IsReady() before data operations
  • Enhanced OnChanged() callbacks with action info
  • Global listeners with "." path for debugging
  • Manual cache management available

Next Steps โ€‹

PlayerState - High-Performance Roblox Data Management