`TypeError: Cannot add indexer to table`

Hello, I tried using keyof<> and typeof() functions in my code but it doesn’t work and I don’t understand the error message.

Here is my code:
function unicodeProgressBar.generateBar(value: number, minRangeValue: number, maxRangeValue: number, length: number, background_block: keyof<typeof(CONSTANTS.BACKGROUND_BLOCKS)>): string
	local bar: string = ""

	local normalizedValue: number = (value - minRangeValue) / (maxRangeValue - minRangeValue)
	local scaledToLengthValue: number = length * normalizedValue

	local scaledToLengthValueWhole: number, scaledToLengthValueMantissa: number = math.modf(scaledToLengthValue)

	local quantizedScaledValueMantissa: number = quantize(scaledToLengthValueMantissa, CONSTANTS.INTERVAL)

	for _ = 1, scaledToLengthValueWhole, 1 do
		bar ..= CONSTANTS.BLOCKS[8]
	end
	bar ..= CONSTANTS.BLOCKS[quantizedScaledValueMantissa * CONSTANTS.INTERVAL_DENOMINATOR]
	
	for _ = 1, length - scaledToLengthValueWhole, 1 do

--
		bar ..= CONSTANTS.BACKGROUND_BLOCKS[background_block] -- HERE IS THE ERROR
--

	end
	
	return bar
end
Here is the CONSTANTS module:
local CONSTANTS = {}

CONSTANTS.BLOCKS			=	{[0] = "" --[[ Necessary as indices start at 1 ]], "▏","▎","▍","▌","▋","▊","▉","█"} -- to self: smooth right to left bars are not possible as the necessary characters do not exist in unicode
CONSTANTS.BACKGROUND_BLOCKS	=	{
    ["NONE"] = "", 
    ["TRANSPARENT"] = " ", 
    ["MINIMAL"] = "░", 
    ["MEDIUM"] = "▒", 
    ["MEDIUM_REVERSED"] = "�", 
    ["FULL"] = "▓"
}

CONSTANTS.INTERVAL		        =	1/8
CONSTANTS.INTERVAL_DENOMINATOR	=	8

return CONSTANTS

I put my CONSTANTS in a separate script as to avoid cyclic requires.
Pls help.

Cannot add indexer to table means you’re trying to index a table with an invalid data type. I don’t know where you are getting keyof<> from but as far as i’m aware that is not a valid operator in Lua?

It is valid in the new luau type solver.

I see it now, I was unaware they already put the new solver on beta. Let me make a quick repro of this and I’ll see what I can figure out

1 Like

Can I get a quick rundown of what the background_block variable the generateBar function gets fed?

It can get fed these:
"FULL" | "MEDIUM" | "MEDIUM_REVERSED" | "MINIMAL" | "NONE" | "TRANSPARENT"

odd, in my reproduction of your script:

Repro
local CONSTANTS = {}

CONSTANTS.BLOCKS			=	{[0] = "" --[[ Necessary as indices start at 1 ]], "▏","▎","▍","▌","▋","▊","▉","█"} -- to self: smooth right to left bars are not possible as the necessary characters do not exist in unicode
CONSTANTS.BACKGROUND_BLOCKS	=	{
	["NONE"] = "", 
	["TRANSPARENT"] = " ", 
	["MINIMAL"] = "░", 
	["MEDIUM"] = "▒", 
	["MEDIUM_REVERSED"] = "�", 
	["FULL"] = "▓"
}

CONSTANTS.INTERVAL		        =	1/8
CONSTANTS.INTERVAL_DENOMINATOR	=	8

function generateBar(value: number, minRangeValue: number, maxRangeValue: number, length: number, background_block: keyof<typeof(CONSTANTS.BACKGROUND_BLOCKS)>): string
	local bar: string = ""

	local normalizedValue: number = (value - minRangeValue) / (maxRangeValue - minRangeValue)
	local scaledToLengthValue: number = length * normalizedValue

	local scaledToLengthValueWhole: number, scaledToLengthValueMantissa: number = math.modf(scaledToLengthValue)

	local quantizedScaledValueMantissa: number = 1 -- Replaced cuz i dont have the quantize function

	for _ = 1, scaledToLengthValueWhole, 1 do
		bar ..= CONSTANTS.BLOCKS[8]
	end
	bar ..= CONSTANTS.BLOCKS[quantizedScaledValueMantissa * CONSTANTS.INTERVAL_DENOMINATOR]

	for _ = 1, length - scaledToLengthValueWhole, 1 do

		--
		bar ..= CONSTANTS.BACKGROUND_BLOCKS[background_block] -- HERE IS THE ERROR
		--

	end

	return bar
end

print(generateBar(1, 0, 100, 10, "FULL"))

No such error as what you are getting is appearing for me. I did leave away the quantize function but I doubt that has any effect.

1 Like

What if I give you my place file? The module is in replicatedstorage

If you’re willing to do so then yes I’d like to look at your place file

unicodeprogressbar.rbxl (65.6 KB)
Here it is. Thanks for being invested btw

Thank you. I’ll poke around and see what I can find. My hunch is that it’s a quirk with the fact you’re using a module script to store the unicode data but I’m not sure yet

1 Like

update: works perfectly fine in my studio right now:

Oh. Is there no linting in the modulescript too?

I’ve done a bunch of digging and this pending bug seems to be your case: Release Notes for 647 | Documentation - Roblox Creator Hub.
"Fixes new solver crashes related to unions of tables. Users might see false positive errors related to “Cannot add indexer to table”. "

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.