How would you calculate the highest vote

Ok so I’m a little bit confused on how to figure out which map has the most votes, It will have a map vote and 3 options, and I think this is easier than I’m making it

I tried something like this

        if votes1 > votes2 then
			if votes1 > votes3 then
				--votes1 wins
			end
			if votes1 == votes3 then
				--votes1 and 3 tie
			end
		end
		if votes2 > votes1 then
			if votes2 > votes3 then
				--votes2 win
			end
			if votes2 == votes3 then
				--vote2 and 3 tie
			end
		end
--etc

but I’m sure there is an easier (or more proper) way to do this, please if you know anything, let me know
Sinceley, @Joesiuh/@ENTMT

Something like this should work:

local votes = {
	--map vote totals in a dictionary like this
	["SomeMap"] = 2,
	["AnotherMap"] = 1,
	["Anotha"] = 6
}

local currentMostCount = 0
local currentMostName = nil

for name, count in pairs(votes) do
	if (count >= currentMostCount) then
		currentMostCount = count
		currentMostName = name
	end
end

print(("The winner is %s with %d votes!"):format(currentMostName, currentMostCount))
3 Likes

Hi!
So I think you will want to use operators here. Maybe something like this;
if votes1 > votes2 and votes3 then
–votes1 wins
–votes1 wins
end
(sorry for bad formatting, I don’t know how to fix it)

1 Like

Thankyou, I just have one question though, I’ve known

       for i, v in pairs(#something) do
               
       end

and then I can say if “v” is = to something then do this or whatever but what does “i” do/mean?

i and v are basically variables and you can actually name them whatever you like. The first variable, in this case i (which stands for index) is the index or key of the current element you are on in the table. The second variable, in this case v (which stands for value) is the value of that element in the table.

Another common naming scheme for ipairs/pairs you might see is k and v (key and value). Index usually means a number and key usually means a string (text).

Run this example code to see the differences:

local tableOfNumbers = {1, 2, 3, 7, 100, 1337}

for i, v in ipairs(tableOfNumbers) do
	print(i, v)
end

local dictionaryOfValues = {
	MyFavoriteNumber = 99,
	Hello = "World",
	Cool = true
}

for k, v in pairs(dictionaryOfValues) do
	print(k, v)
end
1 Like

Something to keep in mind is that this solution will consistently choose the later options in the dictionary when there is a tie.

If map 1 and 3 have the same amount of votes, this solution will pick map 3.

I’ve worked on vote systems before and some people get really annoyed by this. To make it randomly choose from tied options (this probably isn’t the best solution out there but it’s all I can think of) you’d have to: create a new table and then fill it with all the maps that had an equal amount of votes to currentMostCount. Then you can pick a random number from 1 to the length of the table and then print out the map at that index as the winner.

by the way I can link my vote system model if anybody here wants to see it. the code is from when I was still kinda new to scripting so most of it is really bad, but it works and there could be some useful stuff in there.

3 Likes

This is correct. My solution is only naive and does not handle ties in a graceful way. A more robust system can be designed to pick the map which had the most votes first or simply pick randomly like you said.

The solution you described is probably perfectly fine to do. There is other ways you can do it that would have different time or space complexities (like not having to create two tables), but I don’t think optimizations like that would really be worth the extra time and decreased code readability in this scenario :stuck_out_tongue:

1 Like

Better than that, something like this. The first map to reach a certain amount of votes will always win, I feel like that’s probably what would like and expect.

--\\ this is pseudo code, extremely pseudo code

local votes = {
    Map1 = 0,
    Map2 = 0,
    Map3 = 0
}
local highestVote = votes.Map1 -- This would be randomized

local function Vote(player, map)
    --\\ logic to see if the player has already voted, we skipping over that here.
    --\\ the maps votes would probably be arrays with players in this case;

    votes[map] += 1
    if votes[map] > votes[highestVote] then
        highestVote = map
    end
end

highestVote = votes[highestVote]`

There’s a lot more logic like people switching votes, other things like that which you would have to do with in this case; I’m not doing this here as I’m tired but you get the point

2 Likes