Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do I Find the top 5 players in a server and award them something depending on rank?

I am making a round based game where there are 2 leadersats, 1 that resets each round (Egg Points) and one that saves using data stores (Win Points).

LeaderboardGUI looks like this mid round:

[Player | Egg Points | Win Points]
[-------|------------|-----------]
[Joe36  |     90     |     5     ]
[ITxx1a |     74     |     1     ]
[User27 |     21     |     0     ]
[RBLxx  |     20     |     1     ]
[PlayG  |     18     |     0     ]
[sadboy |      3     |     0     ]

What I want is that at the end of the round I will call a function (from a module script) that finds the top 5 players and awards them "Win Points" in the following manner:

#1 = 10pt #2 = 5pt #3 = 3pt #4 = 1pt #5 = 1pt

I have looked up tutorials on youtube etc but the only things that were coming up was how to make 'global' leaderboards which is nothing to do with what I'm after.

I believe I need to put it in a table and sort it, but because the table is full of sub-tables I don't know how to order it since table.sort only works with number values and from lowest to highest, where I want it the highest to lowest

Here's the function I have written

module.FindTopPlayer = function()
	local leaderstats = {}
	for i,v in pairs(game.Players:GetChildren()) do
		table.insert(leaderstats, {v.leaderstats:FindFirstChild("Egg Points").Value, v})
		table.sort(leaderstats)
	end
	local handler = leaderstats[1]
	handler[2].leaderstats:FindFirstChild("Win Points").Value = handler[2].leaderstats:FindFirstChild("Win Points").Value + 10
	local handler = leaderstats[2]
	handler[2].leaderstats:FindFirstChild("Win Points").Value = handler[2].leaderstats:FindFirstChild("Win Points").Value + 5
	local handler = leaderstats[3]
	handler[2].leaderstats:FindFirstChild("Win Points").Value = handler[2].leaderstats:FindFirstChild("Win Points").Value + 3
	local handler = leaderstats[4]
	handler[2].leaderstats:FindFirstChild("Win Points").Value = handler[2].leaderstats:FindFirstChild("Win Points").Value + 1
	local handler = leaderstats[5]
	handler[2].leaderstats:FindFirstChild("Win Points").Value = handler[2].leaderstats:FindFirstChild("Win Points").Value + 1
end

Any help would be very much appreciated. Thanks

1 comment

0

FYI the playernames, I just made up randomly

1 answer

✓ Accepted answer

table.sort allows you to provide a comparator function in which the Callback will be executed on each value to determine the sort precedence. You can use this to access sub-elements and sort in descending order.

function Module.AwardTopFive()
	local RankRewards = {10, 5, 3, 1, 1}
	local AllScores = {}
	---------------
	for _, Player in ipairs(Players:GetPlayers()) do
		local EggPoints = Player.leaderstats["Egg Points"].Value
		local WinPoints = Player.leaderstats["Win Points"]
		table.insert(AllScores, {EggPoints, WinPoints})
	end
	---------------
	table.sort(AllScores, function(EggPointsA, EggPointsB)
		return EggPointsA[1] > EggPointsB[1]
	end)
	---------------
	for Winner, Reward in ipairs(RankRewards) do
		local WinPoints = AllScores[Winner][2]
		if (WinPoints) then
			WinPoints.Value += Reward
		else
			break
		end
	end
end

3 comments

1

Egg Points are collected during the round and I want to give out win points to the top 5, is that what this does then?

3

Mhm.

1

Thank you so much, this really helped :)

This discussion is preserved as it was. New answers, comments, and votes are closed.