Skip to content

Error Handling ​

Safe Data Access with Readiness Checks ​

Example
lua
-- Safe data access (automatically waits for data)
local function getPlayerData()
    return {
        coins = PlayerState.Get("Coins") or 0,
        level = PlayerState.Get("Level") or 1,
        likes = PlayerState.GetPath("Plot.Likes") or 0
    }
end

-- Safe nested access with validation
local function getInventoryItem(index)
    local inventory = PlayerState.GetPath("Inventory")
    if not inventory or typeof(inventory) ~= "table" then
        return nil
    end

    return inventory[index]
end

-- Check if specific data exists
local function hasValidInventory()
    local inventory = PlayerState.GetPath("Inventory")
    return inventory and typeof(inventory) == "table" and #inventory > 0
end

Enhanced Connection Management ​

Example
lua
-- Advanced connection management system
local ConnectionManager = {}
local activeConnections = {}

function ConnectionManager.add(name, connection)
    -- Cleanup existing connection
    if activeConnections[name] then
        if typeof(activeConnections[name]) == "table" and activeConnections[name].Disconnect then
            activeConnections[name]:Disconnect()
        end
    end

    activeConnections[name] = connection
end

function ConnectionManager.remove(name)
    local connection = activeConnections[name]
    if connection and typeof(connection) == "table" and connection.Disconnect then
        connection:Disconnect()
    end
    activeConnections[name] = nil
end

function ConnectionManager.cleanup()
    for name, connection in pairs(activeConnections) do
        if connection and typeof(connection) == "table" and connection.Disconnect then
            connection:Disconnect()
        end
    end
    activeConnections = {}
end

-- Usage with enhanced error handling
ConnectionManager.add("coins", PlayerState.OnChanged("Coins", function(newValue, oldValue, info)
    if typeof(newValue) == "number" then
        updateCoinsDisplay(newValue)
    end
    else
        warn("Invalid coins value received:", newValue)
    end
end))

-- Cleanup when done
ConnectionManager.cleanup()

PlayerState - High-Performance Roblox Data Management