terminalCode Example
Leaderboards API
Implement leaderboards using s&box Services API.
Submitting Scores
CSHARP
using Sandbox.Services;
// Submit a score
await Leaderboards.Submit("high_score", playerScore);
// Submit with additional data
await Leaderboards.Submit("level_times", timeSeconds, new Dictionary<string, string>
{
["level"] = currentLevel,
["character"] = playerClass
});Reading Leaderboards
CSHARP
// Get top 10 players
var leaderboard = await Leaderboards.Get("high_score", 10);
foreach (var entry in leaderboard.Entries)
{
Log.Info($"{entry.Rank}. {entry.DisplayName}: {entry.Score}");
}
// Get around a specific rank
var aroundMe = await Leaderboards.GetAround("high_score", myRank, 5);Leaderboard Entry Properties
| Property | Description |
|---|---|
| Rank | Position on leaderboard |
| DisplayName | Player name |
| Score | The score value |
| SteamId | Player's Steam ID |
| Timestamp | When score was submitted |
Web API Access
Query leaderboards from external services:
CODE
GET https://services.facepunch.com/sbox/leaderboard/v1/{appid}/{leaderboard_name}Response:
JSON
{
"entries": [
{
"rank": 1,
"steam_id": 76561197991348132,
"score": 999999,
"display_name": "ProGamer"
}
]
}Real-time Updates
CSHARP
// Subscribe to leaderboard changes
Leaderboards.OnUpdated += (leaderboard) =>
{
if (leaderboard.Name == "high_score")
{
UpdateUI(leaderboard.Entries);
}
};Setup Requirements
- Define leaderboards in Project Settings → Services
- Set score type (integer, float, time)
- Configure sort order (higher/lower is better)
Was this helpful?