Voting GUI not working?

Hey y’all I’m currently working on a voting gui system and somehow it gives me this error
ServerScriptService.RoundScript:80: invalid argument #1 to 'insert' (table expected, got nil)

Code:


-- MODULE SCRIPT

local MapVotes = {}

function round.InsertMap()
	local ChoosedMap = round.TriggerChoosing()
	local MapFold = ServerStorage:WaitForChild("Maps")
	local MapModel = MapFold:FindFirstChild(ChoosedMap)
	
	workspace.BaseSpawn:Destroy()
	
	local MapNew = MapModel:Clone()
	workspace.Game.SelectedMap.Value = ChoosedMap
	MapNew.Parent = workspace
	
	return MapNew
end

function round.TriggerChoosing()
	local mapfolder = ServerStorage:WaitForChild("Maps"):GetChildren()
	local MapVotes = {}
	for i, map in ipairs(mapfolder) do
		MapVotes[map.Name] = {}
	end
	
	print("map voting started")
	task.wait(20)
	print("choosing for map now")
	
	local MapWinner = nil
	local mapScore = 0
	for name, map in pairs(MapVotes) do
		if #map > mapScore then
			mapScore = #map
			MapWinner = name
		end
	end

	return MapWinner
end

function round.ProgressVoting(player,vote)
	
	for user, voteddata in pairs(MapVotes) do
		local OldVote = table.find(MapVotes, player.UserId)
		if OldVote then
			table.remove(MapVotes, OldVote)
			print("switching ", OldVote)
			print("found an old vote", OldVote)
			break
		end
	end
	
	print("processed the vote", vote)
	
	print(player, vote)
	table.insert(MapVotes[vote], player.UserId) -- THIS LINE
	
	updateVote:FireAllClients(MapVotes)
end

VoteEvent.OnServerEvent:Connect(round.ProgressVoting)

local script:

local function VotingGuiSet()
	VotingGui.Visible = true
	
	for i, mapsbutton in pairs(VotingGui.Maps:GetChildren()) do
		if mapsbutton:IsA("ImageButton") then
			mapsbutton.Activated:Connect(function()
				VoteEvent:FireServer(mapsbutton.Name)
			end)
		end
	end
	
end

VotingGuiSet()

for the Server Script its just
local Round = requite(script.RoundModule)
local map = Round.InsertMap()

You’re inserting not to a table but a key of a table.

How would i fix it? i still had no idea

When you insert into a table, you only type the table in the first parameter, then you insert the values as the second which would be in the second parameter. You can store the values in a table and add it as the second parameter to accomplish your goal.

well the code acts basically like if the player click it then the player who clicked it is basically inserted into the table of the map name

Yeah but you’re inserting into a key, which makes it nil because it isn’t a table. Hence, you can’t actually insert anything because the purpose of table.insert is to insert into a table.

hmm alright but how would the correct code looks tho

That is what he did though – or at least in theory.

This creates empty tables with a key of the map’s name.

MapVotes[vote], if vote was a string of the name of one of the maps, would be the empty table. Here’s a visual:

table.insert(MapVotes[vote], player.UserId)
-- This would basically be:
MapVotes = {
     [vote] = {player.UserId};
}

Have you tried printing out the MapVotes table itself to see if what you’re voting for exists? The output window gives you a visual representation of a table if you print it.

I didn’t notice that. Thanks for pointing it out.

i tried printing what inside the table and it just gave me {} and 0 so i guess there’s nothing existed there, let me recheck the code again

Can you share the output of the print statement for (player, vote) as well as MapVotes?

print(player,vote) output is SHARPTHRIX Troubled Lain. The troubled lain is the map
i tried doing print(#MapVotes) and print(MapVotes) it just shows 0 and {}

Oh. I see the issue. You create a new MapVotes variable inside of the function and this is the table that the keys are added to.

function round.TriggerChoosing()
	local mapfolder = ServerStorage:WaitForChild("Maps"):GetChildren()
	local MapVotes = {} -- The culprit
	for i, map in ipairs(mapfolder) do
		MapVotes[map.Name] = {}
	end
	
	print("map voting started")
	task.wait(20)
	print("choosing for map now")
	
	local MapWinner = nil
	local mapScore = 0
	for name, map in pairs(MapVotes) do
		if #map > mapScore then
			mapScore = #map
			MapWinner = name
		end
	end

	return MapWinner
end

Just remove that and it should work as intended.

Why not do table.insert(MapVotes, {
vote,
player id}? with the correct syntax of course.

You’re trying to get the key of an empty table which returns nil.

this one works but people can votes multiple times now i’ll discover it soon
Fixed them thanks!

Well, this is meant to remove that previous vote. However:

local OldVote = table.find(MapVotes, player.UserId)

is always going to return nil. As we previously stated, MapVotes is the dictionary that stores the arrays for the maps. If you pass MapVotes as the table in the table.find() function, it will always be nil. If you do this though, it should correctly find the Id if it exists in the table:

local OldVote = table.find(voteddata, player.UserId)

-- Use this when you remove the previous vote from the array:
-- table.remove(voteddata, table.find(voteddata, player.UserId))