"Attempt to compare nil <= number"

Hey devs.

I just added the voting functionality to my game (for both maps and gamemodes), but there is only one error printed out in the output, which is “attempt to compare nil <= number”. (At line )
The thing is that it SHOULD work fine, but it doesn’t.

I have another issue as well, but it doesn’t cause any errors. The two remote event connections run automatically. As well as the button clicks don’t register (which would fire the remote event).
What’s weird to me is this is the exact same system I use in another one of my games, and that works just fine. The only thing I changed about it is a couple variable names, and I added a second votes table to separate the map votes and gamemode votes.

I’ve already tried printing around the script, which is also what made me figure out the two remote event connections run automatically.
I’ve also tried looking around the devforum, but other people with the same error had entirely different problems.


Full voting function (Round Module)
function round.Voting(votingTime)
	local gamemodesList = {gamemodes.Standard, gamemodes.Wave, gamemodes.PlayerAttack, gamemodes.HardcoreWave, gamemodes.Infection}
	
	local mapVotes = {}
	local modeVotes = {}
	
	remotes:WaitForChild("RoundVoting"):FireAllClients(true)
	
	for i, v in pairs(serverStorage.MainGame.Maps:GetChildren()) do
		mapVotes[v.Name] = {}
	end
	
	for x, z in pairs(gamemodesList) do
		modeVotes[z] = {}
	end
	
	local placeMapVoteConnection = remotes:WaitForChild("ClientMapVoted").OnServerEvent:Connect(function(player, mapName)
		print("Player voted for a map")
		
		if mapVotes[mapName] then
			for i, playerVotesTab in pairs(mapVotes) do
				for x, playerName in pairs(playerVotesTab) do
					if playerName == player.Name then
						table.remove(playerVotesTab, x)
						break
					end
				end
			end
			
			table.insert(mapVotes[mapName], player.Name)
			warn(mapVotes)
			remotes:WaitForChild("UpdateMapVoteCount"):FireAllClients(mapVotes)
		end
	end)
	
	local placeModeVoteConnection = remotes:WaitForChild("ClientModeVoted").OnServerEvent:Connect(function(player, modeName)
		print("Player voted for a mode")
		
		if modeVotes[modeName] then
			for i, playerVotesTab in pairs(modeVotes) do
				for x, playerName in pairs(playerVotesTab) do
					if playerName == player.Name then
						table.remove(playerVotesTab, x)
						break
					end
				end
			end

			table.insert(modeVotes[modeName], player.Name)
			warn(modeVotes)
			remotes:WaitForChild("UpdateModeVoteCount"):FireAllClients(modeVotes)
		end
	end)
	
	for i = votingTime, 0, -1 do
		status.Value = "Map Voting ("..i..")"
		task.wait(1)
	end
	
	placeMapVoteConnection:Disconnect()
	placeModeVoteConnection:Disconnect()
	
	remotes:WaitForChild("RoundVoting"):FireAllClients(false)
	
	local mostMapVotes = nil
	local mostModeVotes = nil
	
	local mostVotedMap = nil
	local mostVotedMode = nil
	
	for index, mapTable in pairs(mapVotes) do
		local votes = #mapTable
		
		if mostMapVotes == nil then
			mostMapVotes = votes
			mostVotedMap = index
		else
			if votes >= mostMapVotes then
				mostMapVotes = votes
				mostVotedMap = index
			end
		end
	end
	
	for index_, modeTable in pairs(modeVotes) do
		local votes = #modeTable
		warn(votes)

		if mostMapVotes == nil then
			mostModeVotes = votes
			mostVotedMode = index_
		else
			if votes >= mostModeVotes then --// Specific line causing the error
				mostModeVotes = votes
				mostVotedMode = index_
			end
		end
	end
	
	local chosenMap
	local chosenMode
	
	if mostMapVotes == nil or mostVotedMap == nil then
		chosenMap = serverStorage:WaitForChild("MainGame"):WaitForChild("Maps"):GetChildren()[math.random(1, #serverStorage:WaitForChild("MainGame"):WaitForChild("Maps"):GetChildren())]
	else
		chosenMap = serverStorage:WaitForChild("MainGame"):WaitForChild("Maps")[mostVotedMap]
	end
	
	if mostModeVotes == nil or mostVotedMode == nil then
		chosenMode = gamemodesList[math.random(1, #gamemodesList)]
	else
		chosenMode = gamemodes[mostVotedMode]
	end
	
	wait(2)
	
	return chosenMap, chosenMode
end
What's causing the error

Specific line causing the error:

if votes >= mostModeVotes then

And the full loop that line is in:

for index_, modeTable in pairs(modeVotes) do
	local votes = #modeTable
	warn(votes)

	if mostMapVotes == nil then
		mostModeVotes = votes
		mostVotedMode = index_
	else
		if votes >= mostModeVotes then --// Specific line causing the error
			mostModeVotes = votes
			mostVotedMode = index_
		end
	end
end
And the client code for the entire voting menu
-- >>: Voting
local votingMenu = frames:WaitForChild("Voting")
local votingMaps = votingMenu:WaitForChild("Frames"):WaitForChild("Maps"):WaitForChild("Items")
local votingModes = votingMenu:WaitForChild("Frames"):WaitForChild("Modes"):WaitForChild("Items")

local availableMaps = remotes:WaitForChild("GetMaps"):InvokeServer()
local availableModes = {"Standard", "Wave", "PlayerAttack", "HardcoreWave", "Infection"}

for _, mapBtn in pairs(votingMaps:GetChildren()) do
	if mapBtn:IsA("ImageButton") and table.find(availableMaps, mapBtn.Name) then
		remotes:WaitForChild("ClientMapVoted"):FireServer(mapBtn.Name)
	end
end

for _, modeBtn in pairs(votingMaps:GetChildren()) do
	if modeBtn:IsA("ImageButton") then
		remotes:WaitForChild("ClientModeVoted"):FireServer(modeBtn.Name)
	end
end

remotes:WaitForChild("UpdateMapVoteCount").OnClientEvent:Connect(function(data)
	for i, v in pairs(data) do
		if votingMaps[i] then
			votingMaps[i].Votes.Text = #v
		end
	end
end)

remotes:WaitForChild("UpdateModeVoteCount").OnClientEvent:Connect(function(data)
	for i, v in pairs(data) do
		if votingModes[i] then
			votingModes[i].Votes.Text = #v
		end
	end
end)

Screenshots of the output (where the warns and prints get ran in the script):


You don’t have to help with my other two issues, but if you want to, I’d appreciate that as well.
I just need to get the primary issue causing error fixed. I can figure out the other two problems on my own


I’m in a rush (personal stuff irl), so if you need me to explain a little more clearly, just ask. I’ll clarify as best as I can once I have time.

Thank you in advance for any help.

Best regards,
Amora

Try put the votes >= mostModeVotes inside of () in the if statment.

That definitely will not fix the issue.

OP, if you put this before, what do you get?

print(`votes = {votes}, mostModeVotes = {mostModeVotes}`)
1 Like

I don’t think that’s going to help.

In the if statement, I have these lines:

local votes = #modeTable
warn(votes)

The warn returns zero.

Ah, I see the problem. You are checking >= mostModeVotes before you ever set it.

	for index_, modeTable in pairs(modeVotes) do
		local votes = #modeTable
		warn(votes)

		if mostMapVotes == nil then
			mostModeVotes = votes
			mostVotedMode = index_
		else
			if votes >= mostModeVotes then -- mostModeVotes here is nil
				mostModeVotes = votes
				mostVotedMode = index_
			end
		end
	end

It’s likely because your check is if mostMapVotes == nil instead of if mostModeVotes == nil.

1 Like

Oh my bad then. I have seen in the past an issue in regards to comparing in if statement and the fix was to add brackets (even though I don’t see why this would fix an issue).

It can happen if you have funky precedence. For example:

if not x == y then

behaves differently than

if not (x == y) then
1 Like

If this fixes it I’m actually going to cry.

I need to relearn how to read

1 Like

Ideally strict Luau would catch this for you (it would see you are comparing a number to a number | nil), but I think guard checks for nil don’t work yet unfortunately :frowning:

Looks like it does, but you need to type mostModeVotes as number? explicitly (otherwise it seems to turn into number)

Well, it did fix the main error, but now farther down in the script, I get an error “attempt to call a nil value” at line 160 (in a separate function):

function round.StartRound(gameLength, chosenMap, chosenMode)
	local chosenGamemode = gamemodes[chosenMode]
	
	status.Value = ""
	gameValues.InRound.Value = true
	
	chosenGamemode(gameLength, chosenMap) --// Broken line
end

I presume it does this due to an error in the voting function, and it returning chosenMode as nil at the bottom of it. But it may also be returning the function itself since the gamemodes table at the top is referring to the functions in another module, not strings or anything.
I’m gonna see if something works for that, but if you can help with that as well, that’d be great!

I’d recommend getting comfy with the debugger, here you would place a breakpoint at that line, and then you could make a “watch” for gamemodes, chosenGamemode, chosenMode, etc to figure out exactly what is going wrong. The crux of it is that gamemodes[chosenMode] is nil, but figuring out why would become much much simpler with good use of the debugger.

I’ve never personally used the debugger, so it’ll take some time lol.

But when returning, it does return it as “nil” in the voting function. I can try to figure out why on my own, though!

1 Like

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