Need help with my voting system

I get invalid argument #1 to ‘insert’ (table expected, got nil) when i run my game. I get it the line highlighted with ////. Been switching things around i keep getting the same or similar error.

Heres the script

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

local events = ReplicatedStorage:WaitForChild("Events")


local info = workspace.Info
local voting = {}
local votes = {}

function voting.ToggleVoting()
	local maps = ServerStorage.Mode:GetChildren()
	votes = {}
	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

	if not winVote then
		local n = math.random(#maps)
		winVote = maps[n].Name
	end

	info.Voting.Value = false

	return winVote
end

function voting.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(voting.ProcessVote)





return voting

Replace this line with

if votes[vote] == nil then
	votes[vote] = {}
end
table.insert(votes[vote], player.UserId)

It should work fine

1 Like

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