This was my first time attempting something like this sooo I know its bad and poorly done, sorry about that. SO basically I am trying to get the top 3 players to display on a gui that everyone sees at the end of the round, 3 players get displayed but they are not in order. On different clients they are in different orders and stuff. (they are supposed to be in order)
NOTE: This is in a Local Script
openUpGui.OnClientEvent:connect(function(winner) local players = {} for i,v in pairs(game.Players:GetChildren()) do if v.Playing.Value == true then playing = playing + 1 players[v.Name] = v.MatchKills.Value end table.sort(players) end changeHeader(string.upper(winner).." WIN") changePersonalKillsText() changeCoinsObtainedText(winner) local i = 0 for name, kills in pairs(players) do i = i + 1 if i < 4 then if topSpots[i] then topSpots[i].Visible = true topSpots[i].nametext.Text = name topSpots[i].ds.Text = name if game.Players:FindFirstChild(name).Team == game.Teams.Survivors then topSpots[i].nametext.TextColor3 = Color3.new(1,0,0) elseif game.Players:FindFirstChild(name).Team == game.Teams.Infected then topSpots[i].nametext.TextColor3 = Color3.new(0,1,.1) end topSpots[i].killstext.Text = tostring(kills) topSpots[i].killsds.Text = tostring(kills) end end end -- set player data to text guis and stuff open() end)
Table.sort is your problem in this case. It appears to take the dictionary key into account when sorting producing some wacky and incorrect results, for your purposes. Here's an example to show what I mean:
local test = {["test1"] = 1, ["TEST!"] = 100, ["justme"] = 1000} table.sort(test) for i,v in pairs(test) do print(v) end
Output: 1000; 1; 100
Also, the for loop will arbitrarily pick names, because they are unordered unlike numbers.
Here's a sorting algorithm that will return 2 ordered arrays: The first with top scores, and the second with top scoring player names.
local test = {["test1"] = 1, ["TEST!"] = 100} function getTop3(tbl) local topScores = {} local topPlrNames = {} for i = 1, 3 do local currentTop = -math.huge local currentTopPlr for i,v in pairs(tbl) do if v > currentTop then currentTopPlr = i currentTop = v end end if currentTopPlr and not (currentTop == -math.huge) then table.insert(topScores, currentTop) table.insert(topPlrNames, currentTopPlr) tbl[currentTopPlr] = nil else --End of table handling here table.insert(topScores, 0) table.insert(topPlrNames, "N/A") end end return topScores, topPlrNames end local topScores, topPlayers = getTop3(test) for i = 1, 3 do print(topPlayers [i], topScores[i]) end
Hope I helped :)