Attributes not being written correctly

Hi,

I’m making a chemistry sandbox game where players can mix different chemicals together and see them react. I’ve been doing a lot of troubleshooting and rewriting the past few days, but I’m spent now and I would really appreciate some help on what to do.
My problem is when I add more than 2 chemicals that don’t react with each other, there isn’t a “Chemical3” attribute. It rewrites an existing attribute. Here are the two main scripts:

ModuleScript “Compound Information” - holds all relevant functions. Please look for the “SetAttribute” functions.

local composition = require(liquid.getChemicals) -- gets the ModuleScript in the liquid. it has a table of chemicals in the liquid
		local chems = composition.chemicals -- gets the table containing the liquids
		local addNewChemical = true -- if true, then create new attribute. if false, there will be a reaction and that attribute will be replaced. this is useful because at the end of the for loop, I can see if this is true or false and decide what to do
		-- Now I will observe the reactions possible from the perspective of the beaker. We will go chemical by chemical to see if there is a reaction with newChem. if there is, end the loop and fire the event
		for chemNumber, oldChem in pairs(chems) do -- chemNumber is the key (e.g. "Chemical1"), oldChem is the value of that (e.g. "H2O")
			print("This beaker contains " .. oldChem) -- I could use string operations to find the number in the key and use that to say "the first chemical is"
			if compounds.Reactions[oldChem][newChem] and compounds.Reactions[oldChem][newChem].reactionType ~= "No Reaction" and oldChem ~= "H2O" then -- if there IS a reaction
				local reaction = compounds.Reactions[oldChem][newChem] -- get the reaction
				addNewChemical = false -- turn this off because we will just replace the existing attribute with the product
				product = reaction.product
				liquid:SetAttribute(chemNumber, product)
				compounds.effects(reaction, liquid)
				compounds.TweenColor(liquid, compounds.Compounds[product].Color.Color)
				occurs = true
				local productName = compounds.Compounds[product].Formula
				msg = "You added " .. newChem .. " to " .. oldChem .. " in this beaker. " .. productName .. " is formed through the reaction: " .. reaction.ReactionEquation 
				finalChem = product
				break
			elseif not compounds.Reactions[oldChem][newChem] or compounds.Reactions[oldChem][newChem].reactionType == "No Reaction" then -- if no reaction, add to the beaker
				occurs = false
				msg = "There is no reaction with the following chemicals: " .. oldChem .. " in the beaker and " .. newChem .. " being added."
			elseif oldChem == "H2O" and addNewChemical then -- water is the last resort because most reactions will have water
				local reaction = compounds.Reactions[oldChem][newChem] -- get the reaction
				addNewChemical = false -- turn this off because we will just replace the existing attribute with the product
				product = reaction.product
				liquid:SetAttribute(chemNumber, product)
				compounds.effects(reaction, liquid)
				compounds.TweenColor(liquid, compounds.Compounds[product].Color.Color)
				occurs = true
				local productName = compounds.Compounds[product].Formula
				msg = "You added " .. newChem .. " to " .. oldChem .. " in this beaker. " .. productName .. " is formed through the reaction: " .. reaction.ReactionEquation 
				finalChem = product
				break
			end
		end

Here is the ModuleScript within Liquid, which gets all the current chemicals in the liquid:

local chemicals = {}
chemicals.liquid = script.Parent
local liquid = chemicals.liquid
chemicals.chemicals = {
	Chemical1 = chemicals.liquid:GetAttribute("Chemical1")
}
local number = liquid:GetAttribute("NumberOfChemicals")
chemicals.newChemical = function(name, newChem)
-- I put this loop in the comments because I thought it was causing the problem, but it wasn't and it's still happening
	--[[for chemicalNumber, chemical in pairs(chemicals.chemicals) do
		if newChem == chemical then
			liquid:SetAttribute(name, nil)
		else
			chemicals.chemicals[name] = newChem
			print(chemicals.chemicals)
		end
	end]]
	chemicals.chemicals[name] = newChem
	return chemicals.chemicals
end

return chemicals

I think the problem stems from the “Compound Information” script not being changed because it’s a for loop. I had thought of another reason but I’ve forgotten it.
Here is in-game footage of what I’m talking about:

I programmed this so attributes are only replaced when there’s a reaction that happens. The chemical in the beaker is supposed to be replaced by that reaction’s product. There isn’t supposed to be a reaction with acids and acids or bases and bases, as you can see with the notifications. Why is it rewriting over “Chemical2?”
I would really appreciate some help on how to fix this. I’ve been working on this since noon and have completely rewritten some key parts. I’d appreciate it a lot if any of you could help me.