Invalid argument #2 to 'random' (interval is empty)\\

I am using ProxyService to make a API script for videos, but I keep getting an error that ‘random’ is invalid. Here is my server code: some sensitive information such as links and API keys have been replaced.

local ProxyService = require(game:GetService('ServerScriptService').ProxyService)

local Proxy = ProxyService:New('https://myhiddenwebsite.com', 'myapikey')

local api = Proxy:Get('https://search.roblox.com/catalog/json?CatalogContext=2&SortAggregation=5&LegendExpanded=true&Category=14')

local tab = {}

for i,v in pairs(api) do
table.insert(tab,v["AssetId"])
end

game.ReplicatedStorage.func.OnServerInvoke = function(plr)
local video = tab[math.random(1,#tab)]
print(video)
return video
end

Client code:

local val = game.ReplicatedStorage.func:InvokeServer()
print(val)

The error prints to BOTH the client and the server.

All help is appreciated

And yes, I am aware the code is messy lol

That means the second argument of math.random is less than the first argument. Which means tab is being read as an empty array. Which means the call to Proxy:Get isn’t returning anything.

1 Like

I used expressive output to print the Proxy:Get and it showed the info of the API link. Upon running print(api) I get table: 0xaa805a19a6e98062, and when using expressive output it shows the info on the api page.

Code for printing:

local ProxyService = require(game:GetService('ServerScriptService').ProxyService)
local Proxy = ProxyService:New('https://example.com', 'mykey')
local api = Proxy:Get('https://search.roblox.com/catalog/json?CatalogContext=2&SortAggregation=5&LegendExpanded=true&Category=14')

print(api)

Could you print #tab after it iterates through api? It seems like that error would only happen if #tab is 0.

1 Like

tab printed 0. How would I prevent this

Are you sure tab is an array and not a dictionary?

1 Like

You should probably check your proxy link and the roblox link to make sure what you want it to return is actually returning.

1 Like

Tab is supposed to be any AssetId’s on this link: https://search.roblox.com/catalog/json?CatalogContext=2&SortAggregation=5&LegendExpanded=true&Category=14 I am pretty sure AssetId returns JUST the id, not "AssetId":1. Let me try and print

EDIT: @megukoo So I tried to print the asset id’s with print(v["AssetId"]) and got a bunch of nil’s printed. I assume this is why the array broke. I don’t fully understand why though

Proxy link seems to work fine and so does the roblox link. If you want I can dm you my roblox link, but I prefer if you don’t use it for anything.

I looked up the documentation for the proxyservice module and it says responses are formatted in a specific way:

Did you make sure that you are using api.body and converting it from JSON, etc? In your code you are just iterating over api

No, I have not done that part. How exactly would I do this though? And what data is needed? Sorry, I am new to this side of scripting.

Maybe try something like this when you build the table:

local tab = {}

local body = game:GetService("HttpService"):JSONDecode(api.body)

for i,v in pairs(body) do
table.insert(tab,v["AssetId"])
end

Ah, that worked. Thank you so much. I was confused why JSONDecode was not working a while ago when I first was experimenting with ProxyService, and then just assumed that they did it for me. Thank you so much.