Attempt to index nil with :GetProductInfo()

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

Both the pcall and the if statement are inside a while loop, and the game id is randomly generated at the start of the while loop

local gameId = math.random(1,100000)

I will try to add an if statement

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

Idk man I can’t understand code I can’t see.

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


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

Thanks!! I swapped the pcall for my pcall and it worked.

1 Like

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