Weather data coming out as nil

I’m trying to get the current weather in San Diego with OpenWeatherMap. All of this is working except for one part: the description. When I print the output, everything comes out perfectly except for the description:

Output

Screenshot 2022-04-02 202858

As you can see, it all comes out as nil, and I have no idea why. I’m following this JSON response from the OpenWeatherMap tutorial on how to make an API call, too.

JSON response
  "coord": {
    "lon": -122.08,
    "lat": 37.39
  },
  "weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 282.55,
    "feels_like": 281.86,
    "temp_min": 280.37,
    "temp_max": 284.26,
    "pressure": 1023,
    "humidity": 100
  },
  "visibility": 10000,
  "wind": {
    "speed": 1.5,
    "deg": 350
  },
  "clouds": {
    "all": 1
  },
  "dt": 1560350645,
  "sys": {
    "type": 1,
    "id": 5122,
    "message": 0.0139,
    "country": "US",
    "sunrise": 1560343627,
    "sunset": 1560396563
  },
  "timezone": -25200,
  "id": 420006353,
  "name": "Mountain View",
  "cod": 200
  } 

And here is my code:

local RE = game.ReplicatedStorage:WaitForChild('SendWeather')

local httpService = game:GetService("HttpService")

local requestOptions = {
	Url = "http://api.openweathermap.org/data/2.5/weather?id=" .. 
	"5391811" .. -- id for city
	"&apikey=" ..
	'secretiveapikeyishallnotshow'; 
	Method = "GET";
}

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

local function kelvinsToFahrenheit(temp)
	return temp * 9/5 - 459.67
end

if response.Success then -- if it succeeded (your API key is valid)
	local temperature = kelvinsToFahrenheit(tonumber(responseBodyTable.main.temp)) -- convert to fahrenheit
	local description = tostring(responseBodyTable.weather.description)
	local humidity = tostring(responseBodyTable.main.humidity)
	local clouds = tostring(responseBodyTable.clouds.all)
	print("san diego weather:", tostring(temperature) .. "°F") -- output the temperature
	print('san diego description: '..description)
	print('san diego humidity: '..humidity..'%')
	print(clouds)
	
	RE:FireAllClients(tostring(temperature) .. "°F", description, humidity..'%')
	
else
	error(responseBodyTable.message) -- throw an error based on the response they gave
end
if response.Success then -- if it succeeded (your API key is valid)
	local temperature = kelvinsToFahrenheit(tonumber(responseBodyTable.main.temp)) -- convert to fahrenheit
	local description = tostring(responseBodyTable.weather.description) --> "icon"

I dont know if the “icon” information is really an image… but I would replace it with “description”

"weather": [
    {
      "id": 800,
      "main": "Clear",
      "description": "clear sky",
      "icon": "01d"
    }
  ],
1 Like

Quick question… is the icon a string?

sorry i made a small mistake when I was testing. icon is supposed to be description

When you change it to description does it work?

it doesn’t work. it still returns as nil

what about when you change it to main

For some reason, it still returns as nil.

Quick question: It isn’t because you are converting a string to a string?

I think your issue is that you are trying to convert an already existing string into a string with tostring() therefore it is returning nil. Could be wrong but that’s what I think your issue is.

When I remove the tostring(), it says "Workspace.Script:27: attempt to concatenate string with nil " in the output. So I think I have to convert it to a string.

Ooo, Openweathermap, looks fun. :eyes:

Anyways, try the following instead:

local description = tostring(responseBodyTable.weather[1].description)
2 Likes

Thanks for showing me the solution. Also, if you want an explanation on OpenWeatherMap, you can check this out.

1 Like