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)
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?
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)