Script running extremly slow when using functions and math.random

Hey, recently I started working on a random place finder game and I ran into an issue. It was all working fine until it randomly was taking ages to find a new place. This is my script to generate a new placeid. It takes around 0.1 seconds to loop and the majority fall under what I like to call “Ending in Place”. The game is almost unplayable with this amount of “lag”.

local function Check(id)
	local Info = nil
	local success, result = pcall(function()
		Info = MarketplaceService:GetProductInfo(id,Enum.InfoType.Asset)
	end)
	if not success then
		return false
	end
	if not Info or string.find(Info.Name, "Place") or string.find(Info.Name, "[ Content Deleted ]") or string.find(Info.Name, "#####") or Info.AssetTypeId ~= 9 then
		return false
	end
	return true
end

local function GeneratePlace()
	local RandomPlace = math.random(1,1000000000)
	print(RandomPlace)

	if not Check(RandomPlace) then
		GeneratePlace()
		return
	end
end

Incase you’re wondering this script is located inside ServerScriptStorage.

4 Likes

Considering that you are just getting a random number, and checking if its a place, and if not, repeating this proccess. Sometimes, yes, you can get lucky. But othertimes you could get unlucky, and need many calls. There is ‘limits’ on the marketplace API and According to this Dev forum post It looks like that there is a limit ‘GetProductInfo 100 times for around 40 seconds’. As GetProductInfo does Yield too. So this ‘lag’ is probably (My guess) is that the API is yielding requests as you could be sending a ton of requests. You can see how long these requests are taking inside the developer console too!

1 Like

Do you know why it’s getting random numbers slow? I just tested it without the check and it runs super fast. Plus i never ran into the product info limiting error.

I would assume it’s not hitting a valid random id over and over.

Seems like a very odd code …

Is it truly the random numbers that takes time? I don’t think that’s the issue by looking at this code unless there is prints showing otherwise… I think you keep yielding each request with get product info, that when you finally get the info and is not a place, you yielded too much, and once done yielding then you make a new number and repeat this process.

Do you think if I put the Check function inside a coroutine, it would fix itself?

No, because you do need to yield there is nothing you can do, because you need to check if the ID is a place ID. Coroutine won’t help because you will need to wait for a response no matter what for if it returns true. I just ran this code (Just had some more prints when it fails) and it failed over 1000 times+.

Code:

local Number = 0

local function Check(id)
	local Info = nil
	print("Getting Info")
	local success, result = pcall(function()
		Info = game:GetService("MarketplaceService"):GetProductInfo(id,Enum.InfoType.Asset)
	end)
	if not success then
		print("Failed: "..Number)
		return false
	end
	if not Info or string.find(Info.Name, "Place") or string.find(Info.Name, "[ Content Deleted ]") or string.find(Info.Name, "#####") or Info.AssetTypeId ~= 9 then
		print("Failed: "..Number)
		return false
	end
	return true
end

local function GeneratePlace()
	local RandomPlace = math.random(1, 99999999)
	print(RandomPlace)
Number += 1
	if not Check(RandomPlace) then
		GeneratePlace()
		return
	else 
		print("Got A successful one at request number: "..Number)
	end
end
GeneratePlace()

My results:

Edit: It just came back true at request Number 2357 btw. But it took 2356 requests before this to handle it.