I want to make a random game generator, (like Place Roulette), and I already have a good script but there is a problem when I try to get information on the game (with MarketplaceService.)
while not success do --2
if count >= 1 then
warn("retry count:", count, "\nError:", response)
wait(1)
end
success, response = pcall(function()
info = game:GetService("MarketplaceService"):GetProductInfo(gameId)
end)
count = count + 1
end
In that snippet of code, I have a variable that is supposed to be the information of a game (gameId is predetermined and info is also written earlier in the script). But when I try to use the info variable in an if statement, it gives me this error:
Workspace.Chooser:122: attempt to index nil with āDescriptionā
Iām not sure why this is happening. If you need any more information/code feel free to ask!
The gameId that you are using probably doesnt exist, thats why info variable is empty, and you are trying to get Description from an empty variable.
How are you getting the gameIdās in your āRouletteā?
Check if the gotten data inside info variable actually exist before trying to access its properties
Your error is that game:GetService("MarketplaceService") is nil. This is probably because you named a variable game, which overwrote the game global variable. But that would mean you created a game variable with a :GetService function.
Is your title actually the error you got or is your error Workspace.Chooser:122: attempt to index nil with āDescriptionā? This error isnāt in the code snippet you sent.
I can help you with that error if you send the snippet containing it.
I donāt have a variable named game. My error is Workspace.Chooser:122: attempt to index nil with description, I thought it was similar to having an index nil on :GetProductInfo()
Hereās the code
if info.Description ~= "This is your very first Roblox creation. Check it out, then make it your own with Roblox Studio!" and info.AssetTypeId == 9 and isGamePublic(gameId) == true and not string.find(info.Name,"s Place") and info.Created ~= info.Updated then
--print("yes!")
--2
workspace.part.SurfaceGui.ImageLabel.Image = "https://assetgame.roblox.com/Game/Tools/ThumbnailAsset.ashx?aid="..gameId.."&fmt=png&wd=420&ht=420"
workspace.Title.SurfaceGui.TextLabel.Text = info.Name
wait(30)
workspace.part.SurfaceGui.ImageLabel.Image = ""
workspace.Title.SurfaceGui.TextLabel.Text = "loading..."
end
Attempt to index nil means your code tries to get a property or function of nil like nil.Property or nil:Function() but with a variable equal to nil usually. Changing the error code to something else means the error says another object is nil that isnāt.
All the code you sent looks correct, Iām not sure why info is nil. info would be nil if the pcall never worked, but your loop has the correct exit condition. Idk if somewhere else in the code the info variable becomes nil.
I assume the if statement you sent checks if the info is valid, you could simply change it to if info and info.Description ~= ... which checks if info exists first. Ideally you would find where info becomes nil, but that solution would work too.
Info becomes nil at the start of the while loop. I changed it to be declared at the start of the scriot, but it now looks like the gameId never randomizes?
Actually- it doesnāt look like info variable itself is changing. GameID is changing, but it doesnāt look like the actual information about the game is changing
I would get a random game ID in the loop, just in case trying to get a specific game consistently returns errors.
Something like this:
local function isValidGame(info)
return (info) and (info.Description ~= "This is your very first Roblox creation. Check it out, then make it your own with Roblox Studio!" ) and (info.AssetTypeId == 9) and (isGamePublic(gameId) == true) and (not string.find(info.Name,"s Place")) and (info.Created ~= info.Updated)
end
local info = nil
while true do
local gameId = math.random(1,100000) -- You should probably change these numbers, but you can do that later
local success, response = pcall(function()
info = game:GetService("MarketplaceService"):GetProductInfo(gameId)
end)
if not success then
warn(response)
task.wait(1)
continue -- Restarts the loop
end
if not isValidGame(info) then
print("Info ", info, "is invalid!")
task.wait(1)
continue
else
break -- exit the loop, the info variable is ready
end
end
-- Use the info variable down here. You could also put all the code above in a function called getRandomGameInfo() or something
while true do
local count = 0
local gameId = math.random(1,100000)
game.Workspace.ID.Value = gameId
task.wait(1)
--local info
--tp(gameId)
-- desc = nil
--title = nil
-- made = nil
-- updated = nil
--workspace.part.SurfaceGui.ImageLabel.Image = "rbxassetid://12975564"
--[[while not success do --2
if count >= 1 then
gameId = math.random(1,100000)
warn("Retrying, count:", count, "\nError:", response) --3
wait(1) --4
end
success, response = pcall(function()
print("yes!")
end)
count = count + 1
end]]
while not success do --2
if count >= 1 then
-- gameId = math.random(1,100000)
warn("retry count:", count, "\nError:", response) --3
wait(1) --4
end
success, response = pcall(function()
if gameId ~= nil then
print("nil")
info = game:GetService("MarketplaceService"):GetProductInfo(gameId)
end
end)
count = count + 1
end
--info = game:GetService("MarketplaceService"):GetProductInfo(gameId)
if info then
warn(info)
end
if info.Description ~= "This is your very first Roblox creation. Check it out, then make it your own with Roblox Studio!" and info.AssetTypeId == 9 and isGamePublic(gameId) == true and not string.find(info.Name,"s Place") and info.Created ~= info.Updated then
workspace.part.SurfaceGui.ImageLabel.Image = "https://assetgame.roblox.com/Game/Tools/ThumbnailAsset.ashx?aid="..gameId.."&fmt=png&wd=420&ht=420"
workspace.Title.SurfaceGui.TextLabel.Text = info.Name
wait(30)
workspace.part.SurfaceGui.ImageLabel.Image = ""
workspace.Title.SurfaceGui.TextLabel.Text = "loading..."
end
end
-- is info a global? you might be trying to access info out of scope. Does the error come from that if statement line?
while true do
local gameId
while not success do
gameId = math.random(1,100000)
success, response = pcall(function()
if gameId ~= nil then
print("nil")
info = game:GetService("MarketplaceService"):GetProductInfo(gameId)
end
end)
if not success then
warn("retry count:", count, "\nError:", response)
task.wait(1)
end
end
-- (hopefully) have a successful game info beyond this point
if (info) and (info.Description ~= "This is your very first Roblox creation. Check it out, then make it your own with Roblox Studio!" ) and (info.AssetTypeId == 9) and (isGamePublic(gameId) == true) and (not string.find(info.Name,"s Place")) and (info.Created ~= info.Updated) then
game.Workspace.ID.Value = gameId
workspace.part.SurfaceGui.ImageLabel.Image = "https://assetgame.roblox.com/Game/Tools/ThumbnailAsset.ashx?aid="..gameId.."&fmt=png&wd=420&ht=420"
workspace.Title.SurfaceGui.TextLabel.Text = info.Name
wait(30)
workspace.part.SurfaceGui.ImageLabel.Image = ""
workspace.Title.SurfaceGui.TextLabel.Text = "loading..."
else
task.wait()
end
end