How to find where abouts the highest number in a table is

I want to locate where in the table the highest number is, then use that number to locate a certain map in another table.

I’ve tried using different methods but I can’t find one that works.

I’ve tried table.find but couldn’t wrap my head around it.

function SelectMap()
	
	for i, Map in pairs(MapVoting.Maps:GetChildren()) do
		
		local GivenVotes = Map.Votes.SurfaceGui.VotesLabel.Text
		
		table.insert(Votes, GivenVotes)
		table.insert(VoteList, Map.MapName.SurfaceGui.MapName.Text)
		
	end
	
	
	local Highest = math.max(unpack(Votes))
end

image

In the screenshot provided, the first table is the 3 maps in the voting rotation, the second table is the votes. Meaning that all the maps have been voted 0 times, apart from pyramid which has been voted once. I want to use that knowledge to make it so that the script knows that pyramid is the highest voted map. Any help is appreciated.

are u trying to get max value between strings?

“0” and 0 are different

I’m trying to find out which map is voted the most. In this case. It is the pyramid map with 1 vote, but I don’t know how to make the script know that pyramid is the most voted one.

nvm, math.max works with strings anyways

By the poor explanation u provided its very hard to help u,

In general, u can get max value with math.max() function or iteration through the table.

local highestIndex = 0
local highestValue = 0
for i, v in pairs(numbertab) do
    if v > highest then
        highestValue = v
        highestIndex = i
    end
end

print(higuestIndex, highestValue)

Sorry, Im not very good at explaining. I know how to get the maximum value, in the script provided it says “local highest” which is the highest value, so the script knows that the 3rd thing in the table has the highest votes, but it doesn’t know that that thing is. If you look at the screenshot i kind of just want to compare the tables together so the script knows that the third map (pyramid) has the highest votes.

Sinse you have 2 tables, you will have to link vote to map by index.

local MostVotedMap = MapsTable[highestIndex]

Applying to ur code it would be something like

local mostVotedMap = MapVoting.Maps:GetChildren()[1]

for i, Map in pairs(MapVoting.Maps:GetChildren()) do
	local GivenVotes = Map.Votes.SurfaceGui.VotesLabel.Text
	if mostVotedMap.Votes.SurfaceGui.VotesLabel.Text > GivenVotes then
		mostVotedMap = Map
	end
end

print(mostVotedMap, "with",  mostVotedMap.Votes.SurfaceGui.VotesLabel.Text, "votes.")

When I implement that it just says that map1 is the most voted map no matter what.
image

Probably the problem is somewhere else, can u provide the whole script

I think it might be because of the [1] at the end of GetChildren(), because when i change it to [2] it says Map2 instead of Map1
image

do all maps have 0 votes? drfthbgdrfgbredghbedrfhd

No, the third one has 1 vote, the other 2 have 0.

Check if there are no errors on output.

U can still try to use tonumber(…text) on comparision.

If not that, then probably might be a problem about how ure loading these Instances up or when/how are u calling the function.

image
This gives an error.
It says that "attempt to compare number < string " on the if statement line.

dont save the votes as strings, save them as numbers
or currently you can do

if tonumber(v) > Highest then
...
HighestValue = tonumber(v)
...

Highest being…?

Shouldnt it be HighestValue?

‘’’
local HighestIndex = 0
local HighestValue = 0

for i, v in pairs(Votes) do
	
	if tonumber(v) > HighestValue then

		HighestValue = tonumber(v)
		HighestIndex = i
		
		print(HighestIndex)
		print(HighestValue)
	end
end

local MostVotedMap = MapVoting.Maps:GetChildren()[1]

for i, Map in pairs(MapVoting.Maps:GetChildren()) do
	
	local GivenVotes = Map.Votes.SurfaceGui.VotesLabel.Text
	
	if MostVotedMap.Votes.SurfaceGui.VotesLabel.Text > GivenVotes then
		MostVotedMap = Map
	end
end

print(MostVotedMap, "with",  MostVotedMap.Votes.SurfaceGui.VotesLabel.Text, "votes.")

end
‘’’

This is the current code, it always outputs “Map1 with 0 votes” no matter what

this cant be the whole code, and instead of adapting u just ctrlV what ive written. I quit helping lol

I have been adapting it, I’ve been removing certain parts and having others, I’ve been fiddling with the code for like 30 minutes now and with a combination of your code and the other persons code I’ve managed to fix it.
image

Thank you both for the help though!

local function SelectMap()
	local ChosenMap
	local HighestVotes
	for _, Map in MapVoting.Maps:GetChildren() do
		local GivenVotes = assert(tonumber(Map.Votes.SurfaceGui.VotesLabel.Text), "Invalid number")

		if not HighestVotes or GivenVotes > HighestVotes then
			HighestVotes = GivenVotes
			ChosenMap = Map
		end
	end

	print(ChosenMap, HighestVotes)
	return ChosenMap
end