How Could I Make Map Voting System Random Maps?

Hey There! I am trying to make a map voting system for my Tower Defense game and I would want a random 3 maps to be pulled out of a folder and placed into my maps frame.
image
Heres the scripts to be edited
Local Script:

local function SetupVoteGui()
	if not info.Voting.Value then 
		return
	end
	Gui.Voting.Visible = true

	
	local events = ReplicatedStorage:WaitForChild("Events")
	local voteEvent = events:WaitForChild("VoteForMap")
	local voteCountUpdate = events:WaitForChild("UpdateVoteCount")
	local maps = Gui.Voting.Maps:GetChildren()
	
	for i, button in ipairs(maps) do 
		if button:IsA("ImageButton") then 
			button.Activated:Connect(function()
				voteEvent:FireServer(button.Name)
			end)
		end
	end
	
	voteCountUpdate.OnClientEvent:Connect(function(mapScores)
		for name, voteInfo in pairs(mapScores) do
			local button = Gui.Voting.Maps:FindFirstChild(name)
			if button then 
				button.Vote.Text = #voteInfo
			end
		end
	end)
end 

Normal Script (Or server side):

function round.ToggleVoting()
	local maps = ServerStorage.Maps:GetChildren()
	for i, map in ipairs(maps) do 
		votes[map.Name] =  {}
	end
	
	info.Voting.Value = true
	
	for i=10, 1, -1 do
		info.Message.Value = "Map Voting ("..i..")"
		task.wait(1)
	end
	
	local winVote = nil
	local winScore = 0 
	for name, map in pairs(votes) do
		if #map > winScore then 
			winScore = #map
			winVote = name
		end
	end
	
	info.Voting.Value = false
	
	return winVote
end

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

local NumberOfPlayers = #game.Players:GetPlayers()

game.Players.PlayerAdded:Connect(function()
	NumberOfPlayers = #game.Players:GetPlayers()
end)

All help will be appreciated!

1 Like

Heres how to select a number of unique items in general: How to select N random items from a list (Example)

1 Like

How can I best implement it in?

You can use the example code, for example, to select 3 maps at random:

local mapnames = {"Map1", "Map2", "Map3", "Map4"}
local randomSelector = Scramble(IdentitySet(#mapnames))

local mapOption1Name = maps[randomSelector[1]]
local mapOption2Name = maps[randomSelector[2]]
local mapOption3Name = maps[randomSelector[3]]
1 Like

I’m not the best at scripting, but maybe this could help.

local Maps = [MAPS DIRECTORY]:GetChildren() --[[foo, bar, baz, qux, quux]]--
local Options = {}

for i = 1, 3 do -- Change the 3 to any amount if you want
	local Option = Maps[math.random(1, #Maps)]
	table.remove(Maps, table.find(Maps, Option))
	table.insert(Options, Option)
end

.
It chooses 3 random maps, if I were to add a print(Options), it would print a random result:

"{foo bar baz}" -- One result (maybe)
"{qux foo baz}" -- Another result (maybe)
"{bar quux qux}" -- Yet Another result (maybe)
etc.
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.