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:
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.
--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
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.
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
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
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)