Hello everyone, so I’m having difficulty trying to find tied values inside the map selection dictionary I made.
I managed to find which map got the most votes but I’m struggling to find out if some maps have tied votes.
For example, let’s say 3 people voted for 3 different maps inside the mapVotes table.
Map1 = 1 vote
Map2 = 1 vote
Map3 = 1 vote
They all have 1 vote and they’re basically tied.
I want to be able to determine if there’s a tie like this, then take those maps that have tied scores and choose a random one between those maps.
So if map1, 2, and 3 have tied votes, those 3 maps get put into a table perhaps, and a random one gets selected, maps 4 and 5 are not included.
I also want to try to put it into a function so that this tied system can work not only for 3 maps tied but any number of maps tied.
For example, there are a total of 10 maps, and x are tied in votes, it will only take those x amounts of maps and choose a random one between those tied.
I appreciate any help and comments! Thank you.
local mapVotes = {
Map1= 0,
Map2 = 0,
Map3 = 0,
Map4 = 0,
Map5 = 0
}
local function mapVoteSelect()
local mapSelected = "name"
local winnerMap = false
local tiedMap = false
local highestNumber = 0
for key, value in pairs(mapVotes) do
if value > highestNumber then
mapSelected = key
winnerMap = true
end
end
if winnerMap then
chosenmap.Value = mapSelected
print(chosenmap.Value, "WON THE VOTE")
return true
else
return false
end
-- not sure how to figure out if maps are tied in votes
if tiedMap then
-- tied maps get randomly chosen here
end
end
Perhaps making the code to loop through the mapVotes table again after getting the highest votes, this time checking if the amount of votes is equal to the highest amount of votes. If it is, then insert the map name into a table. You can then select a random map from that table by doing tableName[math.random(1,#tableName)] . Let me know how this goes or if you need any help!
I already understand how to get a random value, my only issue is finding repeating values inside my dictionary, then I can take those and insert it into a new table and use math.random with the length of the table.
Try doing this after the highest amount of votes is determined:
local tiedVotes = {}
for mapName,votes in pairs(mapVotes) do
if votes == highestNumber then
table.insert(tiedVotes,mapName)
end
end
mapSelected = tiedVotes[math.random(1,#tiedVotes)]
local MapVotes = {
map1 = 1,
map2 = 2,
map3 = 3,
}
local function getWinningMaps(map_votes) --> [array] returns array of all the maps with the highest votes
local maps_ordered_by_votes = {}
local highest_vote = 0
for map,vote_number in pairs(map_votes) do
-- keeping track of the highest vote
if vote_number > highest_vote then
highest_vote = vote_number
end
-- keeping track of which maps have which vote
if maps_ordered_by_votes[vote_number] == nil then
maps_ordered_by_votes[vote_number] = {}
end
table.insert(maps_ordered_by_votes[vote_number],map)
end
return maps_ordered_by_votes[highest_vote]
end
-- you could do whatever you want here really
-- here's an example
local selected_map = nil
local maps_that_won = getWinningMaps(MapVotes)
if #maps_that_won > 1 then
selected_map = maps_that_won(math.random(1,#maps_that_won))
else
selected_map = maps_that_won[1]
end
print(selected_map)
I think I explained it a bit weird; you only have to generate one number between x and y, then choose the map based on the number.
For example if I had 5 maps, and they were in this order: 2 votes, 1 vote, 2 votes, 1 vote, 2 votes. You insert the tied maps (1, 3, and 5) into a new table of tied maps. This makes the new indices 1, 2, 3. Then you pick your random number and choose the map that it happens to be.