# Online Subsystem Write to Leaderboard

**URL:** <https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623>\
**Category:** C++\
**Tags:** onlinesubsystem, leaderboard, unreal-engine, question, CPP\
**Created:** [May 26, 2023, 7:20pm UTC](https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623 "2023-05-26T19:20:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 26, 2023, 7:20pm UTC](https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623/1 "2023-05-26T19:20:44Z")

</div>

Hello, I’m trying to implement Leaderboard using OnlineSubsystem Leaderboards Interface. And it’s not working for me.

**Steam is properly integrated in to my game** - Achievements and online Sessions work perfectly fine.

I’ve created the Leaderboard on Steam (image below)

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/8/0/98073cd9e46b3a65ff0518676404a849d22c621d.png)

This is the code I’m trying to use to write to the Leaderboard.

```
		IOnlineLeaderboards* Leaderboards = OnlineSubsystem->GetLeaderboardsInterface().Get();
		if (Leaderboards != nullptr)
		{
			const FUniqueNetIdPtr NetId = RetrievePlayerUniqueNetId(PlayerController); // works fine for achievements
			FOnlineLeaderboardWrite NewLeaderboardInfo = FOnlineLeaderboardWrite();
			NewLeaderboardInfo.DisplayFormat = ELeaderboardFormat::Number;
			NewLeaderboardInfo.SortMethod = ELeaderboardSort::Ascending;
			NewLeaderboardInfo.UpdateMethod = ELeaderboardUpdateMethod::KeepBest;
			NewLeaderboardInfo.LeaderboardNames = TArray<FName> { FName("TestLeaderboard") };
			NewLeaderboardInfo.RatedStat = "BestLap";
			NewLeaderboardInfo.SetIntStat("BestLap", 140);

			const bool Result = Leaderboards->WriteLeaderboards(NAME_GameSession, *NetId.Get(), NewLeaderboardInfo);
		}

```

The error I get is this:

```auto
[2023.05.26-18.14.05:926][544]LogOnlineLeaderboard: Warning: STEAM: Failure to write key value pair when uploading to Steam TestLeaderboard_BestLap=140
[2023.05.26-18.14.05:928][544]LogOnline: Warning: OSS: Async task 'FOnlineAsyncTaskSteamUpdateStats bWasSuccessful: 0 User: Bojan [0xSOME_NUMBER]' failed in 0.622502 seconds
[2023.05.26-18.14.05:942][545]LogOnline: Warning: OSS: Async task 'FOnlineAsyncTaskSteamUpdateLeaderboard bWasSuccessful: 0 Leaderboard: TestLeaderboard Score: 0' failed in 0.638273 seconds

```

What am I doing wrong? Do I need to add Leaderboards in DefaultEngine.ini in the same way I have added Achievements? If yes, what is the syntax for that? Maybe I should do some additional work on Steamworks side? If yes, what should I do?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 27, 2023, 3:45pm UTC](https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623/2 "2023-05-27T15:45:38Z")

</div>

Anyone has any idea? I’m sorry for bringing it up, but I really need this one 🙂  
I forgot to mention, I’m running the game using BAT file.

---

<div class="post-metadata">

**Author:** ![Bojann](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bojann/32/906560_2.png) [@Bojann](https://forums.unrealengine.com/u/Bojann)\
**Post date:** [May 31, 2023, 6:04pm UTC](https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623/3 "2023-05-31T18:04:40Z")

</div>

OK, so I managed to figure it out.

If you create Leaderboard named `Jynkyard01`, you also must go to stats and create stat named `Jynkyard01_Jynkyard01`

Then, you can do this in code to submit to leaderboard, and it should work just fine

```auto
void YourClass::SubmitLeaderboardScore(APlayerController* PlayerController, FName StatName, int32 StatValue)
{
	FOnlineLeaderboardWrite WriteObject;
	WriteObject.LeaderboardNames.Add(StatName);
	WriteObject.RatedStat = StatName;
	WriteObject.DisplayFormat = ELeaderboardFormat::Milliseconds;
	WriteObject.SortMethod = ELeaderboardSort::Ascending;
	WriteObject.UpdateMethod = ELeaderboardUpdateMethod::KeepBest;
	WriteObject.SetIntStat(StatName, StatValue);
	bool Successful = DoWriteLeaderboardObject(PlayerController, WriteObject);
	if (Successful)
	{
		// Do success logic	
	} else
        {
        // Do fail logic
	}
}

```

```auto
bool YourClass::DoWriteLeaderboardObject(APlayerController* PlayerController, FOnlineLeaderboardWrite& WriteObject)
{
	if (APlayerState* PlayerState = (PlayerController != NULL) ? PlayerController->PlayerState : NULL)
	{
		FUniqueNetIdPtr UserId = PlayerState->GetUniqueId().GetUniqueNetId();
		if (UserId.IsValid())
		{
			if (IOnlineSubsystem* const OnlineSub = IOnlineSubsystem::IsLoaded() ? IOnlineSubsystem::Get() : nullptr)
			{
				IOnlineLeaderboardsPtr Leaderboards = OnlineSub->GetLeaderboardsInterface();
				if (Leaderboards.IsValid())
				{
					bool bResult = Leaderboards->WriteLeaderboards(PlayerState->SessionName, *UserId, WriteObject);

					bool bFlushResult = Leaderboards->FlushLeaderboards(PlayerState->SessionName);

					return bResult && bFlushResult;
				}
				else
				{
					FFrame::KismetExecutionMessage(TEXT("WriteLeaderboardObject - Leaderboards not supported by Online Subsystem"), ELogVerbosity::Warning);
				}
			}
			else
			{
				FFrame::KismetExecutionMessage(TEXT("WriteLeaderboardObject - Invalid or uninitialized OnlineSubsystem"), ELogVerbosity::Warning);
			}
		}
		else
		{
			FFrame::KismetExecutionMessage(TEXT("WriteLeaderboardObject - Cannot map local player to unique net ID"), ELogVerbosity::Warning);
		}
	}
	else
	{
		FFrame::KismetExecutionMessage(TEXT("WriteLeaderboardObject - Invalid player state"), ELogVerbosity::Warning);
	}

	return false;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 30, 2023, 6:04pm UTC](https://forums.unrealengine.com/t/online-subsystem-write-to-leaderboard/1179623/4 "2023-06-30T18:04:56Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
