How to make strings for each possibility WITHOUT having to write around 31 strings

this is currently my code (no votetopic code)

location.Fire.Activated:Connect(function()
	local bald = false
	local npc = false
	local geno = false
	local cat = false
	local rare = false
	local wunkus = Players.LocalPlayer.DisplayName
	local votetopic = ""
	if location["1"].TextButton.BackgroundColor3 == activeColor then
		bald = true
	end
	if location["2"].TextButton.BackgroundColor3 == activeColor then
		npc = true
	end
	if location["3"].TextButton.BackgroundColor3 == activeColor then
		geno = true
	end
	if location["4"].TextButton.BackgroundColor3 == activeColor then
		cat = true
	end
	if location["5"].TextButton.BackgroundColor3 == activeColor then
		rare = true
	end
	-- votetopic building code
	votecilent(votetopic, false, "props", npc, geno, rare, bald, cat)
end)

since max unsigned 5 bits is 31 this means those variables could give me a real headache
(WHAT ALL THE VARIABLES DO
bald: removes all dropped hats
geno: remove all npcs
rare: some props are considered rare and not removed by default from a prop clear vote, enabling this overrides that obviously
cat: spare the cats from the genocide
npc: bald must be enabled, npc hats get removed too)

Maybe try using for loop and HashMap for reducing the statements logic

You can use table instead:

-- [ Services ] --
local Players = game:GetService("Players")

-- [ Variables ] --
local player = Players.LocalPlayer
local playerGui = player.PlayerGui

local bald = false
local npc = false
local geno = false
local cat = false
local rare = false

-- [ Types ] --
type location = {
	color: Color3,
	activation: () -> ()
}

local localtions: {[number]: location} = {
	[1] = {
		color = Color3.fromRGB(0, 0, 0),
		activation = function()
			bald = true
		end,
	},
	[2] = {
		color = Color3.fromRGB(0, 0, 0),
		activation = function()
			npc = true
		end,
	},
	[3] = {
		color = Color3.fromRGB(0, 0, 0),
		activation = function()
			geno = true
		end,
	},
	[4] = {
		color = Color3.fromRGB(255, 255, 255),
		activation = function()
			cat = true
		end,
	},
	[5] = {
		color = Color3.fromRGB(0, 0, 0),
		activation = function()
			rare = true
		end,
	}
}

-- [ Testing ] --
local screenGui = Instance.new("ScreenGui")
screenGui.Parent = playerGui

local location1 = Instance.new("TextLabel") -- Your location
location1.Parent = screenGui
location1.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

-- Linear search
for i, location in ipairs(localtions) do
	if location.color == location1.BackgroundColor3 then
		print(i)
		location.activation()
	end
end

Here what i think of :

-- ModuleScript will be used as HashMap
--- ModuleScript1
local self = {}
self.bald = {false, "1"}
self.npc = {false, "2"}
self.geno = {false, "3"}
self.cat = {false, "4"}
self.rare = {false, "5"}
return self

Also how the logic looks :

-- Signal
local pathM = ... -- replace with path to your ModuleScript1
local M1 = require(pathM.ModuleScript1)
location.Fire.Activated:Connect(function()
	-- logic
       for i, v in pairs(M1) do 
            local C = location[v[2]].TextButton.BackgroundColor3
            if C == activeColor then v[1] = true end
       end
	-- votetopic building 
        -- whatever :P
       votecilent(votetopic, false, "props",  table.unpack(M1[1]))
end)

– this example uses for loop and HashMap for the logics and it take about O^2 time complexity
So if you want simple code you might wanna try this first

Im lazy you can apply my logic and add it to the system
Im typing with my feets

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.

1 Like

i like the attributes method it very good and simple than mine

there a error at or not textButton:IsA("TextButton") end could you edit it?

1 Like