Teams not displaying correct order when use GetChildren()

This is horrifying. This is whats inside my teams folder
Screenshot 2024-08-24 163658
Looks good right? Spectating first, then the Alive team. But no…

Sometimes when I print the table, the whole order is swapped (on a localscript) This doesn’t happen like back and forth, it’s more like the twisted mind of Roblox decides to choose one of the order and stick with it for the rest of the game when I join it.

This is what happens. I want it to be 1, spectating then 2 is alive… Why does it swap sometimes?? In the teams folder the hierarchy is clearly defined.
Screenshot 2024-08-24 163506
Screenshot 2024-08-24 163555
By the way all I’m printing is print(game.Teams:GetChildren()) I’ve never worked with teams before, so I have absolutely no idea on how to face this problem

Am I an idiot or is Roblox up to something? Am I missing something???

The Explorer view is not indicative of the actual order. The order is defined by which Instance was added in first.

When running the code on the client, it is entirely possible that the Spectating team loaded in before the Alive team, vice versa.

You can manually sort the array via table.sort. Examples:

-- ascending alphabetically
local teams = game:GetService("Teams"):GetChildren()
table.sort(teams, function(a, b)
    return a.Name < b.Name
end)

-- descending alphabetically
local teams = game:GetService("Teams"):GetChildren()
table.sort(teams, function(a, b)
    return a.Name > b.Name
end)

I see, and I have tried using table.sort
But I don’t want alphabetical teams, I want a specific order that I am not able to achieve with Roblox’s tomfoolery.

I have also tried creating the teams in a server script, where the spectating one is created first then the alive team, but on a local script it’s the same result, it swaps up in the table sometimes.

Is there any way to fetch my desired order in a table? Do I have to manually do it?

Yeah. For example, you can use attributes to manually define the order. In this example, you can add a number attribute called “Priority”.

-- descending from priority (higher first, lower last)
local teams = game:GetService("Teams"):GetChildren()
table.sort(teams, function(a, b)
    local priorityA = a:GetAttribute("Priority") or 0
    local priorityB = b:GetAttribute("Priority") or 0

    return priorityA > priorityB
end)
1 Like

Thanks, didn’t think of that

I hate you Roblox

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