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
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.
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.
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.
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.")
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
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.
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