Randomized Map Voting Problems

Im trying to make a map voting GUI but it somehow turns out like this, it was working and looks perfectly when it just 1 player but it broken when it 2 players. So, I want the apearence of when it 2 Players, is the same as when it just 1 Player

When it was 1 Player:


When it was 2 Player:


Here is the codes that I used to make the Voting GUI:
- Module Script

function round.ToggleVoting()
	local mapsFolder = serverStorage.Maps:GetChildren()
	maps = {}
	votes = {}
	
	repeat
		local map = mapsFolder[math.random(1, #mapsFolder)]
		if not table.find(maps, map.Name) then
			table.insert(maps, map.Name)
		end
	until #maps == 3
	
	for i, map in ipairs(maps) do
		votes[map] = {}
	end
	
	events.SelectingMaps:FireAllClients(maps)
	info.Voting.Value = true
	print(#votes)
	
	for i = 30, 1, -1 do
		info.Massage.Value = "Map Voting In Progress - "..i
		wait(1)
	end
	
	local winVote = nil
	local winScore = 0
	
	for name, map in pairs(votes) do
		if #map > winScore then
			winVote = #map
			winVote = name
		end
	end
	
	if not winVote then
		local n = math.random(1, #maps)
		winVote = maps[n]
	end
	
	info.Voting.Value = false
	
	print(winVote)
	
	return winVote
end

function round.ProcessVote(player, vote)
	
	for i, mapVotes in pairs(votes) do
		local oldVote = table.find(mapVotes, player.UserId)
		if oldVote then
			table.remove(mapVotes, oldVote)
			break
		end
	end
	
	table.insert(votes[vote], player.UserId)
	
	print(vote)
	events:WaitForChild("UpdateVoteCount"):FireAllClients(votes)
end
events:WaitForChild("CastMapVote").OnServerEvent:Connect(round.ProcessVote)

- Local Script

function SetupVoteGUI()
	if not info.Voting.Value then
		return
	end
	
	gui.VotingFrame.Visible = true
	
	local events = replicatedStorage:WaitForChild("Events")
	local voteEvent = events:WaitForChild("CastMapVote")
	local voteCountUpdate = events:WaitForChild("UpdateVoteCount")

	events.SelectingMaps.OnClientEvent:Connect(function(maps)
		if maps then
			for i, map in pairs(maps) do
				
				local votingButton = gui.VotingFrame.MapList.Template:Clone()
				votingButton.Name = map
				votingButton.Parent = gui.VotingFrame.MapList
				votingButton.MapName.Text = map
				votingButton.Visible = true
				
				votingButton.Activated:Connect(function()
					voteEvent:FireServer(map)
				end)
			end	
		end
	end)
		
	voteCountUpdate.OnClientEvent:Connect(function(mapScore)
		for name, voteInfo in pairs(mapScore) do
			print(name)
			local button = gui.VotingFrame.MapList:FindFirstChild(name)
			if button then
				button.VoteNumber.Text = #voteInfo 
			end
		end
	end)
end

Thank You :slight_smile:

1 Like

I’m going to have to guess that the table of maps that is being passed to the client is duplicating.

Your maps and votes variables are shared variables which is causing this issue.

You could place these variables outside of the function scope and clear it once voting is complete so it doesn’t duplicate the maps.

local maps = {}
local votes = {}

-- The rest of your module

Also Instead of a repeat loop, you can do:

for i = 0, 3, 1 do -- 3 is maximum number of maps being chosen
	local randomMap = mapsFolder[math.random(1, #mapsFolder)]
	table.insert(maps, randomMap.Name)
end
2 Likes

Thanks for the help, but it doesn’t work. I tried moving the maps and votes tables variable out from the function and its still apears 2 times

Once voting is finished, you need to clear the table so the next time voting happens you don’t add on to the existing values in the table.

1 Like

I’m terribly sorry to sort of abandoned you for 19 days, I’m just so busy right now with school.

Anyways, the voting happens for once and at start of the game(the player got teleported to the game) and I think it’s effects to the players count, so when it’s more than 1 player, the voting script got messed up.

Thank you for trying to help me

Since it’s specifically for multiplayer that this happens, I’d assume the LocalScripts are both doing something that adds to the map voting display when only the one for the specific player is supposed to work