• 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!

RevScripts Print unique message from the table in default drop loot script

Obito

0x1337
Joined
Feb 17, 2011
Messages
350
Solutions
8
Reaction score
144
Location
Egypt
I'm trying to print a unique message for the monsters that are in the table if the player killed, For example when I killed Archmage monster this error appear

Code:
Lua Script Error: [Event Interface]
data/events/scripts/monster.lua:Monster@onDropLoot
...ta\scripts\eventcallbacks\monster\default_onDropLoot.lua:41: attempt to index local 'item' (a boolean value)
stack traceback:
        [C]: in function '__index'
        ...ta\scripts\eventcallbacks\monster\default_onDropLoot.lua:41: in function <...ta\scripts\eventcallbacks\monster\default_onDropLoot.lua:11>
        ...esktop\Unique 1.4.2\data\scripts/lib\event_callbacks.lua:127: in function 'EventCallback'
        data/events/scripts/monster.lua:3: in function <data/events/scripts/monster.lua:1>
this the script i tried so far
Lua:
local List_Monsters = {
    {
        name = "Archmage",
        items = {6551}
    },
   
}

local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    local monsterLoot = mType:getLoot()

    local increaseLoot = false
    local extraLootPercent = 15
    if player and player:getStorageValue(12500) > os.time() then
        increaseLoot = true
    end

    for i = 1, #monsterLoot do
        if increaseLoot then
            local default = monsterLoot[i]
            if default.chance < 100000 then
                default.chance = default.chance + ((extraLootPercent * (default.chance / 1000) / 100) * 1000)
            end
        end
local item = corpse:createLootItem(monsterLoot[i])
if not item then
    print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
else
    -- Check if the looted item is in the table
    for j = 1, #List_Monsters do
        if mType:getName() == List_Monsters[j].name then
            for k = 1, #List_Monsters[j].items do
                if item:getId() == List_Monsters[j].items[k] then
                    player:sendTextMessage(MESSAGE_LOOT, "You have looted a special item from ".. List_Monsters[j].name .."!")
                end
            end
        end
    end
end
end

    if player then
        local text = ("%s of %s: %s"):format((increaseLoot and "Increased loot" or "Loot"), mType:getNameDescription(), corpse:getContentDescription())
        player:sendTextMessage(MESSAGE_LOOT, text)
    end
end

ec:register()
I guess this error is likely occurring because the createLootItem function is returning false instead of an item object. This can happen if the function fails to create the loot item for some reason I don't know really :/
 
Lua:
local monsterList = {
    ["Archmage"] = {6551}
}

local ec = EventCallback
ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    local monsterLoot = mType:getLoot()

    local increaseLoot = false
    local extraLootPercent = 15

    if player and player:getStorageValue(12500) > os.time() then
        increaseLoot = true
    end

    for i = 1, #monsterLoot do
        if increaseLoot then
            local default = monsterLoot[i]
            if default.chance < 100000 then
                default.chance = default.chance + ((extraLootPercent * (default.chance / 1000) / 100) * 1000)
            end
        end

        local item = corpse:createLootItem(monsterLoot[i])
        if not item then
            print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
        end
    end

    if player then
        local text = ("%s of %s: %s"):format((increaseLoot and "Increased loot" or "Loot"), mType:getNameDescription(), corpse:getContentDescription())
        player:sendTextMessage(MESSAGE_LOOT, text)

        local displayed = {}
        local specialList = monsterList[self:getName()]

        if specialList then
            local items = corpse:getItems()
            for i = 1, #items do
                local item = items[i]
                if table.contains(specialList, item:getId()) and not displayed[item:getId()] then
                    displayed[item:getId()] = 1
                    player:sendTextMessage(MESSAGE_LOOT, "You have looted a special item (" .. item:getName() .. ") from " .. mType:getNameDescription() .. ".")
                end
            end
        end
    end
end

ec:register()
 
Lua:
local monsterList = {
    ["Archmage"] = {6551}
}

local ec = EventCallback
ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    local monsterLoot = mType:getLoot()

    local increaseLoot = false
    local extraLootPercent = 15

    if player and player:getStorageValue(12500) > os.time() then
        increaseLoot = true
    end

    for i = 1, #monsterLoot do
        if increaseLoot then
            local default = monsterLoot[i]
            if default.chance < 100000 then
                default.chance = default.chance + ((extraLootPercent * (default.chance / 1000) / 100) * 1000)
            end
        end

        local item = corpse:createLootItem(monsterLoot[i])
        if not item then
            print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
        end
    end

    if player then
        local text = ("%s of %s: %s"):format((increaseLoot and "Increased loot" or "Loot"), mType:getNameDescription(), corpse:getContentDescription())
        player:sendTextMessage(MESSAGE_LOOT, text)

        local displayed = {}
        local specialList = monsterList[self:getName()]

        if specialList then
            local items = corpse:getItems()
            for i = 1, #items do
                local item = items[i]
                if table.contains(specialList, item:getId()) and not displayed[item:getId()] then
                    displayed[item:getId()] = 1
                    player:sendTextMessage(MESSAGE_LOOT, "You have looted a special item (" .. item:getName() .. ") from " .. mType:getNameDescription() .. ".")
                end
            end
        end
    end
end

ec:register()
Wow that worked thanks so much @Roddet was been trying for hours and I didn't notice that it could be done by using corpse:getItems
 
Back
Top