More efficient way of doing seasonal shop items?

for PendantName, PendantInfo in pairs(PendantIndex) do
		if PendantInfo.Type == "Regular" then
			table.insert(PendantOptions, PendantName)
		elseif PendantInfo.Type == "Seasonal" then
			if PendantInfo.Season == "Winter" and (CurrentMonth == "01" or CurrentMonth == "02" or CurrentMonth == "12") then
				table.insert(PendantOptions, PendantName)
			elseif PendantInfo.Season == "Spring" and (CurrentMonth == "03" or CurrentMonth == "04" or CurrentMonth == "05") then
				table.insert(PendantOptions, PendantName)
			elseif PendantInfo.Season == "Summer" and (CurrentMonth == "06" or CurrentMonth == "07" or CurrentMonth == "08") then
				table.insert(PendantOptions, PendantName)
			elseif PendantInfo.Season == "Fall" and (CurrentMonth == "09" or CurrentMonth == "10" or CurrentMonth == "11") then
				table.insert(PendantOptions, PendantName)
			end
		end
	end

For context, my script iterates through all ‘pendants’ and if its type is the current real life season, it’ll be added to a table of pendants in the shop. I’m looking for a better way than a huge elseif chain, if there is one. Thanks!

Replace pairs with ipairs. Why do people keep forgetting this

Because it appears the table is a dictionary, which errors when used with ipairs.

make a table then use table.find and yea that should do it

I don’t know if it would be much simpler, but how would it look if you made a CurrentSeason variable, once that is assigned, then you’d only need one line to assign any seasonal pendant to it, like:

if PendantInfo.Season == CurrentSeason then
				table.insert(PendantOptions, PendantName)

I think it might look simpler, whether it is or not. :slight_smile:
also, a shortcut to build your currentSeason variable might be using this odd way of assigning a variable with conditionals, i tested it and it works:

image

Try it, its one alternative to elseif barrage:

local month = 9

local currentseason = (month == 1 or month ==2 or month == 3) and "winter" or (month==6 or month ==7 and "summer") or (month == 9 or month == 10) and "whatever"

print(currentseason)
2 Likes