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?
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 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