Need help with voting system [UNSOLVED]

Help me my friends are laughing at me :fearful:

  1. What do you want to achieve?
    I want to make the votes actually work

  2. What is the issue?
    The vote winner is working very poorly and I’ve no clue why

  3. What solutions have you tried so far?
    I tried to recreate my winning vote system but it doesn’t seem to work either

This is my client script (37% sure the error is there):

updateVote.OnClientEvent:Connect(function(data)
	for i, button in pairs(votingFrame:GetChildren()) do
		for x, map in pairs(data) do
			if button:IsA("TextButton") and map.order == button.LayoutOrder then
				button.MapTitle.Text = button.Name
				button.AmountOfVotes.Text = #map.players
				
				if currentVote == map.order then
					button.UIStroke.Color = Color3.fromRGB(81, 255, 69) -- changes the color
				else
					button.UIStroke.Color = Color3.fromRGB(0, 0, 0)
				end
				
				button.MouseButton1Click:Connect(function()
					if valuesFolder.VotingProgress.Value == true then
						if currentVote == nil or currentVote ~= button.LayoutOrder then
							local sfx = Instance.new("Sound")
							sfx.SoundId = "rbxassetid://4676738150"
							sfx.Volume = 0.5
							sfx.Parent = script.Parent
							sfx:Play()
							
							game.Debris:AddItem(sfx, 1)
							
							currentVote = button.LayoutOrder
							
							placeVote:FireServer(currentVote)
						end
					end
				end)
			end
		end
	end
end)

If the problem isn’t coming from my client then this is my module function:

function votingModule.MapVoting(dur) -- votes a map
	mapVotes = {}
	
	for i = timeBeforeVotes, 0, -1 do
		status.Value = "Game is starting in ".. i .." ".. ((i == 1 and "second!") or "seconds!"  )
		
		task.wait(TASK_WAIT_ONE_SECOND)
		
		if i == 0 then

			for a = 1, 3 do
				table.insert(mapVotes,{order = a;name = getMap();players = {}})
			end

			updateVotes:FireAllClients(mapVotes)

			votingProgress.Value = true

			local placeVoteConnection = placeVote.OnServerEvent:Connect(function(player, voteNumber)
				
				for i, map in pairs(mapVotes) do
					for x, plr in pairs(map.players) do
						if plr == player.UserId then
							table.remove(map.players, x)
							break
						end
					end
				end

				for i, map in pairs(mapVotes) do
					if map.order == voteNumber then
						table.insert(map.players, player.UserId)
					end
				end

				updateVotes:FireAllClients(mapVotes)
			end)

			for i = dur, 0, -1 do
				status.Value = ("You have ".. i .." ".. ((i == 1 and "second") or "seconds").. " to vote!")
				task.wait(TASK_WAIT_ONE_SECOND)
			end

			placeVoteConnection:Disconnect()

			votingProgress.Value = false

			table.sort(mapVotes, function(a, b) 
				return #a.players > #b.players
			end)

			local winner

			if #mapVotes[1].players == #mapVotes[2].players and #mapVotes[2].players == #mapVotes[3].players then
				print("this was voted 1") -- this prints if every map are equal
				winner = mapVotes[math.random(1, 3)]
			elseif #mapVotes[1].players == #mapVotes[3].players then
				print("this was voted 2") -- this prints if map 1 and map 3 are equal
				winner = mapVotes[math.random(1,3)]
				while winner == 2 do
					print("this was voted 3")
					winner = mapVotes[math.random(1,3)]
				end
			elseif #mapVotes[1].players == #mapVotes[2].players then
				print("this was voted 4") -- this prints if map 1 and map 2 are equal
				winner = mapVotes[math.random(1, 2)]
			else
				print("this was voted 5") -- this prints if only one map has the most vote
				winner = mapVotes[1]
			end 

			coroutine.wrap(statusChange)(winner)

			return replicatedStorage.Maps:FindFirstChild(winner.name)
		end
	end
end
2 Likes

Are we sure the layout orders arent all the same value…?
I mean I know in the script it does for 1-3 do order = num but did you check thats working properly

if thats not the problem then the issue would have to be in how the votes are counted and selected as “voted”, I’d assume this section is the problem

Its not picking which has the most votes, from what I can tell its just if none of them are equally voted, then its picking the 1 map

1 Like

By using the button.LayoutOrder, it can return it’s value (1 - 3) then choose the map according to it (I tested it and it works perfectly fine to know which map the player has voted)

As you said, the problem mostly comes to checking what map is the most voted and tbh I’ve no clue how to do it with my current system.

Do you have any advice?

1 Like