PlayerDataController
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-middlewarePlayerDataController:
GetValue
(
key:
string
) →
any?
Returns the current value for a given key post middleware.
local value = PlayerDataController:GetValue("SomeKey")
IsReady
PlayerDataController:
IsReady
(
key:
string
) →
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-middlewarePlayerDataController:
GetRawValue
(
key:
string
) →
any?
Returns the raw value prior to middleware for a given key.
SetMiddleware
PlayerDataController:
SetMiddleware
(
key:
string
,
--
The key to set the middleware for
middlewareHandler:
(
(
use:
(
any
)
→
any
,
value:
any
)
→
(
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-middlewarePlayerDataController:
Observe
(
key:
string
,
--
The key to observe.
callback:
(
newValue:
any
)
→
(
)
--
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-middlewarePlayerDataController:
GetChangedSignal
(
keyName:
string
--
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-middlewarePlayerDataController:
ToFusionState
(
key:
string
,
--
The key to get the observable for
defaultValue:
any?
--
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-middlewarePlayerDataController:
ToRxObservable
(
key:
string
,
--
The key to get the observable for
defaultValue:
any?
--
An optional default value to use if the value is nil
) →
Observable
Returns an Rx Observable that fires when the value changes
BindValue
post-middlewarePlayerDataController:
BindValue
(
key:
string
,
value:
Value
<
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)