Error in Mine Script for CC:Tweaked and Advanced Peripheral

Viewed 35

I got a Pastebin script for a Script that takes your Ores and Raw Materials in Minecraft (1.18.2) but when i execute it it stops after 1 Item and says bad argument (table expected,got nil) on line 31

https://pastebin.com/yrMbyY2Y

--inventory filter, ore dumping for mining
--by toastonrye

local im = peripheral.find("inventoryManager")
local cb = peripheral.find("chatBox")

if not im then error("inventoryManager not found") end
if not cb then error("chatBox not found") end

local filter, match = false, false
local tagFilter = {"forge:ores", "forge:raw_materials"}

local function chatListener()
    while true do
        local event = { os.pullEvent("chat") }
        if event[3]:lower() == "ore on" then
            filter = true
            cb.sendMessageToPlayer("ORE ON", event[2])         
        elseif event[3]:lower() == "ore off" then
            filter = false
            cb.sendMessageToPlayer("ORE OFF", event[2])         
        end
    end
end

local function pushItems()
    while true do
        if filter then
            myInv = im.getItems()  
            for slot, item in pairs(myInv) do
                for _, tag in pairs(item.tags) do
                    for k, v in pairs(tagFilter) do
                        if string.find(tag, v) then
                            match = true
                            break
                        end
                    end
                end
                if match then
                    im.removeItemFromPlayer("UP", item.count, slot)
                    match = false
                end    
            end
        end
    os.sleep(10)
    end
end

parallel.waitForAny(chatListener, pushItems)    
1 Answers

The problem is that item or item.tags may be nil. You need to check this before calling pairs.

This should work:

    while true do
        if filter then
            myInv = im.getItems()  
            for slot, item in pairs(myInv) do
                if item~=nil then if item.tags~=nil then
                    for _, tag in pairs(item.tags) do
                        for k, v in pairs(tagFilter) do
                            if string.find(tag, v) then
                                match = true
                                break
                            end
                        end
                    end
                end end
                if match then
                    im.removeItemFromPlayer("UP", item.count, slot)
                    match = false
                end    
            end
        end
    os.sleep(10)
    end
end

Also, you should know that the Lua APIs for these mods change from version to version, and so you shouldn't be surprised if an old program stops working. You can check the official Advanced Peripherals wiki here, and although it isn't perfect, it is very helpful in situations like these. Finally, if you're having trouble with CC, you can always just open up a new world and test any program you like.

Related