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.
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.
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)
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
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.