Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to compare table and number ??? script error

I am making a GUI map voting system and when I go to test the script I get this output:

01:58:16.154 - ServerScriptService.Server:111: attempt to compare table and number

I don't understand what it means by this but the error is happening on line 111:

--Assets--
local rep = game.ReplicatedStorage
local assets = rep.Assets

--Maps--
local maps = assets.Maps

--Signals--
local signals = assets.Signals
local event = signals.Event
local fevent = signals.FEvent

--Static Variables--
local mapVotes = {}

--Game Variables--
local Game = game.StarterGui.Game
local stats = Game.Stats
local settings = {
	mapVoteTime=15;
	MapVoteDelay=3;
	playersRequired=1;
	
}
 
--Primary Events--

--[[
	ARRAY = MapVotes
		MapArray
			id
			mapName
			players
				player=userId
--]]

event.OnServerEvent:connect(function(player, variables)
	if variables.reason == "voteOnMap" then
		--First check if player already voted on a map, if so then change map
		for a,b in pairs(mapVotes)do
			for d,c in pairs(b.players)do
				if c.player == player.UserId then
					table.remove(b.players, d)
					break
				end
			end
		end
		--Add player to the map votes
		for a,b in pairs(mapVotes)do
			if b.id == variables.itemNum then
				table.insert(b.players, {player=player.UserId})			
			end 
		end
		elseif variables.reason == "removeFromVote" then
					for a,b in pairs(mapVotes)do
			for d,c in pairs(b.players)do
				if c.player == player.UserId then
					table.remove(b.players, d)
					break
				end
			end
		end
	end
end)

function getMap()
	local RandomMap = maps:GetChildren()[math.random(1,#maps:GetChildren())]
	for a,b in pairs(mapVotes) do
		if b.mapName == RandomMap.Name then
			return getMap()
		end
	end
end

while wait() do
	if #game.Players:GetPlayers() < settings.playersRequired then
		local playersNeeded = settings.playersRequired-#game.Players:GetPlayers()
		stats.Status.Value = playersNeeded.." "..((playersNeeded==1 and "Player")or "Players").."need to play!"
	else
		--assign maps
		mapVotes={}
		for a,b in pairs(Game.Special.VotingGUIs.ScreenGUI:GetChildren()) do
			table.insert(mapVotes, {id=tonumber(b.Name:match("%d+")); mapName=getMap(); players={};})
		end
		
		--Fire clients, start sending in votes.
		event:FireAllClients({reason="startVoting"})
		
		--Do main loop
		local start = tick()
		while wait() do
			if tick()-start >=settings.mapVoteTime then break end
			local secondsLeft = math.floor(settings.mapVoteTime-(tick()-start))
			stats.Status.Value = secondsLeft.." "..((secondsLeft==1 and "Second ")or "Seconds ").."Left to Vote"
			for a,b in pairs(Game.Special.VotingGUIs.ScreenGUI:GetChildren()) do
				local playersVoting, mName
				for d,c in pairs(mapVotes) do
					if c.id == tonumber(b.Name:match("%d+")) then
						playersVoting=#c.players
						mName=c.mapName
						break
					end
				end
				if mName and playersVoting then
					b.sGui.Title.Text = mName.." - "..playersVoting.." "..((playersVoting==1 and "Voter")or "Voters")
				end
			end
		end
		
		--Get winner
		table.sort(mapVotes, function(a,b) return #a.players>b.players end)
		
		for a,b in pairs(Game.Special.VotingGUIs.ScreenGUI:GetChildren()) do
			if tonumber(b.Name:match("%d+")) == mapVotes[1].id then
				b.votingGUI.sGui.Title.Text = "Winner!"
				stats.Status.Value = mapVotes[1].mapName.." was chosen!"
			else
				b.VotingGUI.sGui.Title.Text = "Lost Vote"
				
				wait(settings.mapVoteDelay)
			end
		end
	end
end

there is also a local script that goes with this that may be adding to the problem:

--Player--
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local gui = player:WaitForChild("PlayerGui")
local ui= gui:WaitForChild("ui")
 
--Assets--
local rep = game.ReplicatedStorage
local assets = rep.Assets

--Maps--
local maps = assets.Maps

--Signals--
local signals = assets.Signals
local event = signals.Event
local fevent = signals.FEvent

--Game Variables--
local Game = game.StarterGui.Game
local stats = Game.Stats

--Static Variables--
local vars = {
	currentVote=nil;
	services={};
}

--Primary Events--
event.OnClientEvent:connect(function(variables)
	if variables.reason == "startVoting" then
		table.insert(vars.services, game:GetService("RunService").RenderStepped:connect(function()
			local ray = Ray.new(char.PrimaryPart.CFrame.p, Vector3.new(0.-1000.0))
			local object = workspace:findPartOnRay(ray, char, false, false)
			if object and object.Name:match("VotingButton") then
				local votingGUINum = tonumber(object.Name:match("%d+"))
				if vars.currentVote==nil then
					vars.currentVote = votingGUINum
					event:FireServer({reason="voteOnMap"; itemNum=votingGUINum;})
				elseif vars.currentVote~=votingGUINum then
					vars.currentVote = votingGUINum
					event:FireServer({reason="voteOnMap"; itemNum=votingGUINum;})
				end
			elseif vars.currentVote~=nil then
				vars.currentVote=nil
				event:FireServer({reason="removeFromVote"})
			end
		end))
	elseif variables.reason == "endVoting" then
		for a,b in pairs(vars.services) do
			b:disconnect()
		end
		vars.services= {}
	end
end)

--Initiate Title Updater
game:GetService("RunService").RenderStepped:connect(function()
	ui:WaitForChild("Title").Text = game.StarterGui.Game.Stats.Status.Value
end)


if you can solve this then thank you!

1 answer

You're getting said error on this part of the line:

#a.players>b.players

a and b are both tables, and b.players is not fed the length operator that a.players has. Therefore, you're actually comparing the hex of b to the length of a, since Lua represents a table by its hex. In order to fix your mistake, just slap the length operator next to b.players:

#a.players>#b.players

This discussion is preserved as it was. New answers, comments, and votes are closed.