Attempt to yield across metamethod/C-call boundary, when sorting a table

i’m trying to sort a table based on information that needs to be fetched individually for each product (table value).

it keeps erroring when i do this, and i want my code to be modular (so i dont need to put a price in a table in ReplicatedStorage or something, it just does it based on the actual price in mktplaceservice)

it works, just not the table sort.


-- a[3]/b[3] = the ID of the product/pass
-- the [2] (2nd arg) = the returned Price value from the table
		local function SortFunction(a, b)
			local aPrice = ReplicatedStorage.Events.ReturnPrice:InvokeServer(a[3])[2]
			local bPrice = ReplicatedStorage.Events.ReturnPrice:InvokeServer(b[3])[2]
			return aPrice < bPrice
		end

		table.sort(List, SortFunction)

The comparator passed to sort is not allowed to yield, which calling InvokeServer does.

I understand, is there a way around this? Such as making a table above it, with the ID and the corresponding price? then calling the table from the function?

Yeah you could get all the prices beforehand, put them in a table as just numbers, and sort that.

Got it.

Got it working with the code below:

		local IDPriceTable = {}
		
		for _, Item in pairs(List) do
			IDPriceTable[Item[3]] = ReplicatedStorage.Events.ReturnPrice:InvokeServer(Item[3])
		end
		
		local function SortFunction(a, b)
			local aPrice = IDPriceTable[a[3]][2]
			local bPrice = IDPriceTable[b[3]][2]
			return aPrice < bPrice
		end

		table.sort(List, SortFunction)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.