luaminecraftcomputercraft

Computercraft error attempt to call nil on a turtle.inspectDown() return value


I have an error on line 42

if parseBlock(bl1.name) then

parseBlock is a function I made to do some checks to see if that block is an ore (I have many different ores from different mods that I have in there)
I narrowed it down to this line

local success1,bl1 = turtle.inspectDown()

success1 is true
bl1 is nil

Wondering why inspectDown() returned nil? There is a block below the turtle as well.
The block is: undergroundbiomes:igneous_stone

I went into the lua program to do the same lines of code, yet it does it properly printing the name of the block below it.


Solution

  • I currently cannot confirm this in the computercraft API, but if I remember correctly it does follow the Lua convention of returning either a value or nil+message on error. What this means is:

    When an error happens, the function will return nil and an error description as a string. When no error happens though, the function will just return its result. The difference here is that the result will be the first return value of the function.

    Normally this can be handled like this:

    local result, err = some_function()
    if result then
       do_something_with(result)
    else
       print("Error! " .. err)
    end
    

    However, while that is likely the reason for success1 being nil, it doesn't explain the error message you are getting. Lua is telling you that you are trying to call nil, and there is only one function call in the code you provided: parseBlock.

    As Egor Skriptunoff pointed out, parseBlock is most likely nil, which causes the error when you attempt to call it. You can confirm this by simply adding print(parseBlock) above the line where you call it and if it says nil, then you've found the cause of the problem.

    Otherwise, you'd have to add a bit more information: