Store table value in a variable

Hello all! I am currently trying to create a system that compares 3 numerical values in a table, then gets the biggest value of them all then stores it in a variable. I will show you the code here:

local tally1 = 1
local tally2 = 2
local tally3 = 2

local tallywinner

local map1 = "Obby"
local map2 = "House"
local map3 = "Volcano"

local winnertable = {
	{map1, tally1};
	{map2, tally2};
	{map3, tally3}
}
		
table.sort(winnertable, function(i, j)
	return i[2] > j[2]
end)

Essentially what I want to accomplish is to take tally1 from the sorted winnertable, then put it in a variable. However, I haven’t figured out a way to do this. Any solutions to my problem are greatly appreciated, thanks!

1 Like

This creates a dummy table called sorted and then sorts it, then the first index will always be the map with the most votes

local UserInputService = game:GetService("UserInputService")
local Variables = require(game.ReplicatedStorage.Common.Variables)

local tally1 = 5
local tally2 = 2
local tally3 = 1

local tallywinner

local map1 = "Obby"
local map2 = "House"
local map3 = "Volcano"

local winnertable = {
   ["map1"] = tally1,
   ["map2"] = tally2,
   ["map3"] = tally3
}

local sorted = {}

for mapName, AmountOfTallies in pairs(winnertable) do
   table.insert(sorted, {mapName = mapName, AmountOfTallies = AmountOfTallies})
end
		
table.sort(sorted, function(a,b)
	return a.AmountOfTallies > b.AmountOfTallies
end)

-- The first index of the sorted will hold the information for the map with the most tallies and them mapname
print(sorted[1].AmountOfTallies)

Output
image

1 Like

Thank you very much for this solution!

1 Like

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