Looping in the wrong order

So, i made this script for a Pokémon like game that me and a friend are making, this script is a test for a party system. I made 6 test “Pokémons” (that we’re calling “Roy” or “Roies”), when i click one of them, they go to folder, the script should see what Roies are in this folder and change the Party UI. It is working, but in the wrong order.

Here’s the Party UI:


Captura de tela 2025-01-24 132134

When i have 1 Roy, it should appear in Roy00, when i have 2 Roies, it shold appear in Roy 00 and Roy01, and so go on, but this is happening:
robloxapp-20250124-1322464.wmv (591.0 KB)

This is the script that update the Party UI:

local function UpdateParty()
	local Party = game.Players.LocalPlayer:FindFirstChild("Party")
	if Party then
		local PartyChildren = Party:GetChildren()
		if #PartyChildren <= 0 then
			return
		else
			for i, v in PartyChildren do
				local PartySlots = game.Players.LocalPlayer.PlayerGui.UI.Party:GetChildren()
				PartySlots[i].RoyName.Text = v.Name
				PartySlots[i].RoyColor.BackgroundColor3 = v.Color
				PartySlots[i].Visible = true
			end
		end
	end
end

local PlayerParty = game.Players.LocalPlayer:WaitForChild("Party")
if PlayerParty then
	PlayerParty.ChildAdded:Connect(UpdateParty)
end

I Haven’t tried anything to fix, because i really don’t know how i would fix it, any help would be appreciated! And if you need to see anything at all that could help to fix it, i will provide it. (And if it’s kinda raw, it’d becausde i’m focusing on getting the script to work first)

Instead of manually set frames you can set a placeholder frame and add a UIGridLayout inside your party frame. This way whenever PlayerParty changes it can clone / destroy the placeholders, set them accordingly and place them inside your party frame with the LayoutOrder set as the order.

The issue seems to be that the order in which the Roies are displayed in the UI does not match the expected order. This could happen if Party:GetChildren() doesn’t return its children in the order you expect (e.g., their insertion order isn’t guaranteed).

To fix this, you can explicitly sort PartyChildren based on the desired order before iterating through it. If you want the order to be based on something specific, like the order they were added, you can assign a numerical Index property to each Roy object when adding them to the Party folder, or rely on a naming convention.

Here’s a revised version of your script:

local function UpdateParty()
	local Party = game.Players.LocalPlayer:FindFirstChild("Party")
	if Party then
		local PartyChildren = Party:GetChildren()
		if #PartyChildren <= 0 then
			return
		else
			-- Sort PartyChildren by name or another custom property
			table.sort(PartyChildren, function(a, b)
				-- Sort by Name or another property like Index
				return a.Name < b.Name -- You can replace this with a custom comparison
			end)

			-- Update the Party UI
			local PartySlots = game.Players.LocalPlayer.PlayerGui.UI.Party:GetChildren()
			for i, v in ipairs(PartyChildren) do
				if PartySlots[i] then
					PartySlots[i].RoyName.Text = v.Name
					PartySlots[i].RoyColor.BackgroundColor3 = v.Color
					PartySlots[i].Visible = true
				end
			end
		end
	end
end

-- Connect UpdateParty to child changes
local PlayerParty = game.Players.LocalPlayer:WaitForChild("Party")
if PlayerParty then
	PlayerParty.ChildAdded:Connect(UpdateParty)
	PlayerParty.ChildRemoved:Connect(UpdateParty) -- Handle when a Roy is removed
end

Explanation of Fixes

  1. Sorting PartyChildren: The table.sort function ensures that the PartyChildren are ordered consistently (e.g., alphabetically by name or by a custom property like Index).
  2. Iterating Safely: The if PartySlots[i] check ensures that you don’t attempt to access an index that doesn’t exist in PartySlots.
  3. Handling Removal: Adding ChildRemoved:Connect(UpdateParty) ensures the UI updates when a Roy is removed from the Party folder.
  4. Focus on Order: Replace the sorting logic (return a.Name < b.Name) with a custom comparison if you have a specific property (e.g., Index) to determine the order.

Troubleshooting Tips

  1. Debugging: Add print statements inside the loop to verify the order of PartyChildren.
    for i, v in ipairs(PartyChildren) do
        print(i, v.Name)
    end
    
  2. Verify UI Slots: Ensure that the PartySlots match the number of Roies and are named consistently.
2 Likes

Roblox already has a built in index system that doesn’t require you to use ipairs, you can just do:

for i, v in PartyChildren:GetChildren() do

end

and it will loop in order.

This is why; local PartyChildren = Party:GetChildren() this will grab them as they were added to the frame not in the order you’re seeing. Adomy has the right approach. You could even pull them all out of that Party frame and add them one by one in the order you wish them to be read. It’s kind of odd. I would go with Adomy approach.

Thank you for suggesting the table.sort, it worked pretty well! Though i didn’t use it with the Roies, i used it with the PartySlots, but it still worked perfectly.

1 Like