Voting system not working

Hello guys! I’m currently having problems with the voting system that was made by my friend.

When the voting starts, the voting doesnt appears, and this appears in the dev console:

And yes, I did check if MapPictureID exists.
I don’t know but I think that just happens in the experience in Roblox, Not on Roblox studio.

Script that gave the error:

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Maps = ServerStorage:WaitForChild('Maps'):GetChildren()
local VoteEvents = ReplicatedStorage.VoteEvents
local Status = ReplicatedStorage:WaitForChild('Status')

local Votes = {}

local function GetMapModelWithMapDataMap(MapData)
	print(MapData)
	local Map = ServerStorage.Maps:FindFirstChild(MapData.Name)
	return Map
end

local function GetMapWithMoreVotes()
	print(Votes)

	-- Returns the map with the most votes, or a random one if all are 0
	local MapWithMoreVotes = nil
	local HighestVotes = 0
	for i, Map in Votes do
		local VotesValue = Map.Votes
		if VotesValue and VotesValue > HighestVotes then
			MapWithMoreVotes = Map
			HighestVotes = VotesValue
		end
	end

	-- If all maps have 0 votes or no votes were cast, pick a random map from Votes
	if MapWithMoreVotes == nil then
		MapWithMoreVotes = Votes[math.random(1, 3)] -- 3 (max maps)
	end
	return MapWithMoreVotes
end

local function ConvertToRandomMapsData(RandomMaps)
	local RandomMapsData = {}

	for i, Map in ipairs(RandomMaps) do
		if Map and Map.Votes and Map.MapPictureID then
			table.insert(RandomMapsData, {Name = Map.Name, Votes = Map.Votes.Value, MapPictureID = Map.MapPictureID.Value})
		else
			return
		end
	end

	Votes = RandomMapsData
	return RandomMapsData
end

VoteEvents.PlayerVote.OnServerEvent:Connect(function(PlayerWhoVoted, MapName, index)
	local MapData = Votes[index]
	MapData.Votes = MapData.Votes + 1
	VoteEvents.UpdateVoteCount:FireAllClients(MapName, MapData)
	print(MapData)
end)

VoteEvents.RemoveVoteCount.OnServerEvent:Connect(function(Player, index)
	local MapData = Votes[index]
	MapData.Votes = MapData.Votes - 1
end)

while true do
	-- Clean up any leftover "Votes" NumberValues from all maps before starting a new round
	for i, map in Maps do
		local oldVotes = map:FindFirstChild("Votes")
		if oldVotes then
			oldVotes:Destroy()
		end
	end

	Votes = {} -- Reset the Votes

	-- Intermission
	local Countdown = 15
	workspace.music.SoundId = "rbxassetid://131736922566985"
	workspace.music:Play()
	Status.Parent.Map.Value = ""

	repeat task.wait(1)
		Countdown = Countdown - 1
		Status.Value = 'Intermission: '..Countdown
	until Countdown <= 0

	-- Vote Time
	Status.Value = 'Voting Time'

	local RandomMaps = {}
	for i = 1, 3 do
		local RandomMap = Maps[math.random(1,#Maps)]
		while table.find(RandomMaps, RandomMap) do
			RandomMap = Maps[math.random(1,#Maps)]
		end

		local NumberValue = Instance.new("NumberValue", RandomMap)
		NumberValue.Name = "Votes"
		table.insert(RandomMaps, RandomMap)
	end
	local RandomMapsData = ConvertToRandomMapsData(RandomMaps)
	print(RandomMapsData) -- Print the RandomMapsData Table
	VoteEvents.VoteTime:FireAllClients(RandomMapsData)

	task.wait(15) -- Voting Time

	VoteEvents.VoteEnd:FireAllClients()
	local MapWithMoreVotes = GetMapWithMoreVotes()
	local ChosenMap = GetMapModelWithMapDataMap(MapWithMoreVotes)

	-- Load Map
	task.wait(1)

	ChosenMap.Parent = workspace
	Status.Value = 'Selected map: '..ChosenMap.Name
	Status.Parent.Map.Value = ChosenMap.Name

	task.wait(2)

	Countdown = 171 -- Game Time
	workspace.music.SoundId = "rbxassetid://78753868115672"
	workspace.music:Play()

	repeat task.wait(1)
		Countdown = Countdown - 1
		Status.Value = 'Time left: '..Countdown
	until Countdown <= 0

	-- Kill the players
	for i, Player in pairs(Players:GetPlayers()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			Player.Character.Humanoid:TakeDamage(2000)
		end
	end

	ChosenMap:Destroy()

	-- Clean up "Votes" NumberValues from all maps after the round
	for i, map in Maps do
		local oldVotes = map:FindFirstChild("Votes")
		if oldVotes then
			oldVotes:Destroy()
		end
	end

	Status.Value = 'da round ended now wait for intermission start >:('

	task.wait(1) -- little pause, make this as long as you want.
end

One of your models “Rocket arena” does not contain a child called MapPictureID, that’s why its throwing that error because the for loop assumes it should have it when trying to access the value.

I’m assuming you’ve checked that already and it does. What I would do is either, if not spawned straight away then use :WaitForChild() otherwise if you expect to have it use :FindFirstChild()

Use Map:FindFirstChild("MapPictureID") instead.

If you reference it directly with the possibility that it could be a nil value, then it will error. With the :FindFirstChild() method, instead of erroring should the object be nil, it returns nil instead, or the object itself if it isn’t nil at all.

So, in this case, this is what your for loop value should turn into:

	for i, Map in ipairs(RandomMaps) do
		if Map:FindFirstChild("Votes") and Map:FindFirstChild("MapPictureID") then
			table.insert(RandomMapsData, {Name = Map.Name, Votes = Map.Votes.Value, MapPictureID = Map.MapPictureID.Value})
		else
			return
		end
	end

It’ll move to the else statement if not all conditions are met (Map.Votes is nil, or MapPictureID is nil…)

Lastly, ipairs and pairs are no longer necessary. You can just do for i, Map in RandomMaps do and it will work just the same. if Map isn’t necessary either because if the ‘RandomMaps’ table has no values, then it won’t loop through anything

Ok, Ima test that script later, I need to go now :((

1 Like

Hey, I changed the script, I can’t really tell if it’s fixed, because, I think the bug only happens in public servers (on Roblox, not on Play test)

(I’m going to test on public servers, if it works I come back and mark your post as a solution.)

In the second voting, it showed this on console: “Players.ota2407.PlayerGui.VoteGui.VoteScriptClient:54: attempt to iterate over a nil value”

Edit: I’m removing the voting system, because it’s breaking the entire game, but thx by your help everyone!

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