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