Invalid order function for sorting

Hi, i sometimes get the error “Invalid order function for sorting” in this function but idk why:

local function loadCategoryItems()
	local categoryItemsFolder = game.ReplicatedStorage.Inventory[lastCategoryObj.Name]
	local itemsInCategory = categoryItemsFolder:GetChildren()
	local ScrollingFrame = inventoryHud.InventoryScrollingFrame
	
	ScrollingFrame:ClearAllChildren()
	
	table.sort(itemsInCategory, --Start
		function(a, b)
			if game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[a.Name]:GetAttribute("Id") > game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[b.Name]:GetAttribute("Id") then
				return false
			elseif game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[a.Name]:GetAttribute("Id") < game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[b.Name]:GetAttribute("Id") then
				return true
			elseif game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[a.Name]:GetAttribute("Id") == game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[b.Name]:GetAttribute("Id") then
				return true
			end
		end
	) -- End
	
	local ItemsOnLine = 3
	
	local Items = #itemsInCategory
	local Line = 0
	
	local x = Items
	while x >= 3 do
		x -= 3
		Line += 1
	end
	
	if x > 0 then
		x = 0
		Line += 1
	end
	
	local distanceFromTop = 0.015
	local distanceFromSide = 0.015
	
	local sizeOfShit = inventoryHud.Example.ItemName.Size
	local convertedsizeOfShit = {OffsetToScale(sizeOfShit.X.Offset), OffsetToScale(sizeOfShit.Y.Offset)}
	
	local expectedYLenght = (Line+1)*(convertedsizeOfShit[2]+distanceFromTop)
	
	ScrollingFrame.CanvasSize = UDim2.new(ScrollingFrame.CanvasSize.X.Scale, ScrollingFrame.CanvasSize.X.Offset, expectedYLenght, ScrollingFrame.CanvasSize.Y.Offset)
	
	local sizeOfFrame = {OffsetToScale(ScrollingFrame.Size.X.Offset, true), ScrollingFrame.CanvasSize.Y.Scale}
	local distanceFromSideToFrame = UDim2.new(sizeOfFrame[1], 0, (expectedYLenght/Line), 0)
	
	listing = true
	
	for i = 1, Line do
		for y = 1, ItemsOnLine do
			if activated then
				listing = false
				activated = false
				break;
			end
			
			if x < Items then
				x += 1
				local frame = inventoryHud.Example.ItemName:Clone()
				frame.Parent = ScrollingFrame
				frame.Visible = true
				frame.Position = UDim2.new((distanceFromSide+(distanceFromSideToFrame.X.Scale*(y-1))), 0, (distanceFromTop+(distanceFromSideToFrame.Y.Scale*(i-1))), 0)
				frame.ViewportFrame.Preview:Destroy()
				
				local preview = game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[itemsInCategory[x].Name]:Clone()
				preview.Parent = frame.ViewportFrame
				preview.Name = "Preview"
				preview.Position = Vector3.new(0,0,0)
				
				local camera = Instance.new("Camera")
				camera.Parent = preview.Parent
				camera.CFrame = CFrame.new(Vector3.new(0, 0, 2), Vector3.new(0,0,0))
				
				frame.ItemName.Text = itemsInCategory[x].Name
				frame.Name = itemsInCategory[x].Name
				
				wait(0.001)
			end
		end
	end
	
	listing = false
end

you need to have an else in your sorting function i believe

Try doing:

table.sort(itemsInCategory,
    function(a,b)
        return game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[a.Name]:GetAttribute("Id") <= game.ReplicatedStorage.Items[lastCategoryObj.Name].Items[b.Name]:GetAttribute("Id")
    end 
)

I’m not entirely sure if it’ll work as I haven’t tested it but let me know if it doesn’t work

Thank you both but i believe i did those 2 things before using the last one, the one from the first post. I’ll try again because why not.

Edit: Apparently what @typedwaves just suggested works, i tried 4 times to rejoin and press the button and i see i doesn’t throw any error anymore.

Edit2: Nevermind, after a couple of tests i see it still does throw the same error but not all the time

1 Like