RatingService - Implement simple Rating/Elointo your game

A ModuleScript which makes handling Rating/Elo easier.

Inside the ModuleScript there is another ModuleScript called Configuration, inside it you’ll find a START_RATING constant which you can change to your liking. Default is 1500. I want to expand the amount of configurable settings further in the future.

ratingService:UpdateRatings(winner: Player, loser: Player, draw: boolean?): (number, number)
The first argument should be the player who won. The second argument should be the player who lost. In the case of a draw, the order doesn’t matter but you must pass true as the third argument. The method returns two values, the winners new rating and the losers new rating. It also saves the rating to the OrderedDataStore.

ratingService:GetRating(player: Player): number
Simply returns the player’s rating. For now, use this method conservatively. Ratings are currently not cached within the ModuleScript (you can save them yourself to a table) so each call is a OrderedDataStore:GetAsync(...) call. Some method(s) take the player’s rating directly as an argument instead of the player instance.

ratingService:GetHighestRatings(leaderboardSize: number?): {[number]: {string|number}}
This method returns the top 10 highest rated players by default if no argument is provided. It can return a maximum of 100 players (Roblox limitation). The table which is returned looks like this:

leaderboard = {
    [1] = {
        [1] = userId, -- // string
        [2] = rating -- // number
    },
    [2] = {
        [1] = userId, -- // string
        [2] = rating -- // number
    },
    ...
}

The key is the rank of the player in the leaderboard, the value is an array where the first index points to the player’s UserId which you can use in combination with PlayersService to e.g. fetch the player’s name. The second index points to the rating of the player. You can use this method to construct a displayable leaderboard.
(I am not completely happy with that table structure, might try something else in the future)

ratingService:GetPotentialRatingChanges(playerRating: number, opponentRating: number): (number, number, number)
This method calculates potential rating changes based on the outcome. It returns three values: rating gain for a win, rating loss for a loss, and rating change for a draw. You could use this method to display these values in a GUI for the players similar to chess.com.

There are also some other functions/methods which the ModuleScript uses internally.

Made this quickly just now because I need a similar system for my game, so currently there’s no retry mechanic in the cases where a DataStore request fails but if I get around to adding that I will update the Roblox model. If there are any obvious missing features I’ve missed please let me know in the replies and I’ll do my best to add them to the ModuleScript.

2 Likes