0

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)    
  • you provided a nil value where a table value is expected. make sure you actually provide a table value to `pairs` to avoid this error. – Piglet Sep 07 '22 at 14:54
  • Thx , but could you maybe do it real quick ? I never used this its just from a tutorial and I dont have a clue what to do – xgamerfausn Sep 07 '22 at 15:02
  • but isnt the tags from a item a table ? – xgamerfausn Sep 07 '22 at 15:21
  • you have three lines that could have caused this issue. assuming that a few lines might have went missing during copy paste I'd say eithre myInv, item.tags or tagFilter is nil. I suggest you print those variables befor you run the loops. I cannot fix that for you. I don't have any documentation nor do I have all code. – Piglet Sep 07 '22 at 15:25
  • So , it printed the first one with myInv so he got a table of my inv but then after it it stopped with the error. But the error says like he needs a table but got nil so item.tags is nil – xgamerfausn Sep 07 '22 at 15:32

1 Answers1

1

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.

Promitheas Nikou
  • 511
  • 4
  • 14