Organize gui objects using grid layout in a script

I’m trying to make code that would parent gui objects to a scrolling frame and have them be organized by ui grid layout. It does parent the objects, but it doesn’t organize them properly, the same way they would be organized if i tossed them in manually in edit mode. How could this be fixed?

This is part of my code:

		for x, truck in ipairs(replicatedStorage:WaitForChild("Trucks").Vehicles:GetChildren()) do
			local newCard = replicatedStorage:WaitForChild("Gui").VehicleCard:Clone()
			newCard.Parent = gui.Frame.Scroll
			newCard.LayoutOrder = x
			newCard.Name = truck.Name
		end

image

2 Likes

You’re not getting their current layout order, only setting it to the current index (x) of your for loop. Makes sense? So you’ll have to manually set the layout order of each truck and then you can parent them in however you want to without even setting the layout order using script.

You could also script something that gives the truck GUI objects layout orders BASED on their price/speed for example :smiley:

Extra note: u don’t have to wait for child on replicated storage

2 Likes

That’s because :GetChildren() doesn’t guarantee you that instances will be in the same order as in explorer in edit mode.

To fix this you can:

  • Add an atrribute to every vehicle that will decide the order in gui and set LayoutOrder based on that attribute

or

  • If you want elements to be sorted alphabetically, just in UIGridLayout set SortOrder to Name
1 Like

I tried setting it to sort items alphabetically, but it still puts them in the same place. The code is placed in a localscript, could that be the reason why?

1 Like

Since I got no answer, as of right now I’m just using a for loop to organize the list instead of using uigridlayout. If you have an answer as to what might be causing my problems, please let me know. (i’m not going to make a card for each of the truck in-game)

1 Like