• 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 Autoloot - Gold to bank need help

Status
Not open for further replies.

Galatea

Banned User
Joined
Sep 8, 2022
Messages
41
Reaction score
15
Location
Germany
Hello, I need help with my autoloot, namely that gold should go straight to the bank and items to the backpack normally. Because he is slightly bugged and the player can have xxx gold stacks in bp, and when he goes to sell the loot, he has gold under him and has to collect manually.

TFS 1.4.2 10.98 rep+++++ for any help!

talkaction
Lua:
function onSay(player, words, param)
    local split = param:split(",")

    local action = split[1]
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        local size = 0
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if size == AUTO_LOOT_MAX_ITEMS then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The list is full, please remove from the list to make some room.")
                break
            end

            if storage == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." is already in the list.")
                break
            end

            if storage <= 0 then
                player:setStorageValue(i, itemType:getId())
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been added to the list.")
                break
            end

            size = size + 1
        end
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            if player:getStorageValue(i) == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been removed from the list.")
                player:setStorageValue(i, 0)
                return false
            end
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." was not founded in the list.")
    elseif action == "show" then
        local text = "-- Auto Loot List --\n"
        local count = 1
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if storage > 0 then
                text = string.format("%s%d. %s\n", text, count, ItemType(storage):getName())
                count = count + 1
            end
        end

        if text == "" then
            text = "Empty"
        end

        player:showTextDialog(1950, text, false)
    elseif action == "clear" then
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            player:setStorageValue(i, 0)
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The autoloot list has been cleared.")
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Use the commands: !autoloot {add, remove, show, clear}")
    end

    return false
end

creaturescripts
Lua:
local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local corpse = Tile(position):getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == cid then
        for a = corpse:getSize() - 1, 0, -1 do
            local containerItem = corpse:getItem(a)
            if containerItem then
                for b = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                    if player:getStorageValue(b) == containerItem:getId() then
                        containerItem:moveTo(player)
                    end
                end
            end
        end
    end
end

function onKill(player, target)
    if not target:isMonster() then
        return true
    end

    addEvent(scanContainer, 100, player:getId(), target:getPosition())
    return true
end
 
Solution
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/creaturescripts/scripts/autoloot.lua:22: attempt to index global 'item' (a nil value)
stack traceback:
[C]: in function '__index'
data/creaturescripts/scripts/autoloot.lua:22: in function <data/creaturescripts/scripts/autoloot.lua:1>
Lua:
local coins = {
    [2148] = 1,
    [2152] = 100,
    [2160] = 10000
}

local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and...
talkaction
Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local cash = {2148, 2152, 2160}
    local action = split[1]
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        elseif isInArray(cash ,itemType:getId()) then
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "All sort of cash are selected by standard of the autoloot system and transferred directly to your bank.")
            return false      
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        local size = 0
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if size == AUTO_LOOT_MAX_ITEMS then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The list is full, please remove from the list to make some room.")
                break
            end

            if storage == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." is already in the list.")
                break
            end

            if storage <= 0 then
                player:setStorageValue(i, itemType:getId())
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been added to the list.")
                break
            end

            size = size + 1
        end
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            if player:getStorageValue(i) == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been removed from the list.")
                player:setStorageValue(i, 0)
                return false
            end
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." was not founded in the list.")
    elseif action == "show" then
        local text = "-- Auto Loot List --\n"
        local count = 1
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if storage > 0 then
                text = string.format("%s%d. %s\n", text, count, ItemType(storage):getName())
                count = count + 1
            end
        end

        if text == "" then
            text = "Empty"
        end

        player:showTextDialog(1950, text, false)
    elseif action == "clear" then
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            player:setStorageValue(i, 0)
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The autoloot list has been cleared.")
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Use the commands: !autoloot {add, remove, show, clear}")
    end

    return false
end

creaturescript
Lua:
local function scanContainer(cid, position)
   local cashList = { -- place currency ids here / their worth in gold
      [2148] = 1,
      [2152] = 100,
      [2160] = 10000,
   }

   local player = Player(cid)
   if not player then
      return
   end

   local corpse = Tile(position):getTopDownItem()
   if not corpse or not corpse:isContainer() then
      return
   end

   if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == cid then
      for a = corpse:getSize() - 1, 0, -1 do
         local containerItem = corpse:getItem(a)
         if containerItem then
            local coin = cashList[item.itemid]
            if coin then
               player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
               item:remove()
            else
               for b = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                  if player:getStorageValue(b) == containerItem:getId() then
                     containerItem:moveTo(player)
                  end
               end
            end
         end
      end
   end

   function onKill(player, target)
      if not target:isMonster() then
         return true
      end

      addEvent(scanContainer, 100, player:getId(), target:getPosition())
      return true
   end

Not tested but should work.
 
Last edited:
talkaction
Lua:
function onSay(player, words, param)
    local split = param:split(",")
    local cash = {2148, 2152, 2160}
    local action = split[1]
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        elseif isInArray(cash ,itemType:getId()) then
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "All sort of cash are selected by standard of the autoloot system and transferred directly to your bank.")
            return false    
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        local size = 0
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if size == AUTO_LOOT_MAX_ITEMS then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The list is full, please remove from the list to make some room.")
                break
            end

            if storage == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." is already in the list.")
                break
            end

            if storage <= 0 then
                player:setStorageValue(i, itemType:getId())
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been added to the list.")
                break
            end

            size = size + 1
        end
    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:getId() == 0 then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There is no item with that id or name.")
                return false
            end
        end

        local itemName = tonumber(split[2]) and itemType:getName() or item
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            if player:getStorageValue(i) == itemType:getId() then
                player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." has been removed from the list.")
                player:setStorageValue(i, 0)
                return false
            end
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, itemName .." was not founded in the list.")
    elseif action == "show" then
        local text = "-- Auto Loot List --\n"
        local count = 1
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            local storage = player:getStorageValue(i)
            if storage > 0 then
                text = string.format("%s%d. %s\n", text, count, ItemType(storage):getName())
                count = count + 1
            end
        end

        if text == "" then
            text = "Empty"
        end

        player:showTextDialog(1950, text, false)
    elseif action == "clear" then
        for i = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
            player:setStorageValue(i, 0)
        end

        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "The autoloot list has been cleared.")
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Use the commands: !autoloot {add, remove, show, clear}")
    end

    return false
end

creaturescript
Lua:
local function scanContainer(cid, position)
   local cashList = { -- place currency ids here / their worth in gold
      [2148] = 1,
      [2152] = 100,
      [2160] = 10000,
   }

   local player = Player(cid)
   if not player then
      return
   end

   local corpse = Tile(position):getTopDownItem()
   if not corpse or not corpse:isContainer() then
      return
   end

   if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == cid then
      for a = corpse:getSize() - 1, 0, -1 do
         local containerItem = corpse:getItem(a)
         if containerItem then
            local coin = cashList[item.itemid]
            if coin then
               player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
               item:remove()
            else
               for b = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                  if player:getStorageValue(b) == containerItem:getId() then
                     containerItem:moveTo(player)
                  end
               end
            end
         end
      end
   end

   function onKill(player, target)
      if not target:isMonster() then
         return true
      end

      addEvent(scanContainer, 100, player:getId(), target:getPosition())
      return true
   end

Not tested but should work.
[Warning - Event::checkScript] Can not load script: scripts/autoloot.lua
data/creaturescripts/scripts/autoloot.lua:44: 'end' expected (to close 'function' at line 1) near '<eof>'

hmm maybe more easy is change this script from sarah? it's revscript


Lua:
local autoloot = {
    talkaction = "!autoloottest",
    storageBase = 50000,
    freeAccountLimit = 10,
    premiumAccountLimit = 20,
    currencyToBank = true
}

local currencyItems = {}
if autoloot.currencyToBank then
    for index, item in pairs(Game.getCurrencyItems()) do
        currencyItems[item:getId()] = true
    end
end

local autolootCache = {}
local textEditRequests = {}

local function getPlayerLimit(player)
    return player:isPremium() and autoloot.premiumAccountLimit or autoloot.freeAccountLimit
end

local function getPlayerAutolootItems(player)
    local limits = getPlayerLimit(player)
    local guid = player:getGuid()
    local itemsCache = autolootCache[guid]
    if itemsCache then
        if #itemsCache > limits then
            local newChache = {unpack(itemsCache, 1, limits)}
            autolootCache[guid] = newChache
            return newChache
        end
        return itemsCache
    end

    local items = {}
    for i = 1, limits do
        local itemType = ItemType(math.max(player.storage[autoloot.storageBase + i], 0))
        if itemType and itemType:getId() ~= 0 then
            items[#items +1] = itemType:getId()
        end
    end

    autolootCache[guid] = items
    return items
end

local function setPlayerAutolootItems(player, newItems)
    local items = getPlayerAutolootItems(player)
    for i = getPlayerLimit(player), 1, -1 do
        local itemId = newItems[i]
        if itemId then
            player.storage[autoloot.storageBase + i] = itemId
            items[i] = itemId
        else
            player.storage[autoloot.storageBase + i] = -1
            table.remove(items, i)
        end
    end
    return true
end

local function addPlayerAutolootItem(player, itemId)
    local items = getPlayerAutolootItems(player)
    for _, id in pairs(items) do
        if itemId == id then
            return false
        end
    end
    items[#items +1] = itemId
    return setPlayerAutolootItems(player, items)
end

local function removePlayerAutolootItem(player, itemId)
    local items = getPlayerAutolootItems(player)
    for i, id in pairs(items) do
        if itemId == id then
            table.remove(items, i)
            return setPlayerAutolootItems(player, items)
        end
    end
    return false
end

local function hasPlayerAutolootItem(player, itemId)
    for _, id in pairs(getPlayerAutolootItems(player)) do
        if itemId == id then
            return true
        end
    end
    return false
end

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    if not corpse:getType():isContainer() then
        return
    end

    local corpseOwner = Player(corpse:getCorpseOwner())
    local items = corpse:getItems()
    local warningCapacity = false
    for _, item in pairs(items) do
        local itemId = item:getId()
        if hasPlayerAutolootItem(corpseOwner, itemId) then
            if currencyItems[itemId] then
                local worth = item:getWorth()
                corpseOwner:setBankBalance(corpseOwner:getBankBalance() + worth)
                corpseOwner:sendTextMessage(MESSAGE_STATUS_SMALL, string.format("Your balance increases by %d gold coins.", worth))
                item:remove()
            elseif not item:moveTo(corpseOwner) then
                warningCapacity = true
            end
        end
    end

    if warningCapacity then
        corpseOwner:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You no have capacity.")
    end
end

ec:register(3)

local talkAction = TalkAction(autoloot.talkaction)

function talkAction.onSay(player, words, param, type)
    local split = param:splitTrimmed(",")
    local action = split[1]
    if not action then
        player:showTextDialog(2160, string.format("Examples of use:\n%s add,gold coin\n%s remove,gold coin\n%s clear\n%s show\n%s edit\n\n~Available slots~\nfreeAccount: %d\npremiumAccount: %d\ncurrency to bank: %s", words, words, words, words, words, autoloot.freeAccountLimit, autoloot.premiumAccountLimit, autoloot.currencyToBank and "yes" or "no"), false)
        return false
    end

    if action == "clear" then
        setPlayerAutolootItems(player, {})
        player:sendCancelMessage("Autoloot list cleaned.")
        return false
    elseif action == "show" then
        local items = getPlayerAutolootItems(player)
        local description = {string.format('~ Your autoloot list, capacity: %d/%d ~\n', #items, getPlayerLimit(player))}
        for i, itemId in pairs(items) do
            description[#description +1] = string.format("%d) %s", i, ItemType(itemId):getName())
        end
        player:showTextDialog(2160, table.concat(description, '\n'), false)
        return false
    elseif action == "edit" then
        local items = getPlayerAutolootItems(player)
        if #items == 0 then
            -- Example
            items = {2160,2672,2432}
        end
        local description = {}
        for i, itemId in pairs(items) do
            description[#description +1] = ItemType(itemId):getName()
        end
        player:registerEvent("autolootTextEdit")
        player:showTextDialog(1948, string.format("To add articles you just have to write their IDs or names on each line\nfor example:\n\n%s", table.concat(description, '\n')), true, 666)
        textEditRequests[player:getGuid()] = true
        return false
    end

    local function getItemType()
        local itemType = ItemType(split[2])
        if not itemType or itemType:getId() == 0 then
            itemType = ItemType(math.max(tonumber(split[2]) or 0), 0)
            if not itemType or itemType:getId() == 0 then
                player:sendCancelMessage(string.format("The item %s does not exists!", split[2]))
                return false
            end
        end
        return itemType
    end

    if action == "add" then
        local itemType = getItemType()
        if itemType then
            local limits = getPlayerLimit(player)
            if #getPlayerAutolootItems(player) >= limits then
                player:sendCancelMessage(string.format("Your auto loot only allows you to add %d items.", limits))
                return false
            end

            if addPlayerAutolootItem(player, itemType:getId()) then
                player:sendCancelMessage(string.format("Perfect you have added to the list: %s", itemType:getName()))
            else
                player:sendCancelMessage(string.format("The item %s already exists!", itemType:getName()))
            end
        end
        return false
    elseif action == "remove" then
        local itemType = getItemType()
        if itemType then
            if removePlayerAutolootItem(player, itemType:getId()) then
                player:sendCancelMessage(string.format("Perfect you have removed to the list the article: %s", itemType:getName()))
            else
                player:sendCancelMessage(string.format("The item %s does not exists in the list.", itemType:getName()))
            end
        end
        return false
    end

    return false
end

talkAction:separator(" ")
talkAction:register()

local creatureEvent = CreatureEvent("autolootCleanCache")

function creatureEvent.onLogout(player)
    setPlayerAutolootItems(player, getPlayerAutolootItems(player))
    autolootCache[player:getGuid()] = nil
    return true
end

creatureEvent:register()

creatureEvent = CreatureEvent("autolootTextEdit")

function creatureEvent.onTextEdit(player, item, text)
    player:unregisterEvent("autolootTextEdit")

    local split = text:splitTrimmed("\n")
    local items = {}
    for index, name in pairs(split) do repeat
        local itemType = ItemType(name)
        if not itemType or itemType:getId() == 0 then
            itemType = ItemType(tonumber(name))
            if not itemType or itemType:getId() == 0 then
                break
            end

            break
        end

        items[#items +1] = itemType:getId()
    until true end
    setPlayerAutolootItems(player, items)
    player:sendCancelMessage(string.format("Perfect, you have modified the list of articles manually."))
    return true
end

creatureEvent:register()

When i have gold to bank on true i got error with GetCurrenyItems function.
 
Last edited:
[Warning - Event::checkScript] Can not load script: scripts/autoloot.lua
data/creaturescripts/scripts/autoloot.lua:44: 'end' expected (to close 'function' at line 1) near '<eof>'

hmm maybe more easy is change this script from sarah? it's revscript


Lua:
local autoloot = {
    talkaction = "!autoloottest",
    storageBase = 50000,
    freeAccountLimit = 10,
    premiumAccountLimit = 20,
    currencyToBank = true
}

local currencyItems = {}
if autoloot.currencyToBank then
    for index, item in pairs(Game.getCurrencyItems()) do
        currencyItems[item:getId()] = true
    end
end

local autolootCache = {}
local textEditRequests = {}

local function getPlayerLimit(player)
    return player:isPremium() and autoloot.premiumAccountLimit or autoloot.freeAccountLimit
end

local function getPlayerAutolootItems(player)
    local limits = getPlayerLimit(player)
    local guid = player:getGuid()
    local itemsCache = autolootCache[guid]
    if itemsCache then
        if #itemsCache > limits then
            local newChache = {unpack(itemsCache, 1, limits)}
            autolootCache[guid] = newChache
            return newChache
        end
        return itemsCache
    end

    local items = {}
    for i = 1, limits do
        local itemType = ItemType(math.max(player.storage[autoloot.storageBase + i], 0))
        if itemType and itemType:getId() ~= 0 then
            items[#items +1] = itemType:getId()
        end
    end

    autolootCache[guid] = items
    return items
end

local function setPlayerAutolootItems(player, newItems)
    local items = getPlayerAutolootItems(player)
    for i = getPlayerLimit(player), 1, -1 do
        local itemId = newItems[i]
        if itemId then
            player.storage[autoloot.storageBase + i] = itemId
            items[i] = itemId
        else
            player.storage[autoloot.storageBase + i] = -1
            table.remove(items, i)
        end
    end
    return true
end

local function addPlayerAutolootItem(player, itemId)
    local items = getPlayerAutolootItems(player)
    for _, id in pairs(items) do
        if itemId == id then
            return false
        end
    end
    items[#items +1] = itemId
    return setPlayerAutolootItems(player, items)
end

local function removePlayerAutolootItem(player, itemId)
    local items = getPlayerAutolootItems(player)
    for i, id in pairs(items) do
        if itemId == id then
            table.remove(items, i)
            return setPlayerAutolootItems(player, items)
        end
    end
    return false
end

local function hasPlayerAutolootItem(player, itemId)
    for _, id in pairs(getPlayerAutolootItems(player)) do
        if itemId == id then
            return true
        end
    end
    return false
end

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    if not corpse:getType():isContainer() then
        return
    end

    local corpseOwner = Player(corpse:getCorpseOwner())
    local items = corpse:getItems()
    local warningCapacity = false
    for _, item in pairs(items) do
        local itemId = item:getId()
        if hasPlayerAutolootItem(corpseOwner, itemId) then
            if currencyItems[itemId] then
                local worth = item:getWorth()
                corpseOwner:setBankBalance(corpseOwner:getBankBalance() + worth)
                corpseOwner:sendTextMessage(MESSAGE_STATUS_SMALL, string.format("Your balance increases by %d gold coins.", worth))
                item:remove()
            elseif not item:moveTo(corpseOwner) then
                warningCapacity = true
            end
        end
    end

    if warningCapacity then
        corpseOwner:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You no have capacity.")
    end
end

ec:register(3)

local talkAction = TalkAction(autoloot.talkaction)

function talkAction.onSay(player, words, param, type)
    local split = param:splitTrimmed(",")
    local action = split[1]
    if not action then
        player:showTextDialog(2160, string.format("Examples of use:\n%s add,gold coin\n%s remove,gold coin\n%s clear\n%s show\n%s edit\n\n~Available slots~\nfreeAccount: %d\npremiumAccount: %d\ncurrency to bank: %s", words, words, words, words, words, autoloot.freeAccountLimit, autoloot.premiumAccountLimit, autoloot.currencyToBank and "yes" or "no"), false)
        return false
    end

    if action == "clear" then
        setPlayerAutolootItems(player, {})
        player:sendCancelMessage("Autoloot list cleaned.")
        return false
    elseif action == "show" then
        local items = getPlayerAutolootItems(player)
        local description = {string.format('~ Your autoloot list, capacity: %d/%d ~\n', #items, getPlayerLimit(player))}
        for i, itemId in pairs(items) do
            description[#description +1] = string.format("%d) %s", i, ItemType(itemId):getName())
        end
        player:showTextDialog(2160, table.concat(description, '\n'), false)
        return false
    elseif action == "edit" then
        local items = getPlayerAutolootItems(player)
        if #items == 0 then
            -- Example
            items = {2160,2672,2432}
        end
        local description = {}
        for i, itemId in pairs(items) do
            description[#description +1] = ItemType(itemId):getName()
        end
        player:registerEvent("autolootTextEdit")
        player:showTextDialog(1948, string.format("To add articles you just have to write their IDs or names on each line\nfor example:\n\n%s", table.concat(description, '\n')), true, 666)
        textEditRequests[player:getGuid()] = true
        return false
    end

    local function getItemType()
        local itemType = ItemType(split[2])
        if not itemType or itemType:getId() == 0 then
            itemType = ItemType(math.max(tonumber(split[2]) or 0), 0)
            if not itemType or itemType:getId() == 0 then
                player:sendCancelMessage(string.format("The item %s does not exists!", split[2]))
                return false
            end
        end
        return itemType
    end

    if action == "add" then
        local itemType = getItemType()
        if itemType then
            local limits = getPlayerLimit(player)
            if #getPlayerAutolootItems(player) >= limits then
                player:sendCancelMessage(string.format("Your auto loot only allows you to add %d items.", limits))
                return false
            end

            if addPlayerAutolootItem(player, itemType:getId()) then
                player:sendCancelMessage(string.format("Perfect you have added to the list: %s", itemType:getName()))
            else
                player:sendCancelMessage(string.format("The item %s already exists!", itemType:getName()))
            end
        end
        return false
    elseif action == "remove" then
        local itemType = getItemType()
        if itemType then
            if removePlayerAutolootItem(player, itemType:getId()) then
                player:sendCancelMessage(string.format("Perfect you have removed to the list the article: %s", itemType:getName()))
            else
                player:sendCancelMessage(string.format("The item %s does not exists in the list.", itemType:getName()))
            end
        end
        return false
    end

    return false
end

talkAction:separator(" ")
talkAction:register()

local creatureEvent = CreatureEvent("autolootCleanCache")

function creatureEvent.onLogout(player)
    setPlayerAutolootItems(player, getPlayerAutolootItems(player))
    autolootCache[player:getGuid()] = nil
    return true
end

creatureEvent:register()

creatureEvent = CreatureEvent("autolootTextEdit")

function creatureEvent.onTextEdit(player, item, text)
    player:unregisterEvent("autolootTextEdit")

    local split = text:splitTrimmed("\n")
    local items = {}
    for index, name in pairs(split) do repeat
        local itemType = ItemType(name)
        if not itemType or itemType:getId() == 0 then
            itemType = ItemType(tonumber(name))
            if not itemType or itemType:getId() == 0 then
                break
            end

            break
        end

        items[#items +1] = itemType:getId()
    until true end
    setPlayerAutolootItems(player, items)
    player:sendCancelMessage(string.format("Perfect, you have modified the list of articles manually."))
    return true
end

creatureEvent:register()

When i have gold to bank on true i got error with GetCurrenyItems function.
Sorry, forgot to add an end, try this:
Lua:
local function scanContainer(cid, position)
   local cashList = { -- place currency ids here / their worth in gold
      [2148] = 1,
      [2152] = 100,
      [2160] = 10000,
   }

   local player = Player(cid)
   if not player then
      return
   end

   local corpse = Tile(position):getTopDownItem()
   if not corpse or not corpse:isContainer() then
      return
   end

   if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == cid then
      for a = corpse:getSize() - 1, 0, -1 do
         local containerItem = corpse:getItem(a)
         if containerItem then
            local coin = cashList[item.itemid]
            if coin then
               player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
               item:remove()
            else
               for b = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                  if player:getStorageValue(b) == containerItem:getId() then
                     containerItem:moveTo(player)
                  end
               end
            end
         end
      end
   end
end

   function onKill(player, target)
      if not target:isMonster() then
         return true
      end

      addEvent(scanContainer, 100, player:getId(), target:getPosition())
      return true
   end
 
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/creaturescripts/scripts/autoloot.lua:22: attempt to index global 'item' (a nil value)
stack traceback:
[C]: in function '__index'
data/creaturescripts/scripts/autoloot.lua:22: in function <data/creaturescripts/scripts/autoloot.lua:1>
 
Lua Script Error: [Main Interface]
in a timer event called from:
(Unknown scriptfile)
data/creaturescripts/scripts/autoloot.lua:22: attempt to index global 'item' (a nil value)
stack traceback:
[C]: in function '__index'
data/creaturescripts/scripts/autoloot.lua:22: in function <data/creaturescripts/scripts/autoloot.lua:1>
Lua:
local coins = {
    [2148] = 1,
    [2152] = 100,
    [2160] = 10000
}

local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end

    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else
            for value = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                if player:getStorageValue(value) == item.itemid then
                    item:moveTo(player)
                end
            end
        end
    end
end

function onKill(player, target)
    if not target:isMonster() then
        return true
    end

    addEvent(scanContainer, 100, player:getId(), target:getPosition())
    return true
end
 
Solution
Lua:
local coins = {
    [2148] = 1,
    [2152] = 100,
    [2160] = 10000
}

local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end

    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end

    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end

    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else
            for value = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                if player:getStorageValue(value) == item.itemid then
                    item:moveTo(player)
                end
            end
        end
    end
end

function onKill(player, target)
    if not target:isMonster() then
        return true
    end

    addEvent(scanContainer, 100, player:getId(), target:getPosition())
    return true
end
@Mjmackan oh one more my brother, can you add here also cap checker? i forgot about it sorry for that. I just checked it now, and if i dont have cap items still going to my backpack, if you fix it you are boss, thanks anyway!
 
joined: 15 september 2022, i dont want know more, and im not salvart go away troll.
I still need help, repps++
 
@Mjmackan oh one more my brother, can you add here also cap checker? i forgot about it sorry for that. I just checked it now, and if i dont have cap items still going to my backpack, if you fix it you are boss, thanks anyway!

If it's not working properly.. you may need to do (player:getFreeCapacity() / 100)
I remember needing to do that before, but can't remember the context of why. 🤷‍♂️

Also, if you want to tell the player about the non-looted items, un-greentext the parts I added in.
Was unsure if you wanted/needed that. 🤷‍♀️
Lua:
local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end
    
    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end
    
    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end
    
    --local unlootedItems = ""
    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else
            for value = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                if player:getStorageValue(value) == item.itemid then
                    if player:getFreeCapacity() >= item:getWeight() then -- If it's not working properly.. you may need to do (player:getFreeCapacity() / 100)
                        item:moveTo(player)
                    --else
                    --    if unlootedItems ~= "" then
                    --        unlootedItems = unlootedItems .. ", "
                    --    end
                    --    unlootedItems = unlootedItems .. item:getName()
                    end
                end
            end
        end
    end
    
    --if unlootedItems ~= "" then
    --    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Autoloot: Unable to loot -> " .. unlootedItems .. " -> due to insufficient capacity.")
    --end
end
 
Why you help this guy, he is a pure leecher, gives nothing in return just trying to get cash from poor people on ots.
I help everyone, regardless of their motives, in most cases.

The original poster of the thread is not the only person who benefits.
There are many other people who frequent the forum who are looking to learn, or may have a similar problem in the future.

Helping one potential leecher, potentially benefits hundreds of people into the future.
I don't see any downside, aside from a little time investment from myself.

Cheers.
 
If it's not working properly.. you may need to do (player:getFreeCapacity() / 100)
I remember needing to do that before, but can't remember the context of why. 🤷‍♂️

Also, if you want to tell the player about the non-looted items, un-greentext the parts I added in.
Was unsure if you wanted/needed that. 🤷‍♀️
Lua:
local function scanContainer(cid, position)
    local player = Player(cid)
    if not player then
        return
    end
   
    local tile = Tile(position)
    local corpse = tile and tile:getTopDownItem()
    if not corpse or not corpse:isContainer() then
        return
    end
   
    if corpse:getType():isCorpse() and corpse:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) ~~= cid then
        return
    end
   
    --local unlootedItems = ""
    for _, item in pairs(corpse:getItems(true)) do
        local coin = coins[item.itemid]
        if coin then
            player:setBankBalance(player:getBankBalance() + (item:getCount() * coin))
            item:remove()
        else
            for value = AUTOLOOT_STORAGE_START, AUTOLOOT_STORAGE_END do
                if player:getStorageValue(value) == item.itemid then
                    if player:getFreeCapacity() >= item:getWeight() then -- If it's not working properly.. you may need to do (player:getFreeCapacity() / 100)
                        item:moveTo(player)
                    --else
                    --    if unlootedItems ~= "" then
                    --        unlootedItems = unlootedItems .. ", "
                    --    end
                    --    unlootedItems = unlootedItems .. item:getName()
                    end
                end
            end
        end
    end
   
    --if unlootedItems ~= "" then
    --    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Autoloot: Unable to loot -> " .. unlootedItems .. " -> due to insufficient capacity.")
    --end
end
Thank you works! big rep+ for you @Xikini and respect, we can close this theard.
 
Last edited by a moderator:
Status
Not open for further replies.
Back
Top