Map Voting Script Not Working

I followed Alvinblox’s youtube tutorial on making a map voting script, but it is not working. The gui setup and voting itself works, but when it calculates the winner it might pick the wrong one sometimes, but others it is correct. This happens if I do a test with multiple players too. I have gone through my script, but I cannot find the problem. I am very new to scripting, so if it is possible to make it easily understandable, please do. Any help would be appreciated!

The script:

--Variables on server--
local plates = game.Workspace:WaitForChild("Plates")
local maps = {"Monster", "Clash"}
local voteTime = game.ReplicatedStorage.voteTime

local connections = {}
local votes = {}

local function choosePlate()
	local plateTable = {game.Workspace.Plates.Monster, game.Workspace.Plates.Clash}
	for i,v in pairs(plateTable) do
		if v:FindFirstChild("InUse") then
			if v.InUse.Value == false then
				v.InUse.Value = true
				return v
			end
		end
	end
	return nil
end

local function setupPlate(mapName)
	--Pick a plate not already in use
	local plate = choosePlate()
	votes[mapName] = {}
	--Set up viewport for that plate
	game.ReplicatedStorage.SendMapChoices:FireAllClients(plate,mapName)
	--Touched event that adds a vote
	connections[#connections+1] = plate.Touched:Connect(function(hit)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		if plr then
			--Remove votes for this player from other plates
			for plateName, plateTable in pairs(votes) do
				for player, playerValue in pairs(plateTable) do
					if player.Name == plr.Name then
						plateTable[player] = nil
					end
				end
			end
			votes[mapName][plr] = true
		end
	end)
	--Store votes in a table
end

local function startVote()
	voteTime.Value = 20
	local chosenMaps = maps
	
	for i,v in pairs(chosenMaps) do
		setupPlate(v)
	end
	
	for i = 20, 0, -0.1 do
		for mapName, mapTable in pairs(votes) do
			local count = 0
			for playerName, value in pairs(mapTable) do
				count += 1
			end
			game.ReplicatedStorage.UpdateMapVotes:FireAllClients(mapName, count, i)
		end
		task.wait(0.1)
	end
	
	--Find the map with the most votes
	local highestVoteCount = 0
	local highestVoteMapNames = {}
	local winningMapName = ""
	
	for mapName, mapTable in pairs(votes) do
		local count = 0
		for playerName, value in pairs(mapTable) do
			count += 1
		end
		
		if count >= highestVoteCount then
			highestVoteCount = count
			table.insert(highestVoteMapNames,mapName)
		end
	end
	
	if #highestVoteMapNames > 1 then
		winningMapName = highestVoteMapNames[math.random(1, #highestVoteMapNames)]
	else
		winningMapName = highestVoteMapNames[1]
	end
	
	print("Chosen map via vote: "..winningMapName)
	print("Loading map "..winningMapName)
	
	game.ReplicatedStorage.Announce:FireAllClients("Next Map: "..winningMapName) --Shows a gui saying the winner
	if winningMapName == "Monster" then
		game.ReplicatedStorage.MapWonEvent.MonsterChosen:FireAllClients() --Plays the Monster song
	elseif winningMapName == "Clash" then
		game.ReplicatedStorage.MapWonEvent.ClashChosen:FireAllClients() -- Plays the Clash song
	end
	game.ReplicatedStorage.ClearMapChoices:FireAllClients() -- Hides the Guis on the plates
	for i,v in pairs(connections) do
		if v then
			v:Disconnect()
		end
	end
	
	for i,v in pairs(plates:GetChildren()) do
		v.InUse.Value = false
	end
	
	votes = {}
end

wait(1)
startVote()

I think I see a problem with this code, if a map overtakes another one in votes it’s still in the table containing the “top voted maps” so there would be a random chance then that it picked a lower voted map because of it. I’d recommend to clear the table if a higher voted map has been found.

2 Likes