I’m using this script to sort the inventory and create the GUI buttons, basing on pet power to sort the “most powerful” pets at the top and go to the “weakest”.
local l__LocalPlayer__1 = game.Players.LocalPlayer;
local l__Pets__2 = l__LocalPlayer__1:WaitForChild("Pets");
local l__ReplicatedStorage__1 = game.ReplicatedStorage;
function getLevel(p1)
local v3 = 0;
local l__Value__4 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
local v5 = 0 - 1;
while true do
if 100 * v5 + v3 <= p1 then
if v5 ~= l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value then
else
return v5;
end;
if p1 < 100 * v5 + v3 + (100 + 25 * v5) then
return v5;
end;
end;
v3 = v3 + v5 * 25;
if 0 <= 1 then
if v5 < l__Value__4 then
else
break;
end;
elseif l__Value__4 < v5 then
else
break;
end;
v5 = v5 + 1;
end;
end;
function GetFolderFromPetID(p2)
local v6, v7, v8 = pairs(l__LocalPlayer__1.Pets:GetChildren());
while true do
local v9, v10 = v6(v7, v8);
if v9 then
else
break;
end;
v8 = v9;
if v10.PetID.Value == p2 then
return v10;
end;
end;
return nil;
end;
function getLayoutOrder(p3)
local v11 = GetFolderFromPetID(p3);
local l__Value__12 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(v11.Name).Settings.Multiplier1.Value;
local l__Value__13 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
local v14 = getLevel(v11.TotalXP.Value);
local v15 = {};
local v16 = 0;
local v17, v18, v19 = pairs(l__ReplicatedStorage__1.Pets.Models:GetChildren());
while true do
local v20, v21 = v17(v18, v19);
if v20 then
else
break;
end;
v19 = v20;
v15[#v15 + 1] = v21;
end;
table.sort(v15, function(p4, p5)
return p4:GetFullName() < p5:GetFullName();
end);
local v22, v23, v24 = pairs(v15);
while true do
local v25 = nil;
local v26 = nil;
v26, v25 = v22(v23, v24);
if v26 then
else
break;
end;
v24 = v26;
if v11.Equipped.Value == true then
if v11.Name == v25.Name then
return l__Value__13 * #v15 * l__Value__12 + v16 * l__Value__13 + (l__Value__13 - v14);
end;
elseif v11.Name == v25.Name then
return #v15 * #l__ReplicatedStorage__1.Pets.Rarities:GetChildren() * l__Value__13 + l__Value__13 * #v15 * l__Value__12 + v16 * l__Value__13 + (l__Value__13 - v14);
end;
v16 = v16 + 1;
end;
end;
local l__UIelements__2 = l__LocalPlayer__1.PlayerGui:WaitForChild("UIelements");
local l__Inventory__3 = script.Parent.MainFrame.Inventory;
function newUI(p6)
local v27 = l__UIelements__2.PetTemplate:Clone();
local v28 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(p6.Name);
local v29 = v28:FindFirstChild(p6:WaitForChild("Type").Value):Clone();
local l__Value__30 = l__ReplicatedStorage__1.Pets.Rarities:FindFirstChild(v28.Settings.Rarity.Value).Color.Value;
local v31 = Instance.new("Camera", v27.PetView);
local l__Position__32 = v29.PrimaryPart.Position;
v27.PetID.Value = p6:WaitForChild("PetID").Value;
v27.Name = p6.Name;
v27.Gradient.UIGradient.Color = ColorSequence.new(l__Value__30, (Color3.new(l__Value__30.R - 0.5882352941176471, l__Value__30.G - 0.5882352941176471, l__Value__30.B - 0.5882352941176471)));
v27.PetView.CurrentCamera = v31;
v29.Parent = v27.PetView;
v31.CFrame = CFrame.new(Vector3.new(l__Position__32.X + 2.25, l__Position__32.Y, l__Position__32.Z + 1), l__Position__32);
v27.Parent = l__Inventory__3;
l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
if p6.Equipped.Value == true then
v27.EquipMarker.Visible = true;
else
v27.EquipMarker.Visible = false;
end;
p6:WaitForChild("TotalXP"):GetPropertyChangedSignal("Value"):Connect(function()
v27.LevelLabel.Text = "Lvl. " .. getLevel(p6:WaitForChild("TotalXP").Value);
end);
local v33, v34, v35 = pairs(l__Inventory__3:GetChildren());
while true do
local v36, v37 = v33(v34, v35);
if v36 then
else
break;
end;
v35 = v36;
if not v37:IsA("UIGridLayout") then
v37.LayoutOrder = getLayoutOrder(v37.PetID.Value);
end;
end;
end;
for v38, v39 in pairs(l__Pets__2:GetChildren()) do
newUI(v39);
end;
local l__TextLabel__4 = script.Parent.MainFrame.StorageDisplay.TextLabel;
local l__Data__5 = l__LocalPlayer__1:WaitForChild("Data");
local l__TextLabel__6 = script.Parent.MainFrame.EquippedDisplay.TextLabel;
l__Pets__2.ChildAdded:Connect(function(p7)
local v40 = 0;
for v41, v42 in pairs(l__Pets__2:GetChildren()) do
if v42:WaitForChild("Equipped").Value == true then
v40 = v40 + 1;
end;
end;
l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
l__TextLabel__6.Text = v40 .. "/" .. l__Data__5.MaxEquip.Value;
newUI(p7);
end);
l__Pets__2.ChildRemoved:Connect(function(p8)
for v43, v44 in pairs(l__Inventory__3:GetChildren()) do
if not v44:IsA("UIGridLayout") and v44.PetID.Value == p8.PetID.Value then
v44:Destroy();
end;
end;
local v45 = 0;
for v46, v47 in pairs(l__Pets__2:GetChildren()) do
if v47.Equipped.Value == true then
v45 = v45 + 1;
end;
end;
l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
l__TextLabel__6.Text = v45 .. "/" .. l__Data__5.MaxEquip.Value;
end);
l__Inventory__3.ChildAdded:Connect(function()
l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
l__Inventory__3.ChildRemoved:Connect(function()
l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
local v48 = 0;
for v49, v50 in pairs(l__Pets__2:GetChildren()) do
if v50.Equipped.Value == true then
v48 = v48 + 1;
end;
end;
l__Data__5.MaxStorage:GetPropertyChangedSignal("Value"):Connect(function()
l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
end);
l__Data__5.MaxEquip:GetPropertyChangedSignal("Value"):Connect(function()
l__TextLabel__6.Text = v48 .. "/" .. l__Data__5.MaxEquip.Value;
end);
l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
l__TextLabel__6.Text = v48 .. "/" .. l__Data__5.MaxEquip.Value;
It gets the values to sort and make here
function getLayoutOrder(p3)
local v11 = GetFolderFromPetID(p3);
local l__Value__12 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(v11.Name).Settings.Multiplier1.Value;
local l__Value__13 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
local v14 = getLevel(v11.TotalXP.Value);
local v15 = {};
local v16 = 0;
local v17, v18, v19 = pairs(l__ReplicatedStorage__1.Pets.Models:GetChildren());
What it currently does is use the pet power but it sorts them the complete opposite way, weakest to strongest and puts the equipped pets at the bottom which it shouldn’t do.
How would I be able to sort of flip the inventory sorting so its from strongest to weakest and the equipped pets stay at the top?