• Bad argument to #1 'pairs' (table expected, got nil)
    3 replies, posted
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! [IMG]https://i.gyazo.com/6d5db7c189ef66e2a9ab9c5707dd0d89.png[/IMG] Here is the code for the error: [CODE] -- 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 [/CODE] Here is the chat command that throws the error: [CODE] 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 ) [/CODE]
huh? [code] RAS = {}; RAS.Chat = {}; RAS.Chat.Players = {}; [/code]
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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/table/HasValue]table.HasValue[/url] with a simple check like so: [lua] -- ... elseif RAS.Chat.Players[ self ] then -- ... [/lua]
[QUOTE=Klaes4Zaugen;50155362]huh? [code] RAS = {}; RAS.Chat = {}; RAS.Chat.Players = {}; [/code][/QUOTE] Both have already been made and included throughout the addon...
Sorry, you need to Log In to post a reply to this thread.