So for context, the players vote for one of three maps. The issue is I have no way of getting the most voted map while breaking up ties.
For instance, sometimes Map1 has 3 votes, Map2 has 1 vote and Map3 also has 3 votes. This is the script I have so far.
for i,v in pairs(VotingOptions) do
if v[1].Votes == v[2].Votes then
v[2].Votes += 1
elseif v[2].Votes == v[3].Votes then
v[3].Votes += 1
elseif v[1].Votes == v[3].Votes then
v[1].Votes += 1
end
end
end
What it does is it iterates through the votes and breaks up ties. I also don’t know how to get the most voted one of them all. Also will it even break up all ties?
Where did he said math.random? He does understands some basics, like for loops, and if statements. You can just compare the 3 values together, determine what is the tie, and get rid of it.
local Votes = {
{
["Votes"] = 3,
["Name"] = "A"
},
{
["Votes"] = 2,
["Name"] = "B"
},
{
["Votes"] = 3,
["Name"] = "C"
},
}
local Highest = {}
for i, v in pairs(Votes) do
if Highest[1] == nil then Highest[1] = v continue end
if v.Votes > Highest[1].Votes then
Highest[1] = v
continue
elseif v.Votes == Highest[1].Votes and v ~= Highest[1] then
Highest[2] = v
continue
end
end
if Highest[2] == nil then
Highest = Highest[1]
else
if Highest[1].Votes == Highest[2].Votes then
Highest = Highest[math.random(1,2)]
end
end
print(Highest)
Why are you even looping if you’re still going to check all of the indexes one by one? Also the solution above most likely works but here’s a less complicated one:
table.sort(VotingOptions, function(a, b) return a.Votes > b.Votes end) -- Sort by votes
local winner = votes[1] -- Take a single most voted one (even if it's tied)