So here is a script that checks which player has the most points in the game, but it breaks whenever two players have the same amount of points. I want it to check if they have the same amount of points, and if they do, I want to make a separate script.
local function GetRichestPlayer()
local Players = game.Players:GetPlayers()
table.sort(Players, function(a,b)
return a.Points.Value > b.Points.Value
end)
return Players[1]
end
local RichestPlayer = GetRichestPlayer()
1 Like
There’s a simple fix to this, that being adding an if statement in case two players have matching values and returning a different value, as such:
if Players[1].Points.Value == Players[2].Points.Value then
return -- whatever you want to return
end
return Players[1]
However, if there are three players who are tied for first, this code won’t work, so you’ll have to find all users who have the same score.
3 Likes
You can create a table for the richest players. Loop through the players starting from the richest to the poorest and check if the player is one of the richest. If so, then add the player to the RichestPlayers table and if not, break the loop and then return that table containing the richest players. Here’s your new code (I have not tested it yet but it should work):
local function GetRichestPlayers()
local Players = game.Players:GetPlayers()
table.sort(Players, function(a,b)
return a.Points.Value > b.Points.Value
end)
local RichestAmount = Players[1].Points.Value
local RichestPlayers = {}
for i, Player in ipairs(Players) do
if Player.Points.Value < RichestAmount then break end
RichestPlayers[#RichestPlayers + 1] = Player
end
return RichestPlayers
end
local RichestPlayers = GetRichestPlayers()
2 Likes
Thanks for replying. Il try this out