• 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 Drop item inside named monsters

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
USING TFS 1.2
Hey, I'm looking for an event that can be activated either through a globalevent or talkaction (e.g., /chestevent 6 for 6 hours). Here's how the event should work: when activated, the monsters that are killed during the event should give players a chance to loot a specified item, but it wont work if you passed the level range that is above described for example like in this table if you're in range of 50 and 150 it get a chance to loot it, if its below or above, it wont work anymore
Lua:
local Monsters = {
    ["monster nr1","monster nr2", "monster nr3"] = {
        { itemId = 20001, chance = 0.005 },
        { itemId = 20002, chance = 0.01 }
        },
        levelRange = { from = 50, to = 150 }
    },
    ["rat nr1","rat nr2", "rat nr3"] = {
        { itemId = 20005, chance = 0.005 },
        { itemId = 20006, chance = 0.01 }
        },
        levelRange = { from = 150, to = 250}
    },
 
Solution
I made a script in less than 30 minutes, and it's already ready. Watch the video... everything is working fine... using the command /chestevent start to begin, /chestevent stop to stop the event... and according to the boss's coordinates... you need to configure correctly fromPos and toPos and where the boss is to kill and win an item with a chance from 0 to 100%, your preference... watch the video.


creaturescript
name.lua
Lua:
-- Configuration of event bosses and their drop chances
local eventBosses = {
    ["Rotworm,Rotworm Boss,Rotworm Skeleton"] = {
        levelRanges = { min = 0, max = 50 },
        fromPos = Position(716, 601, 8),
        toPos = Position(726, 608, 8),
        dropChances = {
            { itemId =...
Creaturescript:

Lua:
function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end
    
    local monsterName = targetMonster:getName()
    
    -- Definiowanie szans na zdobycie przedmiotów dla każdego potwora
    local dropChances = {
        ["Monster1"] = {itemId = 1111, chance = 500},  -- Item 1 z Monster 1
        ["Monster2"] = {itemId = 2222, chance = 1350}, -- Item 2 z Monster 2
        ["Monster3"] = {itemId = 3333, chance = 2500}, -- Item 3 z Monster 3
        -- Dodaj więcej linii według potrzeb
    }
    
    local dropChance = dropChances[monsterName]
    if not dropChance then
        return true  -- Brak szans na zdobycie przedmiotu z tego potwora
    end
    
    local randomChance = math.random(1, 10000)
    if randomChance <= dropChance.chance then
        creature:addItem(dropChance.itemId, 1)
        creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
    
    return true
end
 
Creaturescript:

Lua:
function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end
    
    local monsterName = targetMonster:getName()
    
    -- Definiowanie szans na zdobycie przedmiotów dla każdego potwora
    local dropChances = {
        ["Monster1"] = {itemId = 1111, chance = 500},  -- Item 1 z Monster 1
        ["Monster2"] = {itemId = 2222, chance = 1350}, -- Item 2 z Monster 2
        ["Monster3"] = {itemId = 3333, chance = 2500}, -- Item 3 z Monster 3
        -- Dodaj więcej linii według potrzeb
    }
    
    local dropChance = dropChances[monsterName]
    if not dropChance then
        return true  -- Brak szans na zdobycie przedmiotu z tego potwora
    end
    
    local randomChance = math.random(1, 10000)
    if randomChance <= dropChance.chance then
        creature:addItem(dropChance.itemId, 1)
        creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
    
    return true
end

Are you sure about creature:addItem(dropChance.itemId, 1) ?
I can't find this function anywhere.

Is it a custom function you created?

-- Edit
Ah I see, it's player:addItem, I'm just dumb. lol
When I read through it earlier, I thought you were modifying the monsters' loot table.
 
Last edited:
I made a script in less than 30 minutes, and it's already ready. Watch the video... everything is working fine... using the command /chestevent start to begin, /chestevent stop to stop the event... and according to the boss's coordinates... you need to configure correctly fromPos and toPos and where the boss is to kill and win an item with a chance from 0 to 100%, your preference... watch the video.


creaturescript
name.lua
Lua:
-- Configuration of event bosses and their drop chances
local eventBosses = {
    ["Rotworm,Rotworm Boss,Rotworm Skeleton"] = {
        levelRanges = { min = 0, max = 50 },
        fromPos = Position(716, 601, 8),
        toPos = Position(726, 608, 8),
        dropChances = {
            { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
            { itemId = 2160, chance = 1.0, count = 2 }, -- 100% chance, 2 items
        }
    },
    ["Demon"] = {
        levelRanges = { min = 51, max = 100 },
        fromPos = Position(300, 300, 7),
        toPos = Position(400, 400, 7),
        dropChances = {
            { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
            { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
        }
    },
    ["Rat"] = {
        levelRanges = { min = 101, max = 150 },
        fromPos = Position(500, 500, 7),
        toPos = Position(600, 600, 7),
        dropChances = {
            { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
            { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
        }
    },
    -- Add other boss configurations as needed
}

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()
    local eventEndTime = Game.getStorageValue(GlobalStorage.ChestEvent) or 0

    for bossNames, info in pairs(eventBosses) do
        local names = bossNames:split(",")
        for _, name in ipairs(names) do
            if name:lower() == bossName:lower() then
                if os.time() <= eventEndTime and playerLevel >= info.levelRanges.min and playerLevel <= info.levelRanges.max then
                    return isPositionInRange(playerPos, info.fromPos, info.toPos)
                end
            end
        end
    end

    return false
end

function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()

    if not isPlayerInEvent(player, monsterName) then
        return true
    end

    for bossNames, info in pairs(eventBosses) do
        local names = bossNames:split(",")
        for _, name in ipairs(names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #info.dropChances)
                local chosenDrop = info.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    player:addItem(chosenDrop.itemId, itemCount)
                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end

                break
            end
        end
    end

    return true
end

XML.
XML:
<event type="kill" name="BossEvent" script="name.lua"/>
login.lua.
player:registerEvent("BossEvent")


talkaction.
name.lua
Lua:
local duration = 6 * 60 * 60 -- 6 hours in seconds

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(GlobalStorage.ChestEvent)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(GlobalStorage.ChestEvent, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, os.time() + duration)
            Game.broadcastMessage("Chest Event activated for 6 hours.")
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, 0)
            Game.broadcastMessage("Chest Event deactivated.")
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '/chestevent start' or '/chestevent stop'.")
    end

    return false
end


Go to data/lib/core/storage and add this.
Lua:
GlobalStorage = {
    ChestEvent = 15000,
}
 
Last edited:
Solution
I made a script in less than 30 minutes, and it's already ready. Watch the video... everything is working fine... using the command /chestevent start to begin, /chestevent stop to stop the event... and according to the boss's coordinates... you need to configure correctly fromPos and toPos and where the boss is to kill and win an item with a chance from 0 to 100%, your preference... watch the video.


creaturescript
name.lua
Lua:
-- Definition of level ranges
local levelRanges = {
    { min = 0, max = 50 },
    { min = 51, max = 100 },
    -- Add other level ranges as needed
}

local function isPlayerInRange(playerLevel)
    for _, range in ipairs(levelRanges) do
        if playerLevel >= range.min and playerLevel <= range.max then
            return true
        end
    end
    return false
end

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

-- Configuration of event bosses and their drop chances
local eventBosses = {
    ["Rotworm"] = {
        fromPos = Position(716, 601, 8),
        toPos = Position(726, 608, 8),
        dropChances = {
            { itemId = 6527, chance = 1.0 }, -- 100% chance
            { itemId = 2160, chance = 1.0 }  -- 100% chance
        }
    },
    ["Demon"] = {
        fromPos = Position(300, 300, 7),
        toPos = Position(400, 400, 7),
        dropChances = {
            { itemId = 9876, chance = 0.05 }, -- 5% chance
            { itemId = 5432, chance = 0.95 }  -- 95% chance
        }
    },
    ["Rat"] = {
        fromPos = Position(500, 500, 7),
        toPos = Position(600, 600, 7),
        dropChances = {
            { itemId = 1111, chance = 0.5 }, -- 50% chance
            { itemId = 2222, chance = 0.5 }  -- 50% chance
        }
    },
    -- Add other bosses and their drop information as needed
}

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()

    local eventEndTime = Game.getStorageValue(GlobalStorage.ChestEvent) or 0
    local bossInfo = eventBosses[bossName]

    return os.time() <= eventEndTime
       and isPlayerInRange(playerLevel)
       and isPositionInRange(playerPos, bossInfo.fromPos, bossInfo.toPos)
end

function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()
    local bossInfo = eventBosses[monsterName]
    if not bossInfo or not isPlayerInEvent(player, monsterName) then
        return true
    end

    local monsterPos = targetMonster:getPosition()
    for _, dropChance in ipairs(bossInfo.dropChances) do
        if math.random() <= dropChance.chance then
            player:addItem(dropChance.itemId, 1)
            monsterPos:sendMagicEffect(CONST_ME_MAGIC_GREEN)
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(dropChance.itemId):getName())
            break
        end
    end
 
    return true
end

XML:
<event type="kill" name="BossEvent" script="name.lua" />
login.lua
Code:
player:registerEvent("BossEvent")
talkaction.
name.lua
Lua:
local duration = 6 * 60 * 60 -- 6 hours in seconds

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(GlobalStorage.ChestEvent)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(GlobalStorage.ChestEvent, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, os.time() + duration)
            Game.broadcastMessage("Chest Event activated for 6 hours.")
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, 0)
            Game.broadcastMessage("Chest Event deactivated.")
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '/chestevent start' or '/chestevent stop'.")
    end

    return false
end

Go to data/lib/core/storage and add this.
Lua:
GlobalStorage = {
    ChestEvent = 15000,
}
Thats impressive :eek:
 
@Lopaskurwa I edited the post... just the CreatureScript. I fixed and improved it... added each level according to the boss, and included the quantity of the item that will drop... all while keeping everything normal. Be happy :)
Question does it support like ["Rotworm, Rotworm Boss, Rotworm Skeleton"] = { basically multiple monster names in one table
 
i would love a revscript for this idea aswell ;)
I made a script in less than 30 minutes, and it's already ready. Watch the video... everything is working fine... using the command /chestevent start to begin, /chestevent stop to stop the event... and according to the boss's coordinates... you need to configure correctly fromPos and toPos and where the boss is to kill and win an item with a chance from 0 to 100%, your preference... watch the video.


creaturescript
name.lua
Lua:
-- Configuration of event bosses and their drop chances
local eventBosses = {
    ["Rotworm"] = {
        levelRanges = { min = 0, max = 50 },
        fromPos = Position(716, 601, 8),
        toPos = Position(726, 608, 8),
        dropChances = {
            { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
            { itemId = 2160, chance = 1.0, count = 2 }, -- 100% chance, 2 items
        }
    },
    ["Demon"] = {
        levelRanges = { min = 51, max = 100 },
        fromPos = Position(300, 300, 7),
        toPos = Position(400, 400, 7),
        dropChances = {
            { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
            { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
        }
    },
    ["Rat"] = {
        levelRanges = { min = 101, max = 150 },
        fromPos = Position(500, 500, 7),
        toPos = Position(600, 600, 7),
        dropChances = {
            { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
            { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
        }
    },
  -- Add other boss configurations as needed
}

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()
    local eventEndTime = Game.getStorageValue(GlobalStorage.ChestEvent) or 0
    local bossInfo = eventBosses[bossName]

    if os.time() <= eventEndTime then
        if playerLevel >= bossInfo.levelRanges.min and playerLevel <= bossInfo.levelRanges.max then
            return isPositionInRange(playerPos, bossInfo.fromPos, bossInfo.toPos)
        end
    end

    return false
end

function onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()
    local bossInfo = eventBosses[monsterName]
    if not bossInfo or not isPlayerInEvent(player, monsterName) then
        return true
    end

    local monsterPos = targetMonster:getPosition()
    local dropIndex = math.random(1, #bossInfo.dropChances)

    local chosenDrop = bossInfo.dropChances[dropIndex]
    local dropChance = math.random()

    if dropChance <= chosenDrop.chance then
        player:addItem(chosenDrop.itemId, chosenDrop.count)
        monsterPos:sendMagicEffect(CONST_ME_MAGIC_GREEN)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. chosenDrop.count .. "x)")
    end

    return true
end

XML:
<event type="kill" name="BossEvent" script="name.lua" />
login.lua
Code:
player:registerEvent("BossEvent")
talkaction.
name.lua
Lua:
local duration = 6 * 60 * 60 -- 6 hours in seconds

function onSay(player, words, param)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(GlobalStorage.ChestEvent)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(GlobalStorage.ChestEvent, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, os.time() + duration)
            Game.broadcastMessage("Chest Event activated for 6 hours.")
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(GlobalStorage.ChestEvent, 0)
            Game.broadcastMessage("Chest Event deactivated.")
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '/chestevent start' or '/chestevent stop'.")
    end

    return false
end

Go to data/lib/core/storage and add this.
Lua:
GlobalStorage = {
    ChestEvent = 15000,
}
why so much files, im pretty sure it can be just 1x revscript file ? no ? @Xikini maybe can lend your insight
 
i would love a revscript for this idea aswell ;)

why so much files, im pretty sure it can be just 1x revscript file ? no ? @Xikini maybe can lend your insight
I do it for the RevScripts, yes... because this OP is using TFS 1.2, that's why I separated each function for him to understand. If you want, I can do it, yes. I'll just be back tonight and do it for you :)

Details about RevScripts only accept from version 1.3+; TFS 1.2 and below do not accept, that's why you understand xD.
 
I do it for the RevScripts, yes... because this OP is using TFS 1.2, that's why I separated each function for him to understand. If you want, I can do it, yes. I'll just be back tonight and do it for you :)

Details about RevScripts only accept from version 1.3+; TFS 1.2 and below do not accept, that's why you understand xD.
alot 1,2tfs+ projects have installed revscripts, but yeah in generaly it comes from 1,3+. i would love a revscript
 
Question does it support like ["Rotworm, Rotworm Boss, Rotworm Skeleton"] = { basically multiple monster names in one table
Sure, it's possible. I just updated the post. Take a look...
Post automatically merged:

why so much files, im pretty sure it can be just 1x revscript file ? no ? @Xikini maybe can lend your insight
It's ready to use... here it is.

Lua:
local config = {
    duration = 6 * 60 * 60, -- 6 hours in seconds
    storageKey = 1000,  -- Representative number for storage
    talkactionCommand = "/chestevent",
    eventMessageStart = "Chest Event activated for 6 hours.",
    eventMessageStop = "Chest Event deactivated.",
    eventBosses = {
        {
            names = {"Rotworm", "Rotworm Boss", "Rotworm Skeleton"},
            levelRanges = { min = 0, max = 50 },
            fromPos = Position(716, 601, 8),
            toPos = Position(726, 608, 8),
            dropChances = {
                { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
                { itemId = 2160, chance = 1.0, count = 2 }  -- 100% chance, 2 items
            }
        },
        {
            names = {"Demon"},
            levelRanges = { min = 51, max = 100 },
            fromPos = Position(300, 300, 7),
            toPos = Position(400, 400, 7),
            dropChances = {
                { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
                { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
            }
        },
        {
            names = {"Rat"},
            levelRanges = { min = 101, max = 150 },
            fromPos = Position(500, 500, 7),
            toPos = Position(600, 600, 7),
            dropChances = {
                { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
                { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
            }
        },
        -- Add other boss configurations as needed
    },
}

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()
    local eventEndTime = Game.getStorageValue(config.storageKey) or 0

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == bossName:lower() then
                if os.time() <= eventEndTime and playerLevel >= boss.levelRanges.min and playerLevel <= boss.levelRanges.max then
                    return isPositionInRange(playerPos, boss.fromPos, boss.toPos)
                end
            end
        end
    end

    return false
end

local creatureevent = CreatureEvent("BossKill")

function creatureevent.onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()

    if not isPlayerInEvent(player, monsterName) then
        return true
    end

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #boss.dropChances)
                local chosenDrop = boss.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    player:addItem(chosenDrop.itemId, itemCount)
                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end

                break
            end
        end
    end

    return true
end

creatureevent:register()

local creatureevent2 = CreatureEvent("registerlogin")

function creatureevent2.onLogin(player)
    player:registerEvent("BossKill")
    return true
end

creatureevent2:register()

local talkaction = TalkAction(config.talkactionCommand)

function talkaction.onSay(player, words, param, type)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(config.storageKey)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(config.storageKey, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(config.storageKey, os.time() + config.duration)
            Game.broadcastMessage(config.eventMessageStart)
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(config.storageKey, 0)
            Game.broadcastMessage(config.eventMessageStop)
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '" .. config.talkactionCommand .. " start' or '" .. config.talkactionCommand .. " stop'.")
    end

    return false
end

talkaction:separator(" ")
talkaction:register()
 
Last edited:
Sure, it's possible. I just updated the post. Take a look...
Post automatically merged:


It's ready to use... here it is.

Lua:
local config = {
    duration = 6 * 60 * 60, -- 6 hours in seconds
    storageKey = 1000,  -- Representative number for storage
    talkactionCommand = "/chestevent",
    eventMessageStart = "Chest Event activated for 6 hours.",
    eventMessageStop = "Chest Event deactivated.",
    eventBosses = {
        {
            names = {"Rotworm", "Rotworm Boss", "Rotworm Skeleton"},
            levelRanges = { min = 0, max = 50 },
            fromPos = Position(716, 601, 8),
            toPos = Position(726, 608, 8),
            dropChances = {
                { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
                { itemId = 2160, chance = 1.0, count = 2 }  -- 100% chance, 2 items
            }
        },
        {
            names = {"Demon"},
            levelRanges = { min = 51, max = 100 },
            fromPos = Position(300, 300, 7),
            toPos = Position(400, 400, 7),
            dropChances = {
                { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
                { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
            }
        },
        {
            names = {"Rat"},
            levelRanges = { min = 101, max = 150 },
            fromPos = Position(500, 500, 7),
            toPos = Position(600, 600, 7),
            dropChances = {
                { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
                { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
            }
        },
        -- Add other boss configurations as needed
    },
}

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()
    local eventEndTime = Game.getStorageValue(config.storageKey) or 0

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == bossName:lower() then
                if os.time() <= eventEndTime and playerLevel >= boss.levelRanges.min and playerLevel <= boss.levelRanges.max then
                    return isPositionInRange(playerPos, boss.fromPos, boss.toPos)
                end
            end
        end
    end

    return false
end

local creatureevent = CreatureEvent("BossKill")

function creatureevent.onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()

    if not isPlayerInEvent(player, monsterName) then
        return true
    end

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #boss.dropChances)
                local chosenDrop = boss.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    player:addItem(chosenDrop.itemId, itemCount)
                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end

                break
            end
        end
    end

    return true
end

creatureevent:register()

local creatureevent2 = CreatureEvent("registerlogin")

function creatureevent2.onLogin(player)
    player:registerEvent("BossKill")
    return true
end

creatureevent2:register()

local talkaction = TalkAction(config.talkactionCommand)

function talkaction.onSay(player, words, param, type)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(config.storageKey)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(config.storageKey, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(config.storageKey, os.time() + config.duration)
            Game.broadcastMessage(config.eventMessageStart)
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(config.storageKey, 0)
            Game.broadcastMessage(config.eventMessageStop)
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '" .. config.talkactionCommand .. " start' or '" .. config.talkactionCommand .. " stop'.")
    end

    return false
end

talkaction:separator(" ")
talkaction:register()
:))) Gatitute from many of us :) will test and tell how it goes :)
 
Feel free to add a reward bag or a custom bag if you'd like and make good use of it! Check out the video for more details!
look for this line:

Code:
 for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #boss.dropChances)
                local chosenDrop = boss.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    player:addItem(chosenDrop.itemId, itemCount)
                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end
change to:
Lua:
for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #boss.dropChances)
                local chosenDrop = boss.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    local backpack = player:addItem(26144, 1, false)
                    local isStackable = isItemStackable(chosenDrop.itemId)

                    if isStackable then
                        local fullStacks = math.floor(itemCount / 100)
                        local remainder = itemCount % 100

                        for i = 1, fullStacks do
                            local addedItem = backpack:addItem(chosenDrop.itemId, 100)
                            if not addedItem then
                                break
                            end
                        end

                        if remainder > 0 then
                            local addedRemainder = backpack:addItem(chosenDrop.itemId, remainder)
                            if not addedRemainder then
                            end
                        end
                    else
                        local addedNonStackable = backpack:addItem(chosenDrop.itemId, itemCount)
                        if not addedNonStackable then
                        end
                    end

                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)  
player:sendTextMessage(MESSAGE_INFO_DESCR, "Reward Bag added to your inventory: " .. ItemType(config.rewardBag):getName() .. ", " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end
                end
Please pay attention; you can change the backpack ID or reward or add anything else interesting you'd like, okay?
local backpack = player:addItem(26144, 1, false)
 
Last edited:
Sure, it's possible. I just updated the post. Take a look...
Post automatically merged:


It's ready to use... here it is.

Lua:
local config = {
    duration = 6 * 60 * 60, -- 6 hours in seconds
    storageKey = 1000,  -- Representative number for storage
    talkactionCommand = "/chestevent",
    eventMessageStart = "Chest Event activated for 6 hours.",
    eventMessageStop = "Chest Event deactivated.",
    eventBosses = {
        {
            names = {"Rotworm", "Rotworm Boss", "Rotworm Skeleton"},
            levelRanges = { min = 0, max = 50 },
            fromPos = Position(716, 601, 8),
            toPos = Position(726, 608, 8),
            dropChances = {
                { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
                { itemId = 2160, chance = 1.0, count = 2 }  -- 100% chance, 2 items
            }
        },
        {
            names = {"Demon"},
            levelRanges = { min = 51, max = 100 },
            fromPos = Position(300, 300, 7),
            toPos = Position(400, 400, 7),
            dropChances = {
                { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
                { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
            }
        },
        {
            names = {"Rat"},
            levelRanges = { min = 101, max = 150 },
            fromPos = Position(500, 500, 7),
            toPos = Position(600, 600, 7),
            dropChances = {
                { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
                { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
            }
        },
        -- Add other boss configurations as needed
    },
}

local function isPositionInRange(position, fromPos, toPos)
    return position.x >= fromPos.x and position.x <= toPos.x
       and position.y >= fromPos.y and position.y <= toPos.y
       and position.z == fromPos.z
end

local function isPlayerInEvent(player, bossName)
    local playerLevel = player:getLevel()
    local playerPos = player:getPosition()
    local eventEndTime = Game.getStorageValue(config.storageKey) or 0

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == bossName:lower() then
                if os.time() <= eventEndTime and playerLevel >= boss.levelRanges.min and playerLevel <= boss.levelRanges.max then
                    return isPositionInRange(playerPos, boss.fromPos, boss.toPos)
                end
            end
        end
    end

    return false
end

local creatureevent = CreatureEvent("BossKill")

function creatureevent.onKill(creature, target)
    local targetMonster = target:getMonster()
    if not targetMonster then
        return true
    end

    local player = creature:getPlayer()
    if not player then
        return true
    end

    local monsterName = targetMonster:getName()

    if not isPlayerInEvent(player, monsterName) then
        return true
    end

    for _, boss in ipairs(config.eventBosses) do
        for _, name in ipairs(boss.names) do
            if name:lower() == monsterName:lower() then
                local dropIndex = math.random(1, #boss.dropChances)
                local chosenDrop = boss.dropChances[dropIndex]
                local dropChance = math.random()

                if dropChance <= chosenDrop.chance then
                    local itemCount = chosenDrop.count or 1
                    player:addItem(chosenDrop.itemId, itemCount)
                    targetMonster:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "Item added to your backpack: " .. ItemType(chosenDrop.itemId):getName() .. " (" .. itemCount .. "x)")
                end

                break
            end
        end
    end

    return true
end

creatureevent:register()

local creatureevent2 = CreatureEvent("registerlogin")

function creatureevent2.onLogin(player)
    player:registerEvent("BossKill")
    return true
end

creatureevent2:register()

local talkaction = TalkAction(config.talkactionCommand)

function talkaction.onSay(player, words, param, type)
    if not player:getGroup():getAccess() then
        return true
    end

    if player:getAccountType() < ACCOUNT_TYPE_GOD then
        return false
    end

    local storedTime = Game.getStorageValue(config.storageKey)
    if storedTime == nil then
        storedTime = 0
        Game.setStorageValue(config.storageKey, storedTime)
    end

    local eventActive = storedTime > os.time()

    if param == "start" then
        if eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is already active.")
        else
            Game.setStorageValue(config.storageKey, os.time() + config.duration)
            Game.broadcastMessage(config.eventMessageStart)
        end
    elseif param == "stop" then
        if not eventActive then
            player:sendTextMessage(MESSAGE_STATUS_WARNING, "Chest Event is not active.")
        else
            Game.setStorageValue(config.storageKey, 0)
            Game.broadcastMessage(config.eventMessageStop)
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Invalid parameter. Use '" .. config.talkactionCommand .. " start' or '" .. config.talkactionCommand .. " stop'.")
    end

    return false
end

talkaction:separator(" ")
talkaction:register()

Doesnt work for me, none errors, the command and message appears about event, but monsters aint droping shit.

+ just now noticed this evevent as you start it starts for all locations, would be epicly nice to seperate them, so i can like set event in different room. like /chestevent 1 or /chestevent room1 etc etc.



saw about bag thingy, its very cool, if you could can you add it up also ? im abit comfused what to change
 
works super with item/bp drop :)))

Can i ask you please for a version, so you can like

example:
Lua:
    eventBosses = {

              LOCATION 1
        {
            names = {"Rotworm", "Rotworm Boss", "Rotworm Skeleton"},
            levelRanges = { min = 0, max = 50 },
            fromPos = Position(716, 601, 8),
            toPos = Position(726, 608, 8),
            dropChances = {
                { itemId = 6527, chance = 1.0, count = 3 }, -- 100% chance, 3 items
                { itemId = 2160, chance = 1.0, count = 2 }  -- 100% chance, 2 items
            }
        },
        {     LOCATION 2
            names = {"Demon"},
            levelRanges = { min = 51, max = 100 },
            fromPos = Position(300, 300, 7),
            toPos = Position(400, 400, 7),
            dropChances = {
                { itemId = 9876, chance = 0.05, count = 1 }, -- 5% chance, 1 item
                { itemId = 5432, chance = 0.95, count = 2 }  -- 95% chance, 2 items
            }
        },
        {    LOCATION 3
            names = {"Rat"},
            levelRanges = { min = 101, max = 150 },
            fromPos = Position(500, 500, 7),
            toPos = Position(600, 600, 7),
            dropChances = {
                { itemId = 1111, chance = 0.5, count = 1 }, -- 50% chance, 1 item
                { itemId = 2222, chance = 0.5, count = 1 }  -- 50% chance, 1 item
            }
        },

So you have to like /chestevent location 3 , to enable the event only in Rats location.

would be epic, i hope i explain it understandaible
 
Back
Top