• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua attempt to index a nil value

skic

Member
Joined
Aug 15, 2020
Messages
58
Reaction score
13
Hey guys, I get "attempt to index a nil value" error when using !autoloot add without parameters, is it possible to remove this error, to perform some kind of check?

"!autoloot add, itemName" - If itemName is not provided I get error in console.

Here's the code:

Lua:
function onSay(player, words, param)
    local i = player:getAutoLootList()
    local cache = "Check your loot list: "
    local split = param:split(",")
    local action = split[1]

    if param == "list" then
        if i then
            for _, item in ipairs(i) do
                cache = cache .. (ItemType(item)):getName() .. ", "
            end
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is empty! Add some item and try again.")
            return false
        end

        player:sendTextMessage(MESSAGE_INFO_DESCR, cache:sub(1, -3))
        return false
    end

   if action == "add" then
        local item = split[2]:gsub("%s+", "", 1)
        local itemType = ItemType(item)
        if itemType:getId() == 0 then
            itemType = ItemType(tonumber(item))
            if itemType:getName() == '' then
                player:sendCancelMessage("There is no item with that id or name.")
                return false
            end
        end

        if player:getItemFromAutoLoot(itemType:getId()) then
            player:sendCancelMessage("You're already autolooting this item.")
            return false
        end

        player:addItemToAutoLoot(itemType:getId())
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You're now auto looting " .. itemType:getName())
        return false
    elseif action == "remove" then
        local item = split[2]:gsub("%s+", "", 1)
        local itemType = ItemType(item)
        if itemType:getId() == 0 then
            itemType = ItemType(tonumber(item))
            if itemType:getName() == '' then
                player:sendCancelMessage("There is no item with that id or name.")
                return false
            end
        end

        if player:getItemFromAutoLoot(itemType:getId()) then
            player:removeItemFromAutoLoot(itemType:getId())
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You removed the " .. itemType:getName() .. " from your loot list.")
        else
            player:sendCancelMessage("This item does not exist in your loot list.")
        end
        return false          
    elseif action == "clear" then
        if i then
            for _, item in ipairs(i) do
                player:removeItemFromAutoLoot(ItemType(item):getId())
            end
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is cleared.")
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is empty! Add some item and try again.")
        end
    end

    return false
end

And Error:
1598465492390.png
 
Last edited:
Solution
In global.lua put:
Lua:
table.size = function(table)
    local size = 0
    for k, v in pairs(table) do
        size = size + 1
    end
    return size
end
then before line 22 and 41 add this
Lua:
if table.size(split) < 2 then
    player:sendCancelMessage("You didnt specify any item.")
    return false
end
post your code not an image

Lua:
function onSay(player, words, param)
    local i = player:getAutoLootList()
    local cache = "Check your loot list: "
    local split = param:split(",")
    local action = split[1]

    if param == "list" then
        if i then
            for _, item in ipairs(i) do
                cache = cache .. (ItemType(item)):getName() .. ", "
            end
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is empty! Add some item and try again.")
            return false
        end

        player:sendTextMessage(MESSAGE_INFO_DESCR, cache:sub(1, -3))
        return false
    end

   if action == "add" then
        local item = split[2]:gsub("%s+", "", 1)
        local itemType = ItemType(item)
        if itemType:getId() == 0 then
            itemType = ItemType(tonumber(item))
            if itemType:getName() == '' then
                player:sendCancelMessage("There is no item with that id or name.")
                return false
            end
        end

        if player:getItemFromAutoLoot(itemType:getId()) then
            player:sendCancelMessage("You're already autolooting this item.")
            return false
        end

        player:addItemToAutoLoot(itemType:getId())
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You're now auto looting " .. itemType:getName())
        return false
    elseif action == "remove" then
        local item = split[2]:gsub("%s+", "", 1)
        local itemType = ItemType(item)
        if itemType:getId() == 0 then
            itemType = ItemType(tonumber(item))
            if itemType:getName() == '' then
                player:sendCancelMessage("There is no item with that id or name.")
                return false
            end
        end

        if player:getItemFromAutoLoot(itemType:getId()) then
            player:removeItemFromAutoLoot(itemType:getId())
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You removed the " .. itemType:getName() .. " from your loot list.")
        else
            player:sendCancelMessage("This item does not exist in your loot list.")
        end
        return false           
    elseif action == "clear" then
        if i then
            for _, item in ipairs(i) do
                player:removeItemFromAutoLoot(ItemType(item):getId())
            end
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is cleared.")
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Your list is empty! Add some item and try again.")
        end
    end

    return false
end
 
Here's the code:
no, this is not the code, it is a screenshot 😁 its easier if you post the code for us to edit, but the solution would be something like this or this:

Lua:
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end
 
no, this is not the code, it is a screenshot 😁 its easier if you post the code for us to edit, but the solution would be something like this or this:

Lua:
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

Ye sorry about that, I forgot I can show line numbers when posting code.
 
no, this is not the code, it is a screenshot 😁 its easier if you post the code for us to edit, but the solution would be something like this or this:

Lua:
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

Same error when trying to check if item == "" or if item == nil etc..
I guess it just throws that error every time it tries to assign split[2]:gsub("%s+", "", 1) to item. Line 22
 
Same error when trying to check if item == "" or if item == nil etc..
I guess it just throws that error every time it tries to assign split[2]:gsub("%s+", "", 1) to item. Line 22
noooo, don't check item, check parameter!!!!!

add this right on line 6
Lua:
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end
 
noooo, don't check item, check parameter!!!!!

add this right on line 6
Lua:
    if param == "" then
        player:sendCancelMessage("Command param required.")
        return false
    end

That is working fine if there is not parameter like "add" "remove" etc.. But if you put "!autoloot add" then I get that error. After "add" there should be 1 more parameter.

Full command is "!autoloot add, itemName"

If I write "!autoloot add" I get error.
 
In global.lua put:
Lua:
table.size = function(table)
    local size = 0
    for k, v in pairs(table) do
        size = size + 1
    end
    return size
end
then before line 22 and 41 add this
Lua:
if table.size(split) < 2 then
    player:sendCancelMessage("You didnt specify any item.")
    return false
end
 
Solution
In global.lua put:
Lua:
table.size = function(table)
    local size = 0
    for k, v in pairs(table) do
        size = size + 1
    end
    return size
end
then before line 22 and 41 add this
Lua:
if table.size(split) < 2 then
    player:sendCancelMessage("You didnt specify any item.")
    return false
end

Perfect!
Thanks :D
 
Back
Top