Random Map Chooser to Vote Map Chooser

Hi everyone. I’m making a game where a map is chosen. Currently the map is randomly chosen, (I followed a youtube tutorial for this) but I wanted to integrate voting into this. I have made the Gui, and here it is:

This is the explorer (in case you provide code):
image

And this is the script for randomly choosing a map:

local lobby = game.Workspace.Lobby
local maps = game.ReplicatedStorage.Maps:GetChildren()
local status = game.ReplicatedStorage.Status

while true do
	for i = 20, 0, -1 do
		status.Value = "Intermission: "..i
		task.wait(1)
	end

	local chosenmap = maps[math.random(1,#maps)]
	local clonedmap = chosenmap:Clone()
	clonedmap.Parent = game.Workspace
	status.Value = "The map is "..clonedmap.Name
	
	task.wait(3)
	
	for i, Player in pairs(game.Players:GetPlayers()) do
		
		local char = Player.Character
		
		if char then
			local humroot = char.HumanoidRootPart
			humroot.CFrame = clonedmap.TeleportPoint.CFrame
		end
	end


	for i = 10, 0, -1 do
		status.Value = i
		task.wait(1)
	end

for i, Player in pairs(game.Players:GetPlayers()) do
	local char = Player.Character
	
		if char then
			local humroot = char.HumanoidRootPart
			humroot.CFrame = lobby.TeleportPoint.CFrame
		end
	end
	
	clonedmap:Destroy()
	
end


This is a local script that the tutorial made me write, but isn’t necessarily to do with the random map choosing:

local status = game.ReplicatedStorage:WaitForChild("Status")
local label = script.Parent.TextLabel

status:GetPropertyChangedSignal("Value"):Connect(function()
	label.Text = status.Value
end)

I want to change this from random to voting. Does anyone know how I could do this, and if possible provide a script? I am aware that this could possibly be an inconvenience to help me with, and that’s why I truly thank anyone who can help me out here. Thanks! :slight_smile:

2 Likes

I should make a table playervotes and check if there are votes for a map if not so, just let the game decide, put eveyrthing in separate functions.

local maps = game.ReplicatedStorage.Maps:GetChildren()
local status = game.ReplicatedStorage.Status

local option1votes = {}
local option2votes = {}
local option3votes = {}

I have added tables for each option. If a player clicks ‘vote’, how can I add them to the table from a local script?

Fire a remote event, check if the player has voted (if they are in any of the lists), if not, add them in. If so, change the option if its a dfiffernt one

Fire remote also, do you have 3 maps or more? Because you still need 3 maps and just add votes to 1 table with the maps name or something

Assuming you have a function named ‘LoadMap’, you could try this code:
On the server

local VoteRemote = game.ReplicatedStorage.Vote
local Maps = {[1] = '', [2] = '', [3] = ''}
local Voted = {}
local Metatable = {}
function Metatable.__index() return 0 end
local Votes = setmetatable({}, Metatable)
VoteRemote.OnServerEvent:Connect(function(Player, Map)
	if not Voted[Player] then
		Voted[Player] = true
		Votes[Map] += 1
	end
end)

task.wait(10)

local ChosenMap, TotalCount = 0
for Map, Count in pairs(Votes) do
	if Maps[Map] then --Check that the player did not make an illegal vote through other means like cheating
		if Count > TotalCount then
			TotalCount = Count
			ChosenMap = Map
		end
	end
end
LoadMap(Maps[ChosenMap])

On the client:

Button.Activated:Connect(function()
	game.ReplicatedStorage.Vote:FireServer(Map) --Number corresponding to button
end)

Make sure you have a remoteevent named ‘Vote’ parented to replicatedstoarage