Function returns a empty table

Code is inside a module which is server sided only and it has 2 functions which should work together to pick a map. One gets all the votes and the other sets the map. The issue is that when I call the function to get the votes it returns an empty table even tho when I check the table before returning it has content inside it. Here’s the code:

local function GetVotes(voting)
	local votes = {}
	for _, player in pairs(GAME.Players) do
		local voted = player.Matchmaking['Voted'..voting]
		if voted then
			if not votes[voted] then
				votes[voted] = 0
			end
			votes[voted] += 1
		end
	end print(votes) -- this actually prints a table with content
	return votes
end

local function SelectMostVoted(voting, storage)
	local votedList = GetVotes(voting)  print(votedList)-- this prints out an empty table
end

An issue I noticed :

You need to have a table inside the parenthesis.
Change it to :

game:GetService("Players"):GetPlayers()

My bad I didn’t mention it but GAME is a module that contains a player class that has some important data for the game, it’s not actual game.Players

Solved it, the issue was [‘Voted’…voting] part, basically I used GetVotes() function on 2 different occasions (one I didn’t show because I completely forgot about it) and one was sending ‘Map’ and the other was sending ‘VotedMap’ and because of that one of them didn’t work due to such index not existing inside the table. I fixed it by removing ‘Voted’ from the function and adding it to the GetVotes() call.

Instead of putting [Solved] in the title, go to the reply which solved the problem (in this case yours), and click “Solution” at the bottom.

This will automatically highlight that reply and mark the topic as solved.

Yeah I know, don’t know if you’re allowed to mark your own post as a solution.

It’s not really for the glory, it’s to help people who may have a similar problem in the future. Don’t worry about it.

1 Like