Help On Map Voting System

Hello!
I am currently trying to make a Round System where players can vote for different maps in my game however the client doesn’t receive the data I send from the server as seen in the output below:

, If someone would like to review my code here it is below:

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local VoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("RemoteEvents"):WaitForChild("VoteEvent")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local MapVotes = {}
local MapChoices = {}


local function chooseMapsForVoting()
	local allMaps = MapsFolder:GetChildren()
	MapChoices = {}
	MapVotes = {}
	if #allMaps < 3 then
		warn("Not enough maps in MapsFolder for voting")
		return
	end
	for i = 1, 3 do
		local randomMap = allMaps[math.random(1, #allMaps)]
		if randomMap then
			table.insert(MapChoices, randomMap)
			MapVotes[randomMap.Name] = 0
		else
			warn("Random map selection failed. Map is nil.")
		end
	end
end


local function startVoting()
	chooseMapsForVoting()
	if #MapChoices < 3 then
		warn("Not enough valid map choices for voting")
		return
	end
	print("Sending map choices to clients: ", MapChoices)
	VoteEvent:FireAllClients(MapChoices)
end


while true do
	startVoting()
	task.wait(10)
end

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("RemoteEvents"):WaitForChild("VoteEvent")  -- Ensure the VoteEvent exists

local VotingGui = script.Parent.VotingGui
local Button1 = VotingGui:WaitForChild("Map1Button")
local Button2 = VotingGui:WaitForChild("Map2Button")
local Button3 = VotingGui:WaitForChild("Map3Button")

local function startVoting(mapChoices)
	VotingGui.Visible = true 
	print("Client Has Received:")
	print(mapChoices)
	if not mapChoices or #mapChoices < 3 then
		warn("Invalid map choices received from server")
		VotingGui.Visible = false
		return
	end

	if mapChoices[1] then
		Button1.Text = mapChoices[1].Name
	else
		warn("Map1 is nil")
	end

	if mapChoices[2] then
		Button2.Text = mapChoices[2].Name
	else
		warn("Map2 is nil")
	end

	if mapChoices[3] then
		Button3.Text = mapChoices[3].Name
	else
		warn("Map3 is nil")
	end

	Button1.MouseButton1Click:Connect(function()
		if mapChoices[1] then
			VoteEvent:FireServer(mapChoices[1].Name)
		end
		VotingGui.Visible = false 
	end)
	Button2.MouseButton1Click:Connect(function()
		if mapChoices[2] then
			VoteEvent:FireServer(mapChoices[2].Name)
		end
		VotingGui.Visible = false
	end)
	Button3.MouseButton1Click:Connect(function()
		if mapChoices[3] then
			VoteEvent:FireServer(mapChoices[3].Name)
		end
		VotingGui.Visible = false
	end)
end

-- Listen for the server to send voting options
VoteEvent.OnClientEvent:Connect(function(mapChoices)
	startVoting(mapChoices) 
end)

Any Comments would be kindly appreciated :smiley:

You could use my friends voting module

hey there, when you send in a table from a remote event, it wont exactly work, you have to turn it into dictionary or entirely numeric indices

(note: i have not tested it but i will soon, i will get back to you if this following statement is true or not)

oh hey, i just came back and turns out i was completely wrong, it looks like your maps are in the serverstorage, put the to replicated storage and you should be good to go (if you need me to elaborate WHY putting them to serverstorage makes the array not have any thing, please reply)

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