How to get the biggest of three values? [MAP VOTE SYSTEM]

  1. What do you want to achieve?
    I want to make a map voting system.

  2. What is the issue?
    The assigning of votes works already, but now I don’t know how to compare the votes to find out which map won / has the most votes.

  3. What solutions have you tried so far?
    I did look for solutions already, but all of them included using tables. If possible, I would like to avoid using tables.

Thanks in advance :saluting_face:

Honestly you should use tables. They are much more convenient and easy to iterate. But if you don’t want to use tables, you can access the vote values directly to compare. For convenience, I am going tag the vote values to use CollectionService to get them

local highest = 0
local winner
function DecideWinner()
  for _,voteval in ipairs(game.CollectionService:GetTagged('Votes')) do
      if voteval.Value > highest then
         highest = voteval.Value
         winner = workspace.Maps[voteval.Parent.Name] -- Assuming the value is under the map pad and the pad has the same name as the map
      end
  end
  return winner
end

Now, I know im technically using a table, but like I said earlier, this is the best way to get the winner in a voting system

1 Like

alright. Thanks for the reply, it all makes sense now, but i still have one more question.
What if its a draw between two winners?
Would it be better to replace,

if voteval.Value > highest then

with

if voteval.Value >= highest then

?
Or does the system work even if its >

You could replace it with voteval.Value >= highest, but that would select the current represented map instantly. What I did when I made my own system is this (its not very good, so proceed at own discretion)

elseif voteval.Value == highest then
   local num = math.random(1,2)
   if num == 1 then winner = workspace.Maps[voteval.Parent.Name] end
end
1 Like

thanks for all the help, i ended up setting up the table in a way that even my small brain understands it. here the result (might help people who see this later):

local milVotes = script.Parent.Parent.MilitaryMap.Votes
local hubVotes = script.Parent.Parent.HubMap.Votes
local glitchVotes = script.Parent.Parent.GlitchMap.Votes

function returnWinner()
	
	local highest = 0
	
	local winner

	local list = {
		["Military"] = milVotes.Value,
		["HubMap"] = hubVotes.Value,
		["GlitchMap"] = glitchVotes.Value
	}

	for name, votes in pairs(list) do

		if votes > highest then
			
			highest = votes
			
			winner = name
			
		end

	end
	
	return winner
	
end

game.Workspace.Game.Phases.Active.Changed:Connect(function()

	print(returnWinner())
	
end)

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