How can I sort a player by their points?

Hello! Currently working on a leaderboard system, How would I go about sorting the player points, So whoever has the most, gets put at the top of the leaderboard, and vise versa for the least? I already have the table scripted out, This is how it formats:
image
But i’m just a bit stuck on how to make it order, Also it has to order with the UI I made, There is a scrolling frame and a template so all it has to do is set the text of Template.Rank.Text to the player rank, Template.PlayerName.Text to the player name and Template.Points.Text to the player points.

This is the hierachy of the GUI.
image

Help appreciated :smiley:

1 Like

Using table.sort heres a potentially useful source How to use table.sort?

This is pretty easy!

--Wherever the code your making your template goes here
local Template = Template:Clone()
Temp.LayoutOrder = (Points * -1) --negative because negative shows before positive, if you'd like least to greatest then don't multiply by -1
1 Like

Interesting, But i got this error, The 7 in the screenshot I previously provided was the amount of points the player had, Also the table has the players name AND their points.

Can you show me the code, but from the looks of it your just indexing the points incorrectly

Sure

Client:

RemoteEvents.ReturnLeaderboardData.OnClientEvent:Connect(function(data)
	print(data)
	if data ~= nil then
		print(table.unpack(data))
		local Template = Template:Clone()
		Template.Parent = script.Parent.ScrollingFrame
		local function sort()
			local clonedArray = table.clone(data)
			table.sort(clonedArray, function(a, b)
				return a.Kills > b.Kills
			end)
			for i, v in clonedArray do
				v.Template.LayoutOrder = i
			end
		end
	else
		warn("Data is nil; Game continues")
	end
end)

Server

Events.BindableEvents.AddPoint.OnServerEvent:Connect(function(player, points)
	tableOfPlayers[player.Name] = (tableOfPlayers[player.Name] or 0) + points
	print(tableOfPlayers)
end)

Events.BindableEvents.RoundEnding.OnInvoke = function()
	Events.RemoteEvents.ReturnLeaderboardData:FireAllClients(tableOfPlayers)
	table.clear(tableOfPlayers)
	print(tableOfPlayers)
	print("ends")
end

can you show me the structure of Data? if i was correct data should be PlayerName = Points but this makes me think it is something different

image

Name is dupewastaken and points are 7

RemoteEvents.ReturnLeaderboardData.OnClientEvent:Connect(function(data)
	print(data)
	if data ~= nil then
		print(table.unpack(data))
		local New = Template:Clone()
		New.Parent = script.Parent.ScrollingFrame
                New.LayoutOrder = (data[game.Players.LocalPlayer.Name] or 0) * -1 -- added this
	else 
		warn("Data is nil; Game continues")
	end
end)

fairly sure it would work like this, not really sure where the sort function is being used or how from the code snippet but something like that should work. Then you’d just have to change some properties on the template around

Awesome, One last question, how would I go about extracting the data? So like how would I get the points data out of the table?

It should be as easy as data[PlayerName] = Points, so if you’d like to get the points of the client localplayer you’d do data[game.Players.LocalPlayer.Name] which should return the points (if they exist)

Could you show me an example of setting the points textlabel text to the points in the cloned version? And make it change for every player in the table?

RemoteEvents.ReturnLeaderboardData.OnClientEvent:Connect(function(data)
	if data ~= nil then
		for Name, Points in pairs(data) do
                		local New = Template:Clone()
				New.Parent = script.Parent.ScrollingFrame
				New.LayoutOrder = Points * -1
 				New.Name = Name
				New.PlayerName.Text = Name
				New.Points.Text = tostring(Points)
				New.Rank.Text = ??? -- not sure what rank is, but if it's going to to be something like their placement on the leaderboard which would require you to put everything in to an array and use table.sort
		end
		
	else
		warn("Data is nil; Game continues")
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.