local r=game.ReplicatedStorage function getLevel(f)local a=0 local m=r.Pets.Settings.MaxPetLevel.Value local g=-1 while true do if 100*g+a<=f then if g~=r.Pets.Settings.MaxPetLevel.Value then else return g end if f<(100*g+a)+(100+25*g)then return g end end a=a+g*25 if true then if g<m then else break end elseif m<g then else break end g=g+1 end end local f=game.Players.LocalPlayer function GetFolderFromPetID(r)for f,a in pairs(f.Pets:GetChildren())do if a.PetID.Value==r then return a end end return nil end function getLayoutOrder(f)local a=GetFolderFromPetID(f)local m=(r.Pets.Rarities:FindFirstChild((r.Pets.Models:FindFirstChild(a.Name)).Settings.Rarity.Value)).Order.Value local g=r.Pets.Settings.MaxPetLevel.Value local j=getLevel(a.TotalXP.Value)local n={}local Y=0 for r,f in pairs(r.Pets.Models:GetChildren())do n[#n+1]=f end table.sort(n,function(r,f)return r:GetFullName()<f:GetFullName()end)for f,d in pairs(n)do if a.Equipped.Value==true then if a.Name==d.Name then return((g*#n)*(#r.Pets.Rarities:GetChildren()-m)+Y*g)+(g-j)end elseif a.Name==d.Name then return(((#n*#r.Pets.Rarities:GetChildren())*g+(g*#n)*(#r.Pets.Rarities:GetChildren()-m))+Y*g)+(g-j)end Y=Y+1 end end local a=script.Parent.Parent.Parent.Parent.MainFrame.Inventory script.Parent.MouseButton1Click:Connect(function()local f,m=r.RemoteEvents.PetActionRequest:InvokeServer("\068\101\108\101\116\101",{PetID=script.Parent.Parent.Parent.PetID.Value})if f~="\083\117\099\099\101\115\115"then if f=="\069\114\114\111\114"then print(m)end return end script.Parent.Parent.Visible=false for r,f in pairs(a:GetChildren())do if not f:IsA("\085\073\071\114\105\100\076\097\121\111\117\116")then f.LayoutOrder=getLayoutOrder(f.PetID.Value)end end end)


local l__ReplicatedStorage__1 = game.ReplicatedStorage;
function getLevel(p1)
	local v1 = 0;
	local l__Value__2 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
	local v3 = 0 - 1;
	while true do
		if 100 * v3 + v1 <= p1 then
			if v3 ~= l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value then

			else
				return v3;
			end;
			if p1 < 100 * v3 + v1 + (100 + 25 * v3) then
				return v3;
			end;
		end;
		v1 = v1 + v3 * 25;
		if 0 <= 1 then
			if v3 < l__Value__2 then

			else
				break;
			end;
		elseif l__Value__2 < v3 then

		else
			break;
		end;
		v3 = v3 + 1;	
	end;
end;
local l__LocalPlayer__2 = game.Players.LocalPlayer;
function GetFolderFromPetID(p2)
	for v7, v8 in pairs(l__LocalPlayer__2.Pets:GetChildren()) do
		if v8.PetID.Value == p2 then
			return v8;
		end;	
	end;
	return nil;
end;
function getLayoutOrder(p3)
	local v9 = GetFolderFromPetID(p3);
	local l__Value__10 = l__ReplicatedStorage__1.Pets.Rarities:FindFirstChild(l__ReplicatedStorage__1.Pets.Models:FindFirstChild(v9.Name).Settings.Rarity.Value).Order.Value;
	local l__Value__11 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
	local v12 = getLevel(v9.TotalXP.Value);
	local v13 = {};
	local v14 = 0;
	for v18, v19 in pairs(l__ReplicatedStorage__1.Pets.Models:GetChildren()) do
		v13[#v13 + 1] = v19;	
	end;
	table.sort(v13, function(p4, p5)
		return p4:GetFullName() < p5:GetFullName();
	end);
	for v24, v23 in pairs(v13) do
		if v9.Equipped.Value == true then
			if v9.Name == v23.Name then
				return l__Value__11 * #v13 * (#l__ReplicatedStorage__1.Pets.Rarities:GetChildren() - l__Value__10) + v14 * l__Value__11 + (l__Value__11 - v12);
			end;
		elseif v9.Name == v23.Name then
			return #v13 * #l__ReplicatedStorage__1.Pets.Rarities:GetChildren() * l__Value__11 + l__Value__11 * #v13 * (#l__ReplicatedStorage__1.Pets.Rarities:GetChildren() - l__Value__10) + v14 * l__Value__11 + (l__Value__11 - v12);
		end;
		v14 = v14 + 1;	
	end;
end;
local l__Inventory__3 = script.Parent.Parent.Parent.Parent.MainFrame.Inventory;
script.Parent.MouseButton1Click:Connect(function()
	local v25, v26 = l__ReplicatedStorage__1.RemoteEvents.PetActionRequest:InvokeServer("Delete", {
		PetID = script.Parent.Parent.Parent.PetID.Value
	});
	if v25 ~= "Success" then
		if v25 == "Error" then
			print(v26);
		end;
		return;
	end;
	script.Parent.Parent.Visible = false;
	for v27, v28 in pairs(l__Inventory__3:GetChildren()) do
		if not v28:IsA("UIGridLayout") then
			v28.LayoutOrder = getLayoutOrder(v28.PetID.Value);
		end;
	end;
end);
