There are a couple aspects to consider when refactoring this code. For instance, how often do you expect to add new possibilities, or rename possibilities in your code?
Naming each container with the textbutton “1”, “2”, “3” etc will be unmaintainable in the long term because you need to remember which container is responsible for which possibility, which is error prone. I’d prefer it if you renamed these to “npc”, “geno”, “rare” etc instead.
Now you need to consider how often you’d rename possibilities, or add new possibilities, and design your system accordingly. If you’d rename geno to genocide, for instance, you’d want to have typechecked code so you can quickly find every instance where you’re still using the old geno name rather than the genocide name. In a case like this, I might write something like this:
--!strict
type options = {
bald: boolean,
npc: boolean,
geno: boolean,
cat: boolean,
rare: boolean
}
location.Fire.Activated:Connect(function()
local options: options = {
bald = location.bald.TextButton.BackgroundColor3 == activeColor,
npc = location.npc.TextButton.BackgroundColor3 == activeColor,
geno = location.geno.TextButton.BackgroundColor3 == activeColor,
cat = location.cat.TextButton.BackgroundColor3 == activeColor,
rare = location.rare.TextButton.BackgroundColor3 == activeColor
}
-- votetopic building code
votecilent(votetopic, false, "props", options)
end)
-- Somewhere else
local function votecilent(votetopic, bool, props, options: options)
...
end
If you want to facilitate adding new possibilities as quickly as possible instead, an automated solution might be better. In such a case, you might want to add attributes to your textbuttons instead, and automatically collect all selected attributes:
--!string
type possibilities = {string}
location.Fire.Activated:Connect(function()
local possibilities: possibilities = {}
for _, container in location:GetChildren() do
-- Only consider containers which have a textbutton
local textbutton = container:FindFirstChild("TextButton")
if textbutton == nil or not textbutton:IsA("TextButton") then
continue
end
-- Only insert the possibiliy if the container has
local possibility = container:GetAttribute("Possibility")
if textbutton.BackgroundColor3 == activeColor and possibility ~= nil then
table.insert(possibilities, possibility)
end
end
-- votetopic building code
votecilent(votetopic, false, "props", possibilities)
end)
-- Somewhere else
local function votecilent(votetopic, bool, props, possibilities: possibilities)
...
end
You’d then need to add an attribute to the container named “Possibility”, and give it the value of the possibility you want.
This has the downside that upon changing a possibility, you might break some code elsewhere, but it does make it easier to add hundreds or thousands of possibilities without ever looking at this piece of code again.