Setting up Leaderboards

Started by xraybeesx 3 posts 110 views

Reply on sbox.game
  1. #1
    xraybeesx 1,160
    I'm trying to implement a simple leaderboard, I just don't really understand how.


    I have the high score, and best time of my game logged upon completion. 
    How can I make a leaderboard with that?

    they give this code as example: 
     
    var board = Sandbox.Services.Leaderboards.GetFromStat( "facepunch.ss1", "zombies_killed" );
    await board.Refresh();
    
    foreach ( var entry in board.Entries )
    {
    	Log.Info( $"{entry.Rank} - {entry.DisplayName} - {entry.Value}" );
    }
     I just don't understand any of it, what is facepunch.ss1? Where do I put this code?????
  2. #2
    gasleet 6,923
    "facepunch.ss1" is a combination of the organization facepunch and the package/game ident ss1. 
    It means Sausage Survivors 1.
    The code belongs in a new simple component, which you attach to a GameObject.

    I think for all this to work properly, your game has to be published. It doesn't need to be publically available, hidden works. 

    You need to save stats to a leaderboard. I have a public function inside my HighscoreManager.cs to write to the leaderboard "LeaderboardTest". When my player runs out of HP my HealthSystem.cs writes to the leaderboard.
        public void WriteToLeaderboard() 
        {
            // Add score to leaderboard
            Sandbox.Services.Stats.SetValue("LeaderboardTest", CurrentScore );
            LastScore = CurrentScore;
        } 

    To display the board I created this simple component. It uses a TextRenderer as a Property. Both are on the same GameObject.

    using Sandbox;
    using System;
    using System.Threading.Tasks;
    
    public sealed class LeaderboardDisplay : Component
    {
        [Property] TextRenderer boardText;
    
        protected override void OnStart()
        {
            // Call this whenever you want to display/update the leaderboard
            DisplayLeaderboard();
        }
    
        public async Task DisplayLeaderboard()
        {
            // You fetch the stats from the package your organization published. You set your package name when publishing.
            var _scoreBoard = Sandbox.Services.Leaderboards.GetFromStat( "your_organization.your_package", "LeaderboardTest" );
            
            // Configure the created board
            _scoreBoard.SetAggregationMax();
            _scoreBoard.SetSortDescending();
            _scoreBoard.MaxEntries = 15;
    
            boardText.Text = "LOADING...";
            await _scoreBoard.Refresh();
            boardText.Text = "";        
            
            // Loop over the entries and add them to the TextRenderer
            foreach ( var entry in _scoreBoard.Entries )
            {
                boardText.Text += $"\n{entry.CountryCode} #{entry.Rank} {entry.DisplayName}: {Math.Round( entry.Value, 0 )}";
                // Log.Info( $"#{entry.Rank} {entry.DisplayName}: {entry.Value}" );
            }
        }
    }
    Feel free to ask more questions. ❤
    I hope this helps! 🙂👍


  3. edited Jun 2026 #3
    xraybeesx Started this 1,160
    Yea Idk I can't get anything to work, the text is working, but I keep getting: "value cannot be null(parameter 'source')"

    I added this to an empty in my scene:
     
    using Sandbox;
    using System;
    using System.Threading.Tasks;
    
    public sealed class Hsmanager : Component
    {
    	
    	protected async override void OnUpdate()
    	{
    		WriteToLeaderboard();
    		
    	}
    	public static void WriteToLeaderboard()
    	{
    
    		Sandbox.Services.Stats.SetValue( "HighScoreLeaderboard", ScoreManager.HighScore );
    
    	}
    
    
    }
     With the display code attached as well, it just stays on loading..... and I get the error
    edited:
    Never mind it works, I just had to publish like you already said..............TYSM!!!!