PlayerData
Provides an interface for managing player data using ProfileService and ReplicaService.
Functions
GetPlayerDataReplica
PlayerData:GetPlayerDataReplica() → Replica?--
The Replica object associated with the given player, or nil if the player has left.
Returns the Replica object associated with the given player. Modifying
tables/arrays should be done through the Replica instead of
GetValue/SetValue in order to make sure they replicate properly. The
Replica API can be found at:
https://madstudioroblox.github.io/ReplicaService/api/#replica
NOTE: Can return nil if the player has left the game, otherwise it will
yield until the replica is available.
GetValue
PlayerData:GetValue(keyPath: string | {string}--
The path to the value being accessed, either as a dot-separated string or an array of strings.
) → any?--
The value at the given keyPath, or nil if it doesn't exist.
Returns the value at the given keyPath. Keys can be passed in any of the
following ways:
PlayerData:GetValue(player, "Tokens")
PlayerData:GetValue(player, "Powerups.ExtraLives")
PlayerData:GetValue(player, { "Powerups", "ExtraLives" })
SetValue
PlayerData:SetValue(keyPath: string | {string},--
The path to the value being modified, either as a dot-separated string or an array of strings.
newValue: any--
The new value to set at the given keyPath.
) → ()
Sets the value at the given keyPath to the given newValue. Keys can be
passed in any of the following ways:
PlayerData:SetValue(player, "Tokens", 0)
PlayerData:SetValue(player, "Powerups.ExtraLives", 1)
PlayerData:SetValue(player, { "Powerups", "ExtraLives" }, 1)
IncrementNumValue
PlayerData:IncrementNumValue(keyPath: string | {string},--
The path to the value being modified, either as a dot-separated string or an array of strings.
increment: number--
The amount to increment the value by. Must be a non-zero number.
) → number?--
The new value after incrementing, or nil if the increment was invalid.
Increments the value at the given keyPath by the given increment. The
current stored value must be a number or nil. If the current value is nil,
it is first initialized to 0, then incremented. The increment must be a
non-zero number. Keys can be passed in any of the following ways:
PlayerData:IncrementNumValue(player, "Tokens", 1)
PlayerData:IncrementNumValue(player, "Powerups.ExtraLives", 1)
PlayerData:IncrementNumValue(player, { "Powerups", "ExtraLives" }, 1)
Observe
PlayerData:Observe(keyPath: string | {string},--
The path to the value being observed, either as a dot-separated string or an array of strings.
callback: (newValue: any,oldValue: any) → ()--
The function to call when the value changes. Will also be called immediately with the current value.
) → RBXScriptConnection?--
The connection object for the change listener, or nil if the player has left.
Immediately processes the callback on the current value at the given keyPath
and listens for changes. Keys can be passed in any of the following ways:
PlayerData:Observe(player, "Tokens", callback)
PlayerData:Observe(player, "Powerups.ExtraLives", callback)
PlayerData:Observe(player, { "Powerups", "ExtraLives" }, callback)
SetValueCallback
PlayerData:SetValueCallback(keyPath: string | {string},callback: (newValue: any,oldValue: any) → (),runImmediately: boolean?) → RBXScriptConnection?
Sets a callback function to be run when the value in path is changed. If
runImmediately is true, the callback will also be run immediately with the
current value. Keys can be passed in any of the following ways:
PlayerData:SetValueCallback(player, "Tokens", callback, true)
PlayerData:SetValueCallback(player, "Powerups.ExtraLives", callback, true)
PlayerData:SetValueCallback(player, { "Powerups", "ExtraLives" }, callback, true)
CreateProfileHold
PlayerData:CreateProfileHold() → string?--
A unique hold ID that can be used to release the hold, or nil if the player's profile doesn't exist.
Creates a hold on the player's profile, preventing it from being released until the hold is released. This can be used to prevent the player's data from being unloaded while asynchronous operations are still pending. Returns a unique hold ID that can be used to release the hold.
ReleaseProfileHold
PlayerData:ReleaseProfileHold(holdId: string--
The unique hold ID that was returned by CreateProfileHold.
) → ()Releases a hold on the player's profile with the given hold ID. If the hold ID is invalid or doesn't exist, this function does nothing.