1st place, 2nd place, etc. not ordered right in Gui list

Hi you all. I’m developing an obby game with a round system, and there is a placement system (1st place, 2nd place, etc.)

For some reason, the Gui that displays the placements does not order right, and sometimes gives the wrong placements to the wrong players. The Gui list does go from 1st to 2nd to 3rd etc., but the actual players shown are in the wrong placements.
This is only a front end problem with the Gui, because everything else in values work, and everything else that uses these placements are ordered correctly. (Although I’m not sure if the keyset(Basically the competitivePlacement Dictionary reversed for ordering) is right, but I think it is.) I just need a correct method for ordering it, because I couldn’t find anything helpful.

So with that out of the way here is the Gui code in question:

--Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local plr = Players.LocalPlayer

--Remote Events
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local WinDisplayEvents = RemoteEvents:WaitForChild("WinDisplayEvents")
local WinGuiDisplayEvent = WinDisplayEvents:WaitForChild("WinGuiDisplay")
local WinGuiVisibleEvent = WinDisplayEvents:WaitForChild("WinGuiVisible")

--Gui Variables
local PlacementFrame = plr:WaitForChild("PlayerGui"):WaitForChild("WinnersGui"):WaitForChild("PlacementFrame")
local templateFrame = PlacementFrame:WaitForChild("Storage"):WaitForChild("PlayerFrame")
local ScrollingFrame = PlacementFrame:WaitForChild("ScrollingFrame")


WinGuiDisplayEvent.OnClientEvent:Connect(function(competitivePlacements, displayProperties, keyset)
	
	if typeof(competitivePlacements) ~= "table" then print("competitivePlacements not valid") return end
	if typeof(displayProperties) ~= "table" then print("displayProperties not valid") return end
	if typeof(keyset) ~= "table" then print("keyset not valid") return end
	
	print("Client is valid!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
	print(competitivePlacements)
	print(displayProperties)
	print(keyset)
	

	--Maybe keyset is wrong, I think it isn't
	
    --------------------------------------------------
	--THE PROBLEM:
	
	
	local placementProgressVar = 0
	
	
	for i, v in pairs(competitivePlacements) do 
		
		placementProgressVar += 1
		
		for i, placement in pairs(competitivePlacements) do
			if placement == placementProgressVar then
				print(placement)
				local newFrame = templateFrame:Clone()
				local placementLabel = newFrame:WaitForChild("PlacementLabel")
				local nameLabel = newFrame:WaitForChild("NameLabel")
		
				placementLabel.Text = displayProperties[placement][1]
				placementLabel.TextColor3 = displayProperties[placement][2]
		
				nameLabel.Text = Players:GetPlayerByUserId(keyset[placement]).Name
				print("_____________________")
				print(Players:GetPlayerByUserId(keyset[placement]).Name)
				print("_____________________")
		
				newFrame.Visible = true
				newFrame.Parent = ScrollingFrame
			end
		end
	end
	
	
	--END OF PROBLEM
	---------------------------------------------
	
	
	
	
end)


WinGuiVisibleEvent.OnClientEvent:Connect(function()
		
	PlacementFrame.Visible = true
	
end)

Also know that the code for creating the keyset is in the Competitive Module below.

Well here is the related code:

Function called when Round Ends (Part of Server Script)
--Function that handles the competitive placement system rewards
local function competitivePlacementDisplay(plr)
	
	--Variables
	local place
	local reward
	--local err, success = pcall(function()
	--print(competiviveModule.competitivePlacements)
	place = competiviveModule.competitivePlacements[plr.UserId]
	reward = math.ceil(300/place)
	--end)
	
	--if not success then
	--	warn(plr.Name .. " placement data not loaded")
	--end
	
	
	--Checks if the player should go onto the winners podium
	if place and (place <= 3) then
		
		local top3List = {
			[1] = firstPart,
			[2] = secondPart,
			[3] = thirdPart
		}
		
		--Variables
		local podiumPart = top3List[place]
		local positionPart = podiumPart:WaitForChild("PositionPart")
		local nameLabel = podiumPart:WaitForChild("SurfaceGui"):WaitForChild("NameLabel")
		
		--Get the player's character
		local character = plr.Character or plr.CharacterAdded:Wait()
		local newCharacter
		
		if character then
			character.Archivable = true
			newCharacter = character:Clone()
			character.Archivable = false
			newCharacter.Parent = podiumPart
		end
		
		local humanoid
		local humanoidRootPart
		if newCharacter then
			
			humanoidRootPart = newCharacter:FindFirstChild("HumanoidRootPart")
			humanoid = newCharacter:FindFirstChild("Humanoid")
			
			--Apply Character to podium
			humanoidRootPart.CFrame = positionPart.CFrame
			humanoidRootPart.Anchored = true
			nameLabel.Text = plr.Name
			nameLabel.Visible = true
			
			--Animate Character
			local danceAnimation = danceAnimList[math.random(1,3)]
			
			local animator = humanoid:WaitForChild("Animator")
			local danceTrack = animator:LoadAnimation(danceAnimation)
			danceTrack:Play()
			
		end
				
	end
	
end
Competitive Module
local module = {
	
	competitivePlacements = {},
	
	competitivePlacementsAmount = 0,
	
	displayProperties = {
		
		[1] = {"1st", Color3.fromRGB(255, 184, 70)},
		[2] = {"2nd", Color3.fromRGB(239, 239, 239)},
		[3] = {"3rd", Color3.fromRGB(197, 121, 70)},
		[4] = {"4th", Color3.fromRGB(113, 113, 113)},
		[5] = {"5th", Color3.fromRGB(113, 113, 113)},
		[6] = {"6th", Color3.fromRGB(113, 113, 113)}
		
	}
	
}

function module.competitivePlacementsManagement(plr)
	
	module.competitivePlacementsAmount += 1
	
	module.competitivePlacements[plr.UserId] = module.competitivePlacementsAmount
	
	print(module.competitivePlacements)
	
	local place = module.competitivePlacements[plr.UserId]
	local reward = math.ceil(300/place)
	
	return reward
	
end



function module.competitivePlacementsGuiDisplay(isFirstIntermission)

	--Variables
	local WinGuiDisplayEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("WinDisplayEvents"):WaitForChild("WinGuiDisplay")
	local WinGuiVisibleEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("WinDisplayEvents"):WaitForChild("WinGuiVisible")

	--local PlacementFrame = plr:WaitForChild("PlayerGui"):WaitForChild("WinnersGui"):WaitForChild("PlacementFrame")

	if not isFirstIntermission then
		
		local keyset={}
		local n=0

		for k,v in pairs(module.competitivePlacements) do
			n=n+1
			keyset[n]=k
		end
		
		table.sort(keyset)
		
		print("-------------")
		print(keyset)
		print("-------------")
		WinGuiDisplayEvent:FireAllClients(module.competitivePlacements, module.displayProperties, keyset)

		WinGuiVisibleEvent:FireAllClients()

	else	
		print("First Intermission, no placements list")
	end
	
end


return module

When player enters the portal that makes you win (Part of a different module script which gets ran in a Server Script)
--Rewarding Players for Winning
local reward = competiviveModule.competitivePlacementsManagement(player)

--Just know that there is a lot of unrelated code that I'm just not showing, so just know that this variable is declared when the player reaches the end portal.

Thank you all for helping :happy2:

If the elemements are in a frame with a uilistlayout, try setting their LayoutOrder property.

There is only layout order and name. This doesn’t help at all, because I already have it at layout order.

I think I found a pattern. I tested the game with my friend and his username starts with “e” and mine starts with “v”, so alphabetically my friend is first every time, no matter if I win first or he wins first. Does table.sort() by default use alphabetical order to sort?

I think it might have something to do with table.sort() although I don’t think it’s alphabetical since I don’t think that is the default option for sorting. Does anyone know if the table.sort() is the problem, or is it something else?

I’m still confused, does anyone know why the Gui won’t order right?