This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
dcleondc's profile picture

[ERROR] i need help with this error

Started by dcleondc, 27 September 2012 - 03:14 AM
dcleondc #1
Posted 27 September 2012 - 05:14 AM
here is the code

rednet.open("top")
if not fs.exists("banned") then
    local file=io.open("banned", "w")
	    file:write("")
	    file:close()
end
function getCodes()
local file=io.open("banned", "r")
local sCodes=file:read()
file:close()
tCodes=textutils.unserialize(sCodes)
end
function addBan(ban)
    getCodes()
    table.insert("tCodes", ban) -- this is line 17
    local sCodes=textutils.serialize(tCodes)
    local file=io.open(banned, "w")
    file:write(sCodes)
    file:close()
end
function checkBanned(Idnum)
    local file = io.open("banned", "r")
    local sCodes = file:read()
    file:close()
    tCodes=textutils.unserialize(sCodes)
    l=#tCodes
    for i=1, l do
	    if tCodes[l] then
		    return "true"
	    end
    end
end
   
while true do
    addBan("15")
    print("ID | Distance | Message")
    Id,Msg,Dis=rednet.receive(timeout)
    if not checkBanned(Id) == "true" then
	    print(Id.." | "..Dis.." | "..Msg)
	    Id2,Msg2,Dis2=rednet.receive(timeout)
	    print(Id2.." | "..Dis2.." | "..Msg2)
	    if Id1==Id2 then
		    Id3,Msg3,Dis3=rednet.receive(timeout)
			    print(Id3.." | "..Dis3.." | "..Msg3)
		    if Id2==Id3 then
			    Id4,Msg4,Dis4=rednet.receive(timeout)
			    print(Id4.." | "..Dis4.." | "..Msg4)
			    addBan(Id4)
			    print("ID: "..Id4.." Has been BANNED!")
		    end
	    end
    end
end

here is the error

Request:17: bad argument: table expected, got nil
Luanub #2
Posted 27 September 2012 - 05:28 AM
remove the " around tCodes in line 17, it thinks it is a string because of them.
dcleondc #3
Posted 27 September 2012 - 05:39 AM
before when i didnt have "" around tCodes it still gave the same error
Luanub #4
Posted 27 September 2012 - 05:50 AM
Try this for debugging

local a = type(tCodes)
print(a)

EDIT: Also try adding this to the top of the code just to ensure its declared as a table.

local tCodes = {}
Edited on 27 September 2012 - 04:02 AM
Cranium #5
Posted 27 September 2012 - 04:20 PM

function getCodes()
local file=io.open("banned", "r")
local sCodes=file:read()
file:close()
return sCodes --returning the value generated
end
function addBan(ban)
	local tCodes = getCodes() --adding the value of getCodes() to table tCodes
	table.insert(tCodes, ban) -- removing quotations
	local sCodes=textutils.serialize(tCodes)
	local file=io.open(banned, "w")
	file:write(sCodes)
	file:close()
end
This should work. With what little I know about tables, this is a functional, if sloppy implementation.