Get a random game from the entire Roblox webpage, [Place Roulette]

E

I tried math.random() for placelD but unfortunately it did not work,
after one google search l gave up looking and decided to come to the devforum!
Any way of doing this?

You’re most likely gonna wanna get a random number within a certain range and then checking if its a valid place with game:GetService("MarketplaceService"):GetProductInfo(Id). If its not a valid place, recurse and repeat the same process until you find a valid place. Though, there are most likely better ways to go about this (I myself, wouldn’t know as I’ve never looked into making a place roulette type game)

1 Like

That would be the only way I know of as well. Just like you said we would look for a number until it’s a valid Id with a recursive function. Something like this would probably do:

local MPS = game:GetService("MarketplaceService")

local function RetrieveRandomGame()
	local digits = {} --Store the digits of the random place id
	for i=1,10 do
		table.insert(digits, math.random(0,9)) -- insert the digits
	end
	
	local PlaceId = tonumber(table.concat(digits)) -- Combine the digits table into a single number
	if PlaceId then -- Validate it wasn't nil (which it should never be but just to be safe)
		local Place = MPS:GetProductInfo(PlaceId) -- Get game from place id
		local IsAGame = Place["AssetTypeId"] == 9 -- Make sure it's a game and not something else (like a shirt)
		if Place and Place ~= {} and IsAGame then return Place else return RetrieveRandomGame() end -- Make sure it exists
	else
		return RetrieveRandomGame()
	end
end

local randomGame = RetrieveRandomGame()
print(randomGame)

The only thing is most of the times a random generator like that will just return a random user’s starter place.