Sort cloned frames help!

So, i have a script which detects the existing pets and clone a frame for each pet, here is the script:

local Lighting = game:GetService("Lighting")
local ShopItems = Lighting.Pets
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShopData = require(ReplicatedStorage.ShopData)
local PetsData = ShopData["Pets"]

local PrefabItem = ReplicatedStorage.ShopStuff:FindFirstChild("SlotLayout")

local Frame = script.Parent.PetsFrame

for _, Pet in pairs(ShopItems:GetChildren()) do
  local Item = PrefabItem:Clone()
    if (PetsData[Pet.Name]) then --//CHANGED LINE
        local PetData = (PetsData[Pet.Name]) --//CHANGED LINE
        Item.Parent = Frame
        Item.Name = Pet.Name
        Item.Info.Text = PetData["Info"]
        Item.Cost.Value = PetData["Cost"]
        Item.ImageShow.Image = "rbxassetid://" .. PetData["ImageId"]

    Item.ImageButton.MouseButton1Down:Connect(function()
		local Button = Item.Name..""
		local HasBought = Item:FindFirstChild("HasBought")
		game.ReplicatedStorage.PetAdd:FireServer(Button)
		print(player.Name.." sent a request to the server to buy something!")
    end)
	print("ye")
	wait(0.2)
	end
	print("done")
end

Well, the pets are cloning in alphabetical order, and i want to sort them for the price:

Item.Cost.Value = PetData["Cost"]

But i don’t know how i will achieve that. I really need this, and i hope you respond me :+1: Thanks for helping.

If you need more information then i can give it.

if you’re using a UIGridLayout you can have its SortOrder be LayoutOrder then make the LayoutOrder of each item its cost.

    Item.LayoutOrder = PetData["Cost"]

keep in mind that the layoutorder caps out at 2,147,483,647 so if you have pets more expensive than that this method won’t work

I did this:

local Lighting = game:GetService("Lighting")
local ShopItems = Lighting.Pets
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShopData = require(ReplicatedStorage.ShopData)
local PetsData = ShopData["Pets"]

local PrefabItem = ReplicatedStorage.ShopStuff:FindFirstChild("SlotLayout")

local Frame = script.Parent.PetsFrame

for _, Pet in pairs(ShopItems:GetChildren()) do
  local Item = PrefabItem:Clone()
    if (PetsData[Pet.Name]) then

	local UIGrid = Frame.UIGridLayout --//Changed Line
        UIGrid.LayoutOrder = PetData["Cost"] --//Changed Line

        local PetData = (PetsData[Pet.Name])
        Item.Parent = Frame
        Item.Name = Pet.Name
        Item.Info.Text = PetData["Info"]
        Item.Cost.Value = PetData["Cost"]
        Item.ImageShow.Image = "rbxassetid://" .. PetData["ImageId"]

    Item.ImageButton.MouseButton1Down:Connect(function()
		local Button = Item.Name..""
		local HasBought = Item:FindFirstChild("HasBought")
		game.ReplicatedStorage.PetAdd:FireServer(Button)
		print(player.Name.." sent a request to the server to buy something!")
    end)
	print("ye")
	wait(0.2)
	end
	print("done")
end

		

But that didn’t worked.

I can explain you every local:
-Frame = Frame which contains every frame for each pet
-Pet = Pet selected
-Item = Frame for each Pet
-PetData = Info from a ModuleScript
-PetData[“Cost”] = Variable which contains the value.

Did i made something wrong? I hope your respond :slight_smile:

sorry. i meant change the LayoutOrder of Item to its cost, and the SortOrder of the UIGridLayout to LayoutOrder. SortOrder is just a property that determines if the grid is going to be sorted by name or LayoutOrder. it should look like this:

UIGrid.SortOrder = "LayoutOrder"

and then add this line in:

Item.LayoutOrder = PetData["Cost"]

the UIGrid part should also be moved out of the loop since it only needs to be set once.

1 Like