How do i get a Table index by it's value?

I have a script that gets the biggest value from a table using math.max, however, when I print it, it prints the value, but I want to print the Index, because that’s what I need for my game, my script:

local ChoosenMap = math.max(unpack(Votes))
print(ChoosenMap) --// Prints the value (1) instead of it's index.

I don’t know how to do it, what I could do is get the index with a for i,v in pairs line, but this is not working in this case, I would need to do much more messy code to fix this, thanks for reading.

7 Likes

Use table.find(t, v). It returns the index of v in table t, returns nil if v is not an element of t.

local t = { "a", "b", "c" }
print(table.find(t, "b")) -- 2
print(table.find(t, "d")) -- nil
22 Likes

I know it’s been over a year and all. But I have an issue like this but in the opposite way. I don’t want to make a new topic because it’s similar to my problem. How would I be able to get the Table value by its index?? I tired of using the table.find but it didn’t seem to work.

3 Likes

tab[index] is all you need.

local t = { "a", "b", "c" }
print(t[1])
print(t[2])
local t = { car = "red", truck = "blue", tree = "green" }
print(t.car)
print(t["car"])
local index = "truck"
print(t[index])
2 Likes

Didn’t really solve my issue. I just want to find the table value by it’s index. Basically the opposite way from the question of this topic. I want to find the value of the index.

Can you explain it in a different way? What are you trying to do with this? Because your wording seems 100% crystal clear to me and doesn’t leave me room for confusion, and somehow I still misunderstood?

Just in case I didn’t misunderstand but I instead worded it badly, here’s a slight reiteration.
value = table[index]
that’s how you find a value if you know the index.
(compared of course to table.find(t, value) which is how you find the index if you know the value)

Ok so here I have a math.random table that stores all the colours and gets the index.

local index = math.random(1, 10)
local colour = colours[index]
print(colour)

But I printed colour and it just gave me nil.

Ah. Perhaps you have a dictionary instead of an array. Can you show me the table colours please?

Here you go:

local colours = {
			red = Color3.fromRGB(255, 0, 0);
			orange = Color3.fromRGB(255, 170, 0);
			yellow = Color3.fromRGB(255, 255, 0);
			green = Color3.fromRGB(0, 170, 0);
			blue = Color3.fromRGB(85, 85, 255);
			purple = Color3.fromRGB(85, 0, 127);
			pink = Color3.fromRGB(255, 85, 127);
			grey = Color3.fromRGB(138, 138, 138);
			black = Color3.fromRGB(0, 0, 0);
			white = Color3.fromRGB(255, 255, 255);
		}	

That is indeed your problem. In your table (which is a dictionary of string keys instead of numeric) your keys are accessed like this
colours["red"]
instead of
colours[1]
so the math.random (which provides a number) is useless here.

Either get rid of the red =, pink =, etc if you don’t need them, or you can do this instead.

local colours = {
	red = Color3.fromRGB(255, 0, 0);
	orange = Color3.fromRGB(255, 170, 0);
	yellow = Color3.fromRGB(255, 255, 0);
	green = Color3.fromRGB(0, 170, 0);
	blue = Color3.fromRGB(85, 85, 255);
	purple = Color3.fromRGB(85, 0, 127);
	pink = Color3.fromRGB(255, 85, 127);
	grey = Color3.fromRGB(138, 138, 138);
	black = Color3.fromRGB(0, 0, 0);
	white = Color3.fromRGB(255, 255, 255);
}
local coloursList = {}
for index, value in pairs(colours) do
	table.insert(coloursList, value)
end

-- later in the script
local random = math.random(#coloursList)
local colour = coloursList[random]

Right, but these colours I use are for a loop that randomly colours the parts within the table:

for _,Part in ipairs(parent:GetChildren()) do
	if Part:IsA("Part") then
		local index = math.random(1, 10)
		local colour = colours[index]
		print(colour)
		Part.BrickColor = colour -- just a rough example
		wait()
	end
end

Same deal, just implement these modifications to your code. coloursList is automatically created now in a way that allows you to use math.random, and I just modified the random bits of the code to use coloursList instead of colours.

local colours = {
	red = Color3.fromRGB(255, 0, 0);
	orange = Color3.fromRGB(255, 170, 0);
	yellow = Color3.fromRGB(255, 255, 0);
	green = Color3.fromRGB(0, 170, 0);
	blue = Color3.fromRGB(85, 85, 255);
	purple = Color3.fromRGB(85, 0, 127);
	pink = Color3.fromRGB(255, 85, 127);
	grey = Color3.fromRGB(138, 138, 138);
	black = Color3.fromRGB(0, 0, 0);
	white = Color3.fromRGB(255, 255, 255);
}
local coloursList = {}
for index, value in pairs(colours) do
	table.insert(coloursList, value)
end
for _,Part in ipairs(parent:GetChildren()) do
	if Part:IsA("Part") then
		local index = math.random(#coloursList)
		local colour = coloursList[index]
		print(colour)
		Part.BrickColor = colour -- just a rough example
		wait()
	end
end
1 Like

I fixed it a bit and it works! Thanks for your help I really appreciate it!

1 Like