Errors with my API Script

Don’t judge me too harshly as it’s my first time using API’s on Roblox, but I’m trying to play around with using open weather maps API on Roblox to get weather data, and that part does work the problem is I’m now trying to get specifically the weather condition data from the API. I’m probably missing something obvious but as I said I’m not too familiar with API’s.

Script:


local Api_Key = "my API key here"
local Api_City_Name = "New York"
local Api_Url = "https://api.openweathermap.org/data/2.5/weather?q="..Api_City_Name.."&appid="..Api_Key

local HS = game:GetService("HttpService")

local Data = nil
local s, e = pcall(function()
	Data = HS:GetAsync(Api_Url)
end)

if Data ~= nil then
	Data = HS:JSONDecode(Data)
end

local response = HS:RequestAsync(requestOptions)
local responseBodyTable = HS:JSONDecode(response.Body)

if response.Success then
	local weather = (tostring(responseBodyTable. weather[1].main))
	print("Current Weather :",tostring(weather))

else
	error(responseBodyTable.message)
end


Can you show us the error message?

handle pcall status so you can see if it executed or not.

local s, e = pcall(function()
	Data = HS:GetAsync(Api_Url)
end)

if not s then
    warn(e)
end

You should also be pcalling JSONDecode(), and RequestAsync() as these can error outside of your control.

if line 17 is still local responseBodyTable = HS:JSONDecode(response.Body) like your original code snippet then the problem is response is nil so something went wrong with the call to HS:RequestAsync(requestOptions)

pcalling the methods I mentioned earlier as well as handling the pcall status & error message should shine more light on what went wrong.

I’m a bit confused (still new to API’s here) do you suggest replacing line 17 with

local s, e = pcall(function()
	Data = HS:GetAsync(Api_Url)
end)

if not s then
    warn(e)
end

no you just need to pcall every networked method (GetAsync, RequestAsync) and possible malformed data methods (JSONDecode).

So just like you would have done above for GetAsync, you do again for RequestAsync.

local response
local s, e = pcall(function()
    local response = HS:RequestAsync(requestOptions)
end)

if not s then
    warn(e)
end

That resulted in another error on line 26 (the beginning of the RequestAsync pcall).

please send the error

for future reference: when youre on the devforum, always send your errors if you are looking for help

Terribly sorry for the late reply, your reply got mixed up with the other notifications and I didn’t see it, here are the errors I’m getting:

local Api_Key = "your api key here"
local Api_City_Name = "New York"
local Api_Url = "https://api.openweathermap.org/data/2.5/weather?q="..Api_City_Name.."&appid="..Api_Key

local HS = game:GetService("HttpService")

local Data
local s, e = pcall(function()
	Data = HS:GetAsync(Api_Url)
end)

if Data ~= nil then
	Data = HS:JSONDecode(Data)
	
	if Data.weather then
		local weather = tostring(Data.weather[1].main)
		print("Current Weather : "..weather)
	else
		error()
	end
end

This should work now

1 Like