I don’t know how to add variables into a table each time a for loop runs, without overwriting the previous value. If anyone could help i’d really appreciate it.
local function getUserGeneratedTShirtsRecursive(username, tshirts, cursor)
tshirts = tshirts or {}
cursor = cursor or ""
local requestUrl = baseUrl:format(username, cursor)
local success, result = pcall(function()
return http:GetAsync(requestUrl)
end)
if success then
if result then
local success2, result2 = pcall(function()
return http:JSONDecode(result)
end)
if success2 then
if result2 then
for _, tshirt in ipairs(result2.data) do
table.insert(tshirts, tshirt.id)
end
cursor = result2.nextPageCursor
if cursor then
return getUserGeneratedTShirtsRecursive(username, tshirts, cursor)
else
return tshirts
end
end
else
warn(result)
end
end
else
warn(result)
end
end
winner.PlayerGui.Donation.Enabled = true
local username = winner.Name
local userTShirts = getUserGeneratedTShirtsRecursive(username)
local num = (#userTShirts)
print(table.concat(userTShirts, " ")) --1031862 1036727 1031864
game.ReplicatedStorage.RemoteEvent:FireClient(winner)
winner.PlayerGui.Donation.Frame.TextLabel.Text = "Donate to".. winner.Name
local table1 = {}
for i = num, 0, -1 do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
local assetPrice = Asset.PriceInRobux
local assetId = Asset.AssetId
-- Some how I need to return the assetPrice and assetId each time it runs and add to a table
end
table.sort(table1, function(a, b)
return a < b
end)
for i, item in pairs(userTShirts) do
local newButton = workspace.DonateButton:Clone()
newButton.Text = table1[i] .. "R$"
local id = Instance.new("IntValue", newButton)
id.Value = item.id
newButton.Parent = winner.PlayerGui.Donation.Frame.itemsScroller
end
You can add items to a table using the table.insert() function. This function takes two arguments: the table you want to add to, and the value you want to add.
local table1 = {}
for i = num, 0, -1 do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
local assetPrice = Asset.PriceInRobux
local assetId = Asset.AssetId
-- Create a new table for each item with the price and id
local item = {price = assetPrice, id = assetId}
-- Insert the item into the table
table.insert(table1, item)
end
Now, each item in the table is itself a table with two fields: ‘price’ and ‘id’. You can access these fields like this:
for i, item in ipairs(table1) do
print("Item " .. i .. " has price " .. item.price .. " and id " .. item.id)
end
Just a quick question, any idea why I get this error message?
’ Argument 1 missing or nil’
I’ll highlight the line of code where the issue is:
local function getUserGeneratedTShirtsRecursive(username, tshirts, cursor)
tshirts = tshirts or {}
cursor = cursor or ""
local requestUrl = baseUrl:format(username, cursor)
local success, result = pcall(function()
return http:GetAsync(requestUrl)
end)
if success then
if result then
local success2, result2 = pcall(function()
return http:JSONDecode(result)
end)
if success2 then
if result2 then
for _, tshirt in ipairs(result2.data) do
table.insert(tshirts, tshirt.id)
end
cursor = result2.nextPageCursor
if cursor then
return getUserGeneratedTShirtsRecursive(username, tshirts, cursor)
else
return tshirts
end
end
else
warn(result)
end
end
else
warn(result)
end
end
winner.PlayerGui.Donation.Enabled = true
local username = winner.Name
local userTShirts = getUserGeneratedTShirtsRecursive(username)
local num = (#userTShirts)
print(table.concat(userTShirts, " ")) --1031862 1036727 1031864
game.ReplicatedStorage.RemoteEvent:FireClient(winner)
winner.PlayerGui.Donation.Frame.TextLabel.Text = "Donate to".. winner.Name
local table1 = {}
for i = num, 0, -1 do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i]) -- THIS IS THE ISSUE <-------------
local assetPrice = Asset.PriceInRobux
local assetId = Asset.AssetId
-- Create a new table for each item with the price and id
local item = {price = assetPrice, id = assetId}
-- Insert the item into the table
table.insert(table1, item)
end
table.sort(table1, function(a, b)
return a < b
end)
for i, item in ipairs(table1) do
local newButton = workspace.DonateButton:Clone()
newButton.Text = table1[i] .. "R$"
local id = Instance.new("IntValue", newButton)
id.Value = item.id
newButton.Parent = winner.PlayerGui.Donation.Frame.itemsScroller
end
Thanks, that fixed that error, but now i’m getting this one
‘ServerScriptService.Arena1Script:226: attempt to compare table < table’
Really appreciate your help btw
You can only compare tables using the equality (==) or inequality (~=) operators.
Below is a corrected version of your script, assuming you want to compare the AssetId of the T-Shirts.
local MarketPlaceService = game:GetService("MarketplaceService")
local userTShirts = {} -- assuming this is a list of T-Shirt asset Ids
local num = #userTShirts
for i = num, 1, -1 do
local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
-- compare the AssetId of the current T-Shirt with a specific id
if Asset.AssetId == someSpecificId then
-- do whatever here idk
end
end
oh, my brain isn’t working sorry. Down we go the debugging path good ol print
for i, item in ipairs(table1 or {}) do
if item then
print("Item " .. i .. " has price " .. item.price .. " and id " .. item.id)
else
print("Item " .. i .. " is missing")
end
end
Item 1 has price 101 and id 1031862 - Server - Arena1Script:228
15:56:44.757 ServerScriptService.Arena1Script:228: attempt to concatenate nil with string
for i, item in ipairs(table1 or {}) do
if item then
local price = item.price or "unknown"
local id = item.id or "unknown"
print("Item " .. i .. " has price " .. price .. " and id " .. id)
else
print("Item " .. i .. " is missing")
end
end
Item 1 has price 101 and id 1031862 - Server - Arena1Script:230
16:02:05.109 Item 2 has price unknown and id 1036727 - Server - Arena1Script:230
16:02:05.109 Item 3 has price 178 and id 1031864 - Server - Arena1Script:230
16:02:05.111 ServerScriptService.Arena1Script:237: attempt to compare table < table
^^ that error is the compare line where i tried to compare the prices
Seems like the item at index 2 in your table doesn’t have a price associated with it. You should check your data source to ensure that all items have a price. For now, you can add a default value for price when it’s unknown.
for i, item in ipairs(table1 or {}) do
if item then
local price = item.price or "default price"
local id = item.id or "unknown"
print("Item " .. i .. " has price " .. price .. " and id " .. id)
else
print("Item " .. i .. " is missing")
end
end
Sure, you can use the table.sort() function to sort your table based on the price.
table.sort(table1, function(a, b)
return a.price < b.price
end)
for i, item in ipairs(table1 or {}) do
if item then
local price = item.price or "default price"
local id = item.id or "unknown"
print("Item " .. i .. " has price " .. price .. " and id " .. id)
else
print("Item " .. i .. " is missing")
end
end