A simple way would be to
- determine the biggest value
- determine the first of the values that has the biggest
If there are only three, you could implement it something like this:
local most = math.max( one.Value, two.Value, three.Value )
if most == one.Value then
elseif most == two.Value then
else
end
It would probably be better to use a list, however.
local values = {one, two, three}
local largest = -1
for _, value in pairs(values) do
largest = math.max(largest, value.Value)
end
for i, value in pairs(values) do
if largest == value.Value then
end
end
This has a deficiency, though, in that if there is a tie it will always pick the "first" one.
We should probably pick randomly among all the maximums:
local values = {one, two, three}
local largest = {}
local best = -1
for _, value in pairs(values) do
local v = value.Value
if v > best then
largest = {value}
elseif v == best then
table.insert(largest, value)
end
end
local choice = largest[math.random(#largest)]
A fairer approach
For map selection, though, I think picking only the plurality is unfair. Imagine there are three people playing a game, two of them are friends. The two friends are talking to each other and always want to play the same map as each other. So whatever the third person votes for doesn't matter.
Thus 33% of the people never get a say in the vote.
It would be fairer if everyone could count.
In that case, we want a weighted selection.
local one = workspace.DumbGame
local two = workspace.WonderfulGame
local values = {one, two, workspace.OtherChoice, workspace.FunGame}
local totalWeight = 0
for _, value in pairs(values) do
totalWeight = totalWeight + value.Value
end
local stop = math.random() * totalWeight
local choice, choiceName
for i, value in pairs(values) do
stop = stop - value.Value
if stop <= 0 then
choice = value
choiceIndex = i
choiceName = value.Name
break
end
end
if choice == one then
elseif choice == values[2] then
elseif choiceIndex == 3 then
elseif choiceName == "FunGame" then
This last one is harder to see that it's correct. Here is how it works:
[ 10 ][ 4 ][ 2][ 20 ]
[ 28% ][ 11% ][ 6% ][ 55% ]
0% [one] 28% [two] 39% [three] 45% [four] 100%
3 comments
I'm not the best scripter, although other ways to achieve this is appreciated.
I really didn't think I'd get that many answers, thank you all allot.
Also I forgot a bool values is true or false, I corrected.