Vote returns nil?

I tried to make a mode voting for my tower defense game but its not going so well the modeVote or winVote keeps returning as nil even if I vote something:

function round.ToggleModeVoting()
    local modes = serverStorage.Modes:GetChildren()
    modeVotes = {}
    for i, mode in ipairs(modes) do
        modeVotes[mode.Name] = {}
    end

    local modeVote = nil
    local winScore = 0

    for name, mode in pairs(modeVotes) do
        print(name,mode)
        if #mode > winScore then
            print(name,mode)
            print(winScore, modeVote)
            winScore = #mode
            modeVote = name
            print(name,mode)
        end
    end
    
    print(winScore)
    print(modeVote)
    
    if not modeVote then
        local n = math.random(#modes)
        modeVote = modes[n].Name
    end
    print(winScore)
    print(modeVote)
    
    return modeVote
end
3 Likes

It’s because #mode is 0 which is caused by you setting modeVotes[mode.Name] to {}.

Therefore when this happens:

#mode (which is 0) is never going to be bigger than winScore (which is also 0). So none of the variables in that if statement are set, making it return nil.

But thats how I set everything up so I cant just remove it. For more information, this is the whole thing:

function round.ToggleModeVoting()
	local modes = serverStorage.Modes:GetChildren()
	modeVotes = {}
	for i, mode in ipairs(modes) do
		modeVotes[mode.Name] = {}
	end

	local modeVote = nil
	local winScore = 0

	for name, mode in pairs(modeVotes) do
		print(name,mode)
		if #mode > winScore then
			print(name,mode)
			print(winScore, modeVote)
			winScore = #mode
			modeVote = name
			print(name,mode)
		end
	end
	
	print(winScore)
	print(modeVote)
	
	if not modeVote then
		local n = math.random(#modes)
		modeVote = modes[n].Name
	end
	print(winScore)
	print(modeVote)
	
	return modeVote
end

function round.ProcessModeVote(player, vote)

	for name, theVote in pairs(modeVotes) do
		local oldVote = table.find(theVote, player.UserId)
		if oldVote then
			table.remove(theVote, oldVote)
			print("Old vote found", oldVote)
			break
		end
	end

	print("Processed vote for", vote)
	if modeVotes[vote] == nil then
		modeVotes[vote] = {}
	end
	table.insert(modeVotes[vote], player.UserId)

	events:WaitForChild("UpdateModeCount"):FireAllClients(modeVotes)
end


events:WaitForChild("VoteForMode").OnServerEvent:Connect(round.ProcessModeVote)```

So where do you call .ToggleModeVoting?

In this function:

function round.LoadMode()
	local votedMode = round.ToggleModeVoting()
	print(votedMode)
	local modeFolder = serverStorage.Modes:FindFirstChild(votedMode)
		
	return modeFolder.Name
end

Which is then used when calling the waves

local mode = round.LoadMode()

	info.GameRunning.Value = true

	for i = 10, 0 , -1 do
		info.Message.Value = "Game starting in... "..i
		task.wait(1)
	end
	
	local waves = 10

	for wave = 1, waves  do
		info.Wave.Value = wave
		info.Message.Value = ""

		round.GetWave(wave, map ,mode)

		repeat
			task.wait(1)	
		until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value

Did you mean to add this loop?

It will just reset the votes when you load the round.

I think this should work:

function round.ToggleModeVoting()
	local modes = serverStorage.Modes:GetChildren()
	local modeVote = nil
	local winScore = 0

	for name, mode in pairs(modeVotes) do
		print(name,mode)
		if #mode > winScore then
			print(name,mode)
			print(winScore, modeVote)
			winScore = #mode
			modeVote = name
			print(name,mode)
		end
	end
	
	print(winScore)
	print(modeVote)
	
	if not modeVote then
		local n = math.random(#modes)
		modeVote = modes[n].Name
	end
	print(winScore)
	print(modeVote)
	
	return modeVote
end

Oh wow that worked perfectly and yes I did mean to add that loop since it sets up the whole table but yeah, thank you!

1 Like

No problem. If you have any more questions, feel free to ask. Have a good day!

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