Set values throughout studio to values held in a module based on information from an API

I understand the title is a bit of a mouthful, and a tad confusing, so let me explain further.

I’m working on a system that takes real weather API data and translates that into the lighting system in the game. I have a module that holds the different values for different lighting properties and a serverscript that grabs the API data and changes the different lighting properties based on what weather comes back from the API table. My problem is, I have no idea, as the title suggests, how to basically make my script get to see what the API is returning for the weather, and then find the corresponding weather type in my module and set the different properties to the properties defined in the different parts in my module. For now, my module only has thunderstorms, but it will have more to come.

Serverscript

local Api_Key = "apikeyhere"
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 module = require (game.ReplicatedStorage.Modules.WeatherConditionHandler)

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)
		game.ReplicatedStorage:WaitForChild("weather"):FireAllClients(weather)
	else
		error()
	end
end


for keyName, keyValue in pairs(module) do
	
	
	
end
ModuleScript
local WeatherInfo = {
	
	["Thunderstorm"] = {
		CloudSize = 1;
		CloudColor = 1;
		Brightness = 1;
		AtmosphereDensity = 1;
		AtmosphereOffset = 1;
		SkyboxBk = 1;
		SkyboxDn = 1;
		SkyboxFt = 1;
		SkyboxLf = 1;
		SkyboxRt = 1;
		SkyboxUp = 1
	};
	
}

return WeatherInfo

right here, what you can do is grab the appropriate data like so

local weatherProperties = module[weather]

this will give you all the properties now for Thunderstorm or wahtever the weather is. you can send this off to the client now or i’d probably update it from the server.

with this you can loop through the properties or set it individually like so:

for property, value in pairs(weatherProperties) do
– stuff
end

but id probably just do skyBox.SkyboxBk = weatherProperties.SkyboxBk

and so on

I might be interpreting what you said wrong, but I’m talking about let’s say it’s clear, then the clear values are gathered from the module, all the different properties are changed so on and so fourth, how do I do that part, the only gathering variables from a certain table from the module depending on what the weather variable is.

like i said

local weatherProperties = module[weather]

if the weather is returned as ‘Thunderstorm’, it’ll index your ModuleScript with ‘weather’ variable and try to find Thunderstorm, if it does exist then you can simply do:

weatherProperties.CloudSize

and so on

I tried this and got a 18:22:52.750 ServerScriptService.Weather System:24: attempt to index nil with 'SkyboxBk' - Server - Weather System:24 error, any idea why?

try print ‘weather’ and see what is coming out

Printing weather prints as clouds.

then you need to add a clouds entry to your modulescript or have something that checks if the module contains the weather as a key, and if it doesnt, return a default like ‘Thunderstorm’ e.g

local weatherProperties = module[weather] or module[“Thunderstorm”]

1 Like