Module and LocalScript Help

So I have 4 tables of colors for the 4 seasons in my game, and they look like this,

LocalScript:

local NewYearsColors = {
	"Flame reddish orange",
	"Dark orange",
	"Persimmon"
}

local July4thColors = {
	"Royal purple",
	"Bright green",
	"Tr. Red"
}

local HalloweenColors = {
	"Deep blue",
	"Dark green",
	"Br. reddish orange"
}

local ChristmasColors = {
	"Medium red",
	"Dove blue",
	"Slime green"
}

And I am trying to set images and frame background colors to the colors in the tables above so I descied to make a module and it looks like this,

Module:

local Season = {
	SeasonData = {
		NewYears = false,
		July4th = true,
		Halloween = false,
		Christmas = false,
	}
}

return Season

How would I check if a Value in SeasnData is true, without making an over complicated LocalScript. Here’s what my LocalScript looks like and I feel like it’s so unefficient.

local NewYears = nil
local July4th = nil
local Halloween = nil
local Christmas = nil

if SeasonModule.SeasonData.NewYears == true then
	NewYears = true
elseif SeasonModule.SeasonData.July4th == true then
	July4th = true
elseif SeasonModule.SeasonData.Halloween == true then
	Halloween = true
elseif SeasonModule.SeasonData.Christmas == true then
	Christmas = true
end

But I am having trouble trying to tie everything together, how would I make CurrentColorGroup = to the correct Season? For example 4th of July is coming up and so how would it just change to the July4thColors color table according to the module. Hopefully this all makes sense lol…

local CurrentColorGroup = ??????????
local ChosenColor = CurrentColorGroup[math.random(1, #CurrentColorGroup)]
MainLogo.ImageColor3 = BrickColor.new(ChosenColor).Color
Top.BackgroundColor = BrickColor.new(ChosenColor)
1 Like
local colors = {
	["NewYears"] = {
		"Flame reddish orange",
		"Dark orange",
		"Persimmon",
	},
	["July4th"] = {
		"Royal purple",
		"Bright green",
		"Tr. Red",
	},
	["Halloween"] = {
		"Deep blue",
		"Dark green",
		"Br. reddish orange",
	},
	["Christmas"] = {
		"Medium red",
		"Dove blue",
		"Slime green",
	},
}

local selectedColor = "July4th"

for i, color in colors[selectedColor] do
	print(i, color)
end

local colorGroup = colors[selectedColor]
local randomColor = colorGroup[math.random(#colorGroup)]
MainLogo.ImageColor3 = BrickColor.new(randomColor).Color
Top.BackgroundColor = BrickColor.new(colorGroup[1])
2 Likes

In my original scrpt, I had this local CurrentColorGroup = July4thColors. And it looks like you did something similar. I would prefer CurrentColorGroup to equal the correct season without me having to change it every time.

That way I can just change Halloween to true and July4th to false (or whatever) and the colors would update accordly. Hopefully that makes sense.

How would I do this?

1 Like

I don’t quite understand the difference but maybe like this?

local colors = {
	["NewYears"] = {
		false,
		{"Flame reddish orange", "Dark orange", "Persimmon"}
	},
	["July4th"] = {
		true,
		{"Royal purple", "Bright green", "Tr. Red"}
	},
	["Halloween"] = {
		false,
		{"Deep blue", "Dark green", "Br. reddish orange"}
	},
	["Christmas"] = {
		false,
		{"Medium red", "Dove blue", "Slime green"}
	},
}

local colorGroup = nil
for name, data in colors do
	if data[1] == false then continue end
	colorGroup = data[2]
	break
end

for i, color in colorGroup do
	print(i, color)
end

local randomColor = colorGroup[math.random(#colorGroup)]
MainLogo.ImageColor3 = BrickColor.new(randomColor).Color
Top.BackgroundColor = BrickColor.new(colorGroup[1])
1 Like

Kind of. Basically, the module script below points to which color I should be using.

For example if Christmas was true the colors would automatically switch to the Christmas Color group. That way I do not have to go in and switch out local selectedColor = "July4th" to local selectedColor = "Christmas", hopefully that makes more sense.

That seems a bit unnecessary but who cares.

Once you receive the season table (the true or false values) from the module, you can use a for loop to check which one is true.

I tried to replicate something similar to what you want so here’s the table I have in the module:

function module.Seasons()
	local SeasonData = {
		NewYears = false,
		July4th = true,
		Halloween = false,
		Christmas = false,
	}
	
	return SeasonData
end

Then in the localscript I’m calling this function:

local module = require(game.ReplicatedStorage.ModuleScript)

local CurrentSeason = ""

local function checkseason()
	local seasons = module.Seasons()
	for i, v in pairs(seasons) do
		if v == true then
			CurrentSeason = i -- set CurrentSeason = i (the name of the season that is true)
		end
	end
end

checkseason()

print(CurrentSeason) -- for the correct result

The “i” will print the season that is true. From there you can use the color table and find the same color group with the same name as i.

local colors = {
	NewYears = {
		
		"Flame reddish orange", "Dark orange", "Persimmon"
	},
	
	July4th = {
		
		"Royal purple", "Bright green", "Tr. Red"
	},
	
	Halloween = {
		
		"Deep blue", "Dark green", "Br. reddish orange"
	},
	
	Christmas = {
		
		"Medium red", "Dove blue", "Slime green"
	}
}

local colorGroup = colors[CurrentSeason]
1 Like