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

Why is this displaying people in different orders on different clients???

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)

1 answer

✓ Accepted answer

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

7 comments

0

just a quick question, why do you set currentTop to -math.huge???

0

To make it so that the first number compared will be written. It would do no good if currentTop was initialized to 0, and all your numbers were negative. 0 is greater than all negative numbers, so nothing would ever be selected as the greatest value. Setting it to -math.huge basically just ensures that you get an answer, no matter what your numbers are.

0

oh ok thanks

0

I get an error on the line that says "tbl[currentTopPlr] = nil"

0

the error says "table index is nil"

0

I've updated my answer and added a check to make sure the top score exists. If not, it will make the player name "N/A" and the score 0.

0

thanks, it working good now!

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