How could I go about making a global leaderboards that relies on two players?

In a new game that I am developing, I wish to encourage partnership/duos gameplay. I already have a game that has a global leaderboard and shows top players etc. But I wish to make a global leaderboard that relies on two people who have chosen to partner up. Let’s say the leaderboard is for loyalty points. A player chooses another player in game as their fighting partner, and such information saves, both on the choosing player and the chosen player. For as long as they have their partner data as each other, their loyalty points are (let’s say) increments by one, or their total combined power level which will be the determining factor on where they are in the Out-Of-50 global leaderboard.

Now the issue here is that I want players to have the ability to disband their partnership (hopefully no friendships will be broken as a result), and then either player does, it removes them from the leaderboard as a result. It would be terrible if a player wishes to disband a partnership but their name is still associated with someone that they’ve disbanded (which is visible to anyone in the game as it’s a global leaderboard).

I further want to elaborate that i do not wish for it to rely on both players being in the same game or both players being online at the same time.

I also am using Datastore2. This concept has me scratching my head, could anyone possibly guide me on which services/methods I should use to achieve such a system.

1 Like

I do think it would be the same as a normal DataStore but just use 1 key connecting both players? If someone disbands then reset the data.

1 Like

I personally have no experience with DataStore2 whatsoever, so I won’t be using nor mentioning any API functions from DataStore2, and just using APIs of datastores.

When both players have agreed to a partnership, you can make a new key in your OrderedDataStore which is a combination of both players’ UserIds and the entry’s value is their combined power levels, and you’d also need the two normal (or ordered - it doesn’t really matter) for each individual player, which I assume that you would give the players a datastore of their own for their stats already:

local ODS = game:GetService("DataStoreService"):GetOrderedDataStore("Top50Duos")
local combinedKey = player1.UserId .. ":" .. player2.UserId
local combinedPower = player1.PowerLevel.Value + player2.PowerLevel.Value
ODS:SetAsync(player1.UserId .. ":" .. player2.UserId, combinedPower)

The colon is there as a separator, just in case you want to distinguish both players both from their combined key in the datastore

When a player wishes to disband a partnership, use RemoveAsync on their combined key to remove it from the ordereddatastore, and when the leaderboard is updated, it’ll show that the players are no longer in a partnership / duo anymore:

local ODS = game:GetService("DataStoreService"):GetOrderedDataStore("Top50Duos")
local combinedKey = player1.UserId .. ":" .. player2.UserId
ODS:RemoveAsync(combinedKey)

Occasionally, you’d want to update the players’ combined entry in the datastore whenever their data changes, and/or when they leave the game, and/or when it’s time to do an autosave. You can use SetAsync or UpdateAsync, though it is preferable to use UpdateAsync since it can be used to verify the data, but you should be aware of these functions’ limits.

For this, you’d need the normal data store of the player currently in the game and wishes for their data to be saved, find the difference between their new value with their old value in datastore (so, it shouldn’t be saved yet, unless you have reference to the old value), then add the value in UpdateAsync or SetAsync

3 Likes