Appearance
π Global Leaderboard β
Configuration Required & Runtime Validation
Leaderboard functionality requires proper configuration in PlayerStateConfig!
Make sure your PlayerStateConfig.Server.Leaderboard section is properly configured:
lua
Server = {
Leaderboard = {
Enabled = true,
DataStoreName = "PlayerState_Leaderboards",
TrackedStats = {"Coins", "Level", "HighScore"},
SyncInterval = 60,
UpdateOnPlayerLeave = true,
},
}Runtime Validation: Functions now validate configuration at runtime and will warn/return appropriate values if misconfigured:
GetLeaderboard()returns empty array{}GetPlayerRank()returnsnilUpdateLeaderboard()returnsfalse
Client-synced rank/score (for UI) β
When leaderboards are enabled, PlayerState syncs each playerβs current leaderboard display info to the client via a replicated _LeaderboardRanks table.
What this gives you:
- Reliable UI data: the client can read
{rank, score}for tracked stats without re-deriving it. - Instant-feeling score: when a tracked stat changes (via
Set,SetPath,SetValues, etc.), the syncedscoreupdates right away. - Rank on refresh:
rankupdates on the normal server refresh cycle (based onSyncInterval).
Important:
_LeaderboardRanksis temporary display data and is treated as non-persistent (it will not be saved into player data).- On the client, use
PlayerStateClient.GetLeaderboardInfo(statName)to read this value.
GetLeaderboard() β
GetLeaderboard(statName, topCount) β {LeaderboardEntry}
Retrieves the top players for a specific leaderboard statistic.
Parameters: statName: string, topCount: number
Returns: {LeaderboardEntry} - Array of leaderboard entries sorted by score (highest first), or empty array if configuration issues
Example
lua
-- Get top 10 coin leaders
local coinLeaderboard = PlayerState.GetLeaderboard("Coins", 10)
for i, entry in ipairs(coinLeaderboard) do
print(`#{i}: User {entry.userId} - {entry.score} coins`)
end
-- Get top 100 high scores
local highScores = PlayerState.GetLeaderboard("HighScore", 100)
if #highScores > 0 then
print(`Highest score: {highScores[1].score} by User {highScores[1].userId}`)
endNote
Results are cached for 15 seconds to improve performance.
Configuration Validation
This function validates configuration at runtime:
- Returns empty array
{}and warns ifCONFIG.Leaderboard.Enabled = false - Returns empty array
{}and warns if thestatNameis not inCONFIG.Leaderboard.TrackedStats - Requires the stat name to be properly configured before calling
GetPlayerRank() β
GetPlayerRank(player, statName) β number?
Gets a player's current rank on a specific leaderboard.
Parameters: player: Player, statName: string
Returns: number? - Player's rank (1-based), or nil if not ranked, invalid player, or configuration issues
Example
lua
-- Get player's coin rank
local coinRank = PlayerState.GetPlayerRank(player, "Coins")
if coinRank then
print(`You're ranked #{coinRank} for coins!`)
else
print("You're not yet ranked for coins")
end
-- Check multiple ranks
local levelRank = PlayerState.GetPlayerRank(player, "Level")
local scoreRank = PlayerState.GetPlayerRank(player, "HighScore")
if levelRank and scoreRank then
print(`Level Rank: #{levelRank}, Score Rank: #{scoreRank}`)
endNote
Returns nil if player has no score for this stat or if the player is invalid.
Configuration Validation
This function validates configuration at runtime:
- Returns
niland warns ifCONFIG.Leaderboard.Enabled = false - Returns
niland warns if thestatNameis not inCONFIG.Leaderboard.TrackedStats - Returns
nilif the player is invalid or not found - Requires the stat name to be properly configured before calling
UpdateLeaderboard() β
UpdateLeaderboard(player, statName, score) β boolean
Manually updates a player's leaderboard entry for a specific statistic.
Parameters: player: Player, statName: string, score: number
Returns: boolean - Success status, or false if configuration or validation issues
Example
lua
-- Update player's coin leaderboard
local success = PlayerState.UpdateLeaderboard(player, "Coins", 5000)
if success then
print("Leaderboard updated successfully")
else
warn("Failed to update leaderboard")
end
-- Update multiple stats
PlayerState.UpdateLeaderboard(player, "Level", 25)
PlayerState.UpdateLeaderboard(player, "HighScore", 1200)Note
This function immediately updates the leaderboard. Most stats are automatically updated when data changes, but you can use this for manual updates.
Configuration Validation
This function validates configuration at runtime:
- Returns
falseand warns ifCONFIG.Leaderboard.Enabled = false - Returns
falseand warns if thestatNameis not inCONFIG.Leaderboard.TrackedStats - Returns
falseif the player is invalid - Requires the stat name to be properly configured before calling
- Clears the cache for the updated stat to ensure fresh data
Leaderstats Integration β
π Automatic Leaderstats β
PlayerState now automatically creates and manages leaderstats based on your data configuration:
lua
-- In your DefaultData module, add leaderstats configuration:
local DefaultData = {
Coins = 0,
Level = 1,
Experience = 0,
Plot = {
Likes = 0
},
-- Leaderstats configuration
leaderstats = {
["π° Coins"] = "Coins",
["β Level"] = "Level",
["π Likes"] = "Plot.Likes"
}
}Features:
- Automatic Creation: Leaderstats are created when player data loads
- Real-time Sync: Updates automatically when data changes
- Type-Safe: Numbers use IntValue, others use StringValue
- Path Support: Supports nested paths like
"Plot.Likes"
Leaderstats Protection
Direct modification of leaderstats paths is prevented. Change the source data instead!
lua
-- β This will fail
PlayerState.Set(player, "leaderstats", {...})
-- β
This updates the leaderstat automatically
PlayerState.Set(player, "Coins", 1000)