Bad argument to #1 'pairs' (table expected, got nil)

Here is the error (console wouldn’t let me c&p). I’m not sure why it says the RAS.Chat.Players table doesn’t exist, when It’s clearly in the code… I hope someone can help because I’ve been having this problem for two days!

Here is the code for the error:



-- Meta Tables --

RAS.Chat.Players = {}

function meta:CanChat()
	if !RAS.Settings.Chat.Enabled then
		return true
	elseif table.HasValue( RAS.Chat.Players, self:SteamID() ) then -- Line that the error is talking about
		return false
  elseif plyIsExemptChat( self ) then
    return true
  elseif plyIsBannedChat( self ) then
    return false
  else
    return true
  end
end


Here is the chat command that throws the error:



local functionsTable = {} -- Create a table containing all functions
functionsTable["enable"] = function( ply )
	if plyHasPerms( ply ) then
		RAS.AntiSpamming = true
		RAS.ChatPrint( "Raindeer Anti-Spam has been enabled by "..ply:Nick() )
		RAS.Log( "Enabled by ".."["..ply:SteamID().."]"..ply:Nick() )
		return ''
	else
		RAS.ChatPrint( "You do not have permission to do this!", ply )
		return ''
	end
end

local function CommandFilter( ply, text )
	local args = string.Split( text, " " )
	if args[1] == "!ras" or args[1] == "/ras" then
		local func = functionsTable[args[2]]
		if func then
			table.remove( args, 1 ) -- There's no need to pass on the "ras" part of it, as we only need it here to call the function
			table.remove( args, 1 ) -- There's also no need to pass on the second part of it, as we only need it here to know which function to call exactly
			func( ply, args ) -- We pass any additional argument for the function to handle
		end
		return ''
	end
end
hook.Add( "PlayerSay", "CheckForChatRASCommand", CommandFilter )


huh?



RAS = {};
RAS.Chat = {};
RAS.Chat.Players = {};


At any point in your code are you setting that variable to nil? Do a quick search (Notepad++ allows you to search for a string in a given directory, which will help you find all instances). If not, and you still have no idea what could be causing the error, then replace it with something like

[lua]
table.HasValue( RAS.Chat.Players or {}, self:SteamID() )
[/lua]

On another note, it’s bad practice to use table.HasValue for this kind of thing because the more objects are in the table, the longer it will take for that function to return a value, when you already know what you are looking for. You could instead organise your table like this:

[lua]
RAS.Chat.Players[ PlayerObj ] = infomation_here
[/lua]

allowing you to replace the call to

table.HasValue with a simple check like so:

[lua]
– …

elseif RAS.Chat.Players[ self ] then

– …
[/lua]

Both have already been made and included throughout the addon…