Map Voter Module [Open Sourced Example]

A module for easier map voting.

Module Source:

local Voter = {}

--> Local Variables
local Maps = nil

--> Functions
function Voter.SetMapFolder(MapFolder)
	Maps = MapFolder
end

function Voter.StartVote()
	local PickedMaps = {}
	local CurrentQueue = {
		[1] = {
			Map = nil,
			Votes = {},
		},
		[2] = {
			Map = nil,
			Votes = {},
		},
		[3] = {
			Map = nil,
			Votes = {},
		}
	}
	
	repeat
		local randomI = math.random(1, #Maps:GetChildren())
		for i, v in pairs(Maps:GetChildren()) do
			if i == randomI then
				if table.find(PickedMaps, v) == nil then
					table.insert(PickedMaps, v)
				end
			end
		end
		task.wait()
	until #PickedMaps == 3
	
	CurrentQueue[1].Map = PickedMaps[1]
	CurrentQueue[2].Map = PickedMaps[2]
	CurrentQueue[3].Map = PickedMaps[3]
	
	setmetatable(CurrentQueue, {__index = Voter})
	return CurrentQueue
end


function Voter:AddVote(player, Map): number
	local TotalVotes = 0
	local AlreadyVotedForIndex = nil
	local Voted = false
	
	for i, v in pairs(self) do
		if typeof(v) == "table" then
			if v["Map"] ~= nil then
				if v["Map"] == Map then
					if table.find(self[i].Votes, player.Name) ~= nil then
						AlreadyVotedForIndex = i
					end
				end
			end
		end
	end
	
	for i, v in pairs(self) do
		if typeof(v) == "table" then
			if v["Map"] ~= nil then
				if v["Map"] == Map then
					if AlreadyVotedForIndex == nil then
						Voted = true
						table.insert(self[i].Votes, player.Name)
						TotalVotes = #self[i].Votes
					elseif (Voted == false) and (AlreadyVotedForIndex ~= nil) and (AlreadyVotedForIndex ~= i) then
						local FoundIndex = table.find(self[AlreadyVotedForIndex].Votes, player.Name)
						if FoundIndex ~= nil then
							table.remove(self[AlreadyVotedForIndex].Votes, FoundIndex)
						end
						
						Voted = true
						table.insert(self[i].Votes, player.Name)
						TotalVotes = #self[i].Votes
					end
				end
			end
		end
	end
	
	return TotalVotes
end

function Voter:RemoveVote(player, Map): number
	local TotalVotes = 0
	
	for i, v in pairs(self) do
		if typeof(v) == "table" then
			if v["Map"] ~= nil then
				if v["Map"] == Map then
					local FoundIndex = table.find(self[i].Votes, player.Name)
					
					if FoundIndex ~= nil then
						table.remove(self[i].Votes, FoundIndex)
						TotalVotes = #self[i].Votes
					end
				end
			end
		end
	end
	
	return TotalVotes
end

function Voter:GetVotedMap()
	local VotedMap = nil
	local VotedTotal = 0
	
	for i, v in pairs(self) do
		if typeof(v) == "table" then
			if v.Map ~= nil then
				if #v.Votes > VotedTotal then
					VotedMap = v.Map
					VotedTotal = #v.Votes
				elseif #v.Votes == VotedTotal then
					local Rand = math.random(1,2)
					if Rand == 2 then
						VotedMap = v.Map
						VotedTotal = #v.Votes
					end
				end
			end
		end
	end
	
	return {VotedMap, VotedTotal}
end

function Voter:Over()
	self = nil
end

return Voter

Example Game:

Documentation:

--> 1. Requiring The Module
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Module = require(ReplicatedStorage:WaitForChild("VoterModule"))

--> 2. Setting The Map Folder
local Maps = ReplicatedStorage:WaitForChild("Maps")

Module.SetMapFolder(Maps)

--> 3. Starting The Vote
local CurrentVoting = Module.StartVote()

--> 4. Adding Votes
CurrentVoting:AddVote(player, Map)

--> 5. Removing Votes
CurrentVoting:RemoveVote(player, Map)

--> 6. Getting The Voted Map
local VotedMap = CurrentVoting:GetVotedMap()
local MapObject = VotedMap[1]
local TotalVotes = VotedMap[2]

--> 7. Clearing the current vote
CurrentVoting:Over()
CurrentVoting = nil
3 Likes

any videos so we don’t go to the example game

Hey bro def gonna use in the future for sure.