When the player clicks a button, it will generate a math.random() number. If the math.random number is a valid place and the creation date is not the same as the updated date, it will teleport all players to that place. If the random number is not a place, or it has the creation date set as the same as the updated date, it will repeat the math.random number, then it checks again. But, sometimes, this error comes up:
MarketplaceService:getProductInfo() failed because HTTP 400 (Bad Request)
This is the piece of code:
local games local name repeat games = math.random(1000,99999999) until market:GetProductInfo(games).AssetTypeId == 9 and market:GetProductInfo(games).Created:gsub("T.*", "") ~= market:GetProductInfo(games).Updated:gsub("T.*", "") print("Valid game found!) end
Why is this error happening and how can I stop it? Much appreciated.
Problem
Occasionally, you receive HTTP 400 (Bad Request) when using MarketplaceService:GetProductInfo(id).
I determined that the reason for your issue was an invalid asset id. When the error is received, I had a script output the id and then manually searched for the asset and the webpage redirected me to this page: https://www.roblox.com/request-error?code=404.
Solution:
As @MarkedTomato said, it is good practice to surround HTTP calls with pcalls.
pcall information
A pcall will always return 2 values. The first value is if the code in the pcall ran successfully (boolean). The second value will either be the return value of the code in the pcall, or the error that was thrown, in string format.
Code
local market = game:GetService("MarketplaceService") local games local name local productInfo repeat games = math.random(1000,99999999) wait() -- added a wait to prevent exhaustion local success,returnValue = pcall(function() productInfo = market:GetProductInfo(games) -- prevents unnecessary calls to GetProductInfo end) if not success then if returnValue == "MarketplaceService:getProductInfo() failed because HTTP 400 (Bad Request)" then -- if the asset ID is invalid print("Invalid ID: " .. games) end end until productInfo.AssetTypeId == 9 and productInfo.Created:gsub("T.*", "") ~= productInfo.Updated:gsub("T.*", "") print("Valid game found!")
Conclusion
If you have any more questions, or experience any errors, feel free to contact me via my Discord server.