Skip to main content

PlayerDataController

This item only works when running on the client. Client

Manages various state watchers for replicated Player data.

All methods should be safe to be called at any time, even before RoamStart.

Functions

GetValue

post-middleware
PlayerDataController:GetValue(keystring) → any?

Returns the current value for a given key post middleware.

local value = PlayerDataController:GetValue("SomeKey")

IsReady

PlayerDataController:IsReady(keystring) → boolean

Returns whether or not the value for a given key is ready and loaded.

if PlayerDataController:IsReady("SomeKey") then
	-- Do something
end

GetRawValue

pre-middleware
PlayerDataController:GetRawValue(keystring) → any?

Returns the raw value prior to middleware for a given key.

SetMiddleware

PlayerDataController:SetMiddleware(
keystring,--

The key to set the middleware for

middlewareHandler((
use(any) → any,
valueany
) → (any))?--

A function that is called when the value changes to transform it.

) → ()

Sets the middleware for a specific key that will transform the value before it is returned. The given handler function passes a 'use' function as the first argument, which can be used to add other states as dependencies for the result. The second argument is the raw value. You must then return the actual final value that will be given to the user.

Observe

post-middleware
PlayerDataController:Observe(
keystring,--

The key to observe.

callback(newValueany) → ()--

The function to call.

) → () → ()--

A function to disconnect the observer.

Hooks a callback to a specific key that fires immediately and when the value changes.

GetChangedSignal

post-middleware
PlayerDataController:GetChangedSignal(
keyNamestring--

The key to get the signal for

) → Signal--

The signal that fires when the value changes

Returns a signal that fires when the value changes. Runs post middleware.

local playerLevelSignal = PlayerDataController:GetChangedSignal("Level")

local connection = playerLevelSignal:Connect(function(newValue)
	print("Level changed to", newValue)
end)

task.delay(5, function()
	connection:Disconnect()
end)

ToFusionState

post-middleware
PlayerDataController:ToFusionState(
keystring,--

The key to get the observable for

defaultValueany?--

An optional default value to use if the value is nil. This parameter is deprecated and should be avoided.

) → State<any?>--

A state that reflects the value of the remote property after middleware

Returns a fusion 'state' that is synced with the remote property for a specific key. The state may be nil. The returned state is post middleware, thus you can implement a middleware for nil handling.

local playerLevel = PlayerDataController:ToFusionState("Level")

New("TextLabel")({
	Text = Computed(function(use)
		return "Level: " .. (use(playerLevel) or 0)
	end),
})
Default Value

It is recommended that you implement your own handling of nil values rather than using the defaultValue parameter as it creates a new computed with each call. DefaultParameter has been kept soley for backwards compatability.

ToRxObservable

post-middleware
PlayerDataController:ToRxObservable(
keystring,--

The key to get the observable for

defaultValueany?--

An optional default value to use if the value is nil

) → Observable

Returns an Rx Observable that fires when the value changes

BindValue

post-middleware
PlayerDataController:BindValue(
keystring,
valueValue<any>
) → () → ()

Binds an external fusion value to a remote property for a specific key. Returns a function to disconnect the binding.

local playerLevel = Value(0)
local disconnect = PlayerDataController:BindValue("Level", value)

New("TextLabel")({
	Text = Computed(function(use)
		return "Level: " .. (use(playerLevel) or 0)
	end),
})

task.delay(5, disconnect)
Show raw api
{
    "functions": [
        {
            "name": "GetValue",
            "desc": "Returns the current value for a given key post middleware.\n\n```lua\nlocal value = PlayerDataController:GetValue(\"SomeKey\")\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any?\n"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 96,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "IsReady",
            "desc": "Returns whether or not the value for a given key is ready and loaded.\n\n```lua\nif PlayerDataController:IsReady(\"SomeKey\") then\n\t-- Do something\nend\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 109,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "GetRawValue",
            "desc": "Returns the raw value prior to middleware for a given key.",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "any?\n"
                }
            ],
            "function_type": "method",
            "tags": [
                "pre-middleware"
            ],
            "source": {
                "line": 120,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "SetMiddleware",
            "desc": "Sets the middleware for a specific key that will transform the value before it is returned.\nThe given handler function passes a 'use' function as the first argument, which can be used\nto add other states as dependencies for the result. The second argument is the raw value.\nYou must then return the actual final value that will be given to the user.",
            "params": [
                {
                    "name": "key",
                    "desc": "The key to set the middleware for",
                    "lua_type": "string"
                },
                {
                    "name": "middlewareHandler",
                    "desc": "A function that is called when the value changes to transform it.",
                    "lua_type": "((use: (any) -> any, value: any) -> (any))?"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 136,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "Observe",
            "desc": "Hooks a callback to a specific key that fires immediately and when the value changes.",
            "params": [
                {
                    "name": "key",
                    "desc": "The key to observe.",
                    "lua_type": "string"
                },
                {
                    "name": "callback",
                    "desc": "The function to call.",
                    "lua_type": "(newValue: any) -> ()"
                }
            ],
            "returns": [
                {
                    "desc": "A function to disconnect the observer.",
                    "lua_type": "() -> ()"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 149,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "GetChangedSignal",
            "desc": "Returns a signal that fires when the value changes. Runs post middleware.\n\n```lua\nlocal playerLevelSignal = PlayerDataController:GetChangedSignal(\"Level\")\n\nlocal connection = playerLevelSignal:Connect(function(newValue)\n\tprint(\"Level changed to\", newValue)\nend)\n\ntask.delay(5, function()\n\tconnection:Disconnect()\nend)\n```",
            "params": [
                {
                    "name": "keyName",
                    "desc": "The key to get the signal for",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "The signal that fires when the value changes",
                    "lua_type": "Signal"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 175,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "ToFusionState",
            "desc": "Returns a fusion 'state' that is synced with the remote property for a specific key.\nThe state may be nil. The returned state is post middleware, thus you can implement\na middleware for nil handling.\n\n```lua\nlocal playerLevel = PlayerDataController:ToFusionState(\"Level\")\n\nNew(\"TextLabel\")({\n\tText = Computed(function(use)\n\t\treturn \"Level: \" .. (use(playerLevel) or 0)\n\tend),\n})\n```\n\n:::caution Default Value\nIt is recommended that you implement your own handling of nil\nvalues rather than using the defaultValue parameter as it creates a new computed\nwith each call. DefaultParameter has been kept soley for backwards compatability.\n:::",
            "params": [
                {
                    "name": "key",
                    "desc": "The key to get the observable for",
                    "lua_type": "string"
                },
                {
                    "name": "defaultValue",
                    "desc": "An optional default value to use if the value is nil. This parameter is deprecated and should be avoided.",
                    "lua_type": "any?"
                }
            ],
            "returns": [
                {
                    "desc": "A state that reflects the value of the remote property after middleware",
                    "lua_type": "State<any?>"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 217,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "ToRxObservable",
            "desc": "Returns an Rx Observable that fires when the value changes",
            "params": [
                {
                    "name": "key",
                    "desc": "The key to get the observable for",
                    "lua_type": "string"
                },
                {
                    "name": "defaultValue",
                    "desc": "An optional default value to use if the value is nil",
                    "lua_type": "any?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable\n"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 252,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "BindValue",
            "desc": "Binds an external fusion value to a remote property for a specific key.\nReturns a function to disconnect the binding.\n\n```lua\nlocal playerLevel = Value(0)\nlocal disconnect = PlayerDataController:BindValue(\"Level\", value)\n\nNew(\"TextLabel\")({\n\tText = Computed(function(use)\n\t\treturn \"Level: \" .. (use(playerLevel) or 0)\n\tend),\n})\n\ntask.delay(5, disconnect)\n```",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "Value<any>"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "() -> ()\n"
                }
            ],
            "function_type": "method",
            "tags": [
                "post-middleware"
            ],
            "source": {
                "line": 291,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        },
        {
            "name": "_upsertValue",
            "desc": "Upserts a fusion value for a specific key",
            "params": [
                {
                    "name": "key",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "RemotePropertyHandlers\n"
                }
            ],
            "function_type": "static",
            "private": true,
            "source": {
                "line": 306,
                "path": "src/playerdata/src/Client/PlayerDataController.lua"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "PlayerDataController",
    "desc": "Manages various state watchers for replicated Player data.\n\nAll methods should be safe to be called at any time, even before RoamStart.",
    "realm": [
        "Client"
    ],
    "source": {
        "line": 11,
        "path": "src/playerdata/src/Client/PlayerDataController.lua"
    }
}