Search code examples
luanullcomputercraft

Lua attempt to call nil


So I have this Lua script :

function dispTanks()
    mon.setCursorPos(offsetPos, 1)
    mon2.setCursorPos(offsetPos,1)

    for i=1, #machines do   -- RC Tanks
        --------------------------------------------
        if string.find(machines[i], "rcirontankvalvetile")
           or
           string.find(machines[i], "rcsteeltankvalvetile") then                        
            if peripheral.isPresent(machines[i]) then
                periph = peripheral.wrap(machines[i])

                fluidRaw, fluidName, fluidAmount, fluidCapacity, fluidID = marik.getTank(periph)                            

                if fluidName == nil then
                    -- does not display empty tanks
                elseif fluidName ~= nil then
                    mon2.setTextColor(tc)
                    x,y = mon2.getCursorPos()
                        mon2.setCursorPos(offsetPos, (y+1))
                        mon2.clearLine()
                    -- marik.cString(offsetPos,(y+1), tc, right, " ")
                    nameFL = split(marik.comma(fluidName), " ")
                    nameFL = nameFL[0]
                    mon2.write("Tank (" .. nameFL .. ") :  " .. marik.getBuckets(fluidAmount) .. " buckets") 
                end
             end
         end
     end
end

Now it gives a error at this line :

nameFL = split(marik.comma(fluidName), " ")

The error is: attempt to call nil.

Now, I am a beginner in Lua and this isn't my script but a free to use script and I have no idea how to fix this.

EDIT

So before I added the split section this was the result the script should give :

enter image description here

The problem was I wanted to change the names ( ardite.molten ) to Ardite and a friend said I needed to use split, so I added the following :

function firstToUpper(str)
    return (str:gsub("^%l", string.upper))
end

and

nameFL = split(fluidName, " ")
nameFL = nameFL[0]

And changed : mon2.write("Tank (" .. marik.comma(fluidName) .. ") : " .. marik.comma(fluidAmount) .. " / " .. marik.comma(fluidCapacity) .. " mb (" .. marik.getBuckets(fluidAmount) .. " buckets)") to : mon2.write("Tank (" .. nameFL .. ") : " .. marik.getBuckets(fluidAmount) .. " buckets")

which gives me the error :http://puu.sh/a51QI/a848facf1b.png


Solution

  • Either the split function does not exist or the comma function of marik

    EDIT:

    What I think what you try to do is get everything before the dot instead of the full name right?

    In that case you can do this:

    replace these lines

    nameFL = split(marik.comma(fluidName), " ")
    nameFL = nameFL[0]
    

    with this:

    nameFL = marik.comma(fluidName):match("[^.]*")
    

    You don't need a split for this. what this does is pattern matching and in this case it matches to everything until the first dot