Help with Place ID validation

local function IsGameValid(id)
	local isgame

	local success,message = pcall(function()
		local gameid = MarketplaceService:GetProductInfo(id) 
		isgame = gameid.AssetTypeId == 9
	end)

	return success and isgame
end

local function getPlaceId()
	local placeID = math.random(6000,99999999)

	if IsGameValid(placeID) then
		return placeID
	else
		getPlaceId()
	end
end

function module.setupPortal(portalNum, idObj, nameObj, numberObj, portalObj)
	local nameLabel = nameObj.SurfaceGui.TextLabel
	local numberLabel = numberObj.SurfaceGui.TextLabel
	local light = portalObj.SurfaceLight
	
	numberLabel.Text = portalNum
	nameLabel.Text = "LOADING"
	
	local randomID = getPlaceId()
	repeat wait() until randomID
	
	local randColor = Color3.fromRGB(math.random(0, 255),math.random(0, 255),math.random(0, 255))
	
	nameLabel.Text = MarketplaceService:GetProductInfo(randomID).Name
	idObj.Value = randomID
	portalObj.Color = randColor
	light.Color = randColor
end

What all of this does is generate a random place ID, and then check if it’s valid. If it isn’t it calls itself again and again until it gets one.

The problem is that if it doesn’t get a valid place ID the first time it’s called, it’ll just hang forever and not do anything, even if it does get a valid one after a few more tries. I probably need something better than repeat wait() until. Are there any solutions?

I have a question… What are the use cases for this?

To create a portal that leads into a random game… Why must you know?

I didn’t need to know, I was just curious. I will look over your script now.

The IsGameValid function returns two values, success and isgame. When you wrote if IsGameValid(placeID) then, the if statement is only checking the success value, not isgame, which is the one you want.

I would recommend making a variable first like this:

local function getPlaceId()
	local placeID = math.random(6000,99999999)
    local success, isvalid = IsGameValid(placeID)
	if success and isvalid then
		return placeID
	else
		getPlaceId()
	end
end

I don’t think thats the case. Even if it is, that didn’t work. The problem isn’t really with that function.

I did some of my own testing, and found some things.

Here is a revised version of your script in the original post:

local function IsGameValid(id)
	local isgame

	local success,message = pcall(function()
		local gameid = MarketplaceService:GetProductInfo(id) --hoping you defined MarketplaceService somewhere
		isgame = gameid.AssetTypeId == 9
	end)

	return isgame
end

local function getPlaceId()
    local placeID
    repeat
        placeID = math.random(6000,99999999)
    until IsGameValid(placeID)
    return placeID
end

function module.setupPortal(portalNum, idObj, nameObj, numberObj, portalObj)
	local nameLabel = nameObj.SurfaceGui.TextLabel
	local numberLabel = numberObj.SurfaceGui.TextLabel
	local light = portalObj.SurfaceLight
	
	numberLabel.Text = portalNum
	nameLabel.Text = "LOADING"
	
	local randomID = getPlaceId()
	repeat wait() until randomID
	
	local randColor = Color3.fromRGB(math.random(0, 255),math.random(0, 255),math.random(0, 255))
	
	nameLabel.Text = MarketplaceService:GetProductInfo(randomID).Name
	idObj.Value = randomID
	portalObj.Color = randColor
	light.Color = randColor
end

This should work. Let me know :slight_smile:

1 Like

And to clarify, this doesn’t cause any lag or disturbance although it uses a repeat. It gets a correct id within a few tries.

Also, disclaimer, this will not detect if a game is private or not. If you’d like help with that let me know.

I’ll test it tomorrow. It probably would lag a bit though because a lot of scripts will be requiring that.

It might, but it would be the same as the previous method, since it is basically doing the same thing.