My script isn't working

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
1 Like

Thank you so much :slight_smile:

dfhfdghfgh

1 Like

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

Its the 'local Asset = ’ bit

fgdfhdfhfdg

So you’ve fixed it or still need assistance?

1 Like

Still need help I know where the issue is I just dont know how to fix it

Alright.

In the last example, the error “Argument 1 missing or nil” is likely occurring at this line:

local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])

A possible fix would be to adjust the range of the loop to start from num and go down to 1:

for i = num, 1, -1 do
    local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
end
1 Like

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
1 Like

Thanks but I wanted to compare the prices of all the items and order them in the table cheapest to most expensive

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
1 Like

Item 1 has price 101 and id 1031862 - Server - Arena1Script:228
15:56:44.757 ServerScriptService.Arena1Script:228: attempt to concatenate nil with string

This is tricky.

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

try this

1 Like

Seems to have worked better

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
1 Like

Thank you very much for your help, any idea how I could sort the table from lowest price to highest?

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
1 Like

Thank you so much for your help :slight_smile:

1 Like

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