Player with the most score system

Hello!

I simply have a question. Is there a way to find the player with the most of a stat, and second highest of that stat? If so, How?

Ive searched through several sites and BOOM! Nothing.

Thanks in advance!!!

3 Likes

Collect all of the values for that singular stat for each player within server and then compare using a sort method to table.insert and remove from current table, then repeat. can be done very easily by using a double for loop.either that or you could use ObjectValues (assuming they are a instance via numbervalue) in which you could just collect them all and do table.sort and then figure out the parent (player) for 1st 2nd and 3rd in the table

1 Like

I have no idea how to do that XD

Im kinda bad at it, But thanks for the advice!!!

Alright so basically what are you trying to do because like you can do this either at the end of a round game, or just if you want to make a global thing for the players inside of the current server

Its an end of the round thing.
:slight_smile:

Well since I know its a round thing now, do you want it to just be the amount of coins they gathered that round or that round + what they already have, or just what they already

I mean, it just needs to display a players name with the most stat for that round, like kills for that round. Every round the Kills reset. so its just the player with the most Kills.

Alright well so basically while the game is going on keep track of the players kills for that round and then after use a remoteEvent to display the winners

Just iterate through the players ingame and check for their kills, then create a table that you can call “ranked KOs” so then you can just sort that table by the highest.

I have no idea how to do that. xD

this is assuming you’re using the classic leaderboard system:

local players_list = game.Players:GetPlayers();

local sort_function = function(player1, player2)
    return player1.leaderboard.KOs.Value > player2.leaderboard.KOs.Value;
end;

table.sort(players_list, sort_function);

what game.Players:GetPlayers() does is store all of the player objects into an array/table and return it. we then use table.sort, which sorts the values in the given table based on the boolean value returned by sort_function. what sort_function does here is check if t[n] (t being the table and n being the current index) is greater than t[n+1] (the next index in the table).

you can place this code wherever you need the sorting done. players_list will have the sorted players ready after the table.sort call. if you didn’t really understand the explanation i recommend learning tables and iterators more (and understanding what callbacks are)

6 Likes

Thank you so much! Now is there a way to display the player’s name that won?
(Has the most KOs)

Just run @mew903’s code and then get the first player with players_list[1]

yea, after the table sort just store the first index into a variable

local winner = players_list[1];

winner will be the Player object with the most KOs in the leaderboard. the next index, player_list[2] will contain the 2nd place winner, and so forth

2 Likes

Thank you all so much! This really helped!

Hey, player_list[2] Isn’t working. did you enter something wrong?
Or does it depend on who is in the game? Like if I’m the only one in game?

if you’re the only one in the game, then there will only be one value in the player_list table - yourself. so yea it does depend on how many players are in the game.

if you’d like to check if there are 2 or more players in the game, you’ll need to check the length of the table with a conditional statement (or an if statement). here’s an example:

local num_players = table.getn(players_list);

if num_players > 1 then
    local runner_up = players_list[2];
    print('The runner up is', runner_up.Name);
else
    print('There is less than two players here');
end;

here, we get the length of players_list as an integer value using the table function table.getn and store it in variable num_players. we then use the conditional if statement to check if num_players is greater than 1. if so, we store the runner up players_list[2] in the variable runner_up and print out his name. if num_players is equal to 1 or less, then we print out that there’s less than two players here.

hope this helps

2 Likes

Thank you so much!!!

This helps immensely!!!

1 Like