Argument 1 missing or nil

I want to achieve to load a map when voted on it. Everything works fine but it errors on line 73 when I don’t pick a map.

This is the part where it errors:

function round.LoadMap()
	local votedMap = round.ToggleVoting()
	local mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
	if not mapFolder then
		mapFolder = ServerStorage.Maps.WheatFields
	end
	
	local newMap = mapFolder:Clone()
	newMap.Parent = workspace.Map

Not sure if it has to do something with FindFirstChild()

I think what it could be is that votedMap is returning nil, may we see ToggleVoting?

function round.ToggleVoting()
	local maps = ServerStorage.Maps:GetChildren()
	votes = {}
	for i, map in ipairs(maps) do
		votes[map.Name] = {}
	end
	
	info.Voting.Value = true
	
	for i=10, 0, -1 do
		info.VotingTime.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
	
	info.Voting.Value = false
	
	return winVote
end

Which line is line 73? What does the error say?
Edit: nvm just watched the video

From what I see, Each key value pair just has an empty table, nothing is added to the tables of each key, therefore #map is 0, so the condition is never met because 0 is not greater than 0, so winVote is nil,

I think it may be an issue with ToggleVoting itself, what were you trying to do?

This is line 73:

local mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)

So do I need to set winVote to the map?

Nono, The issue is with all of ToggleVoting, you gave each key an empty table value, but you don’t add anything to them, so #map is always 0.

You set winScore to 0, and in the loop you compare 0 > 0 everytime with how the code is set, this condition is never met so winVote is always nil

The entire function needs a change essentially

1 Like

votedMap returns nil if all the options are tied at 0 votes, and you cannot do :FindFirstChild() with a nil value, so check if it’s not nil before trying to pass it as an arg

function round.LoadMap()
	local votedMap = round.ToggleVoting()

    local mapFolder = ServerStorage.Maps.WheatFields
    if votedMap then
	   mapFolder = ServerStorage.Maps:FindFirstChild(votedMap)
    else

	local newMap = mapFolder:Clone()
	newMap.Parent = workspace.Map

Thanks for the help! I fixed it with an else condition under

if #map > winScore then
1 Like