• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Action Chest With key use, random reward

froie

Expert Mapper
Joined
May 27, 2013
Messages
196
Reaction score
4
Location
Canada
Hello OTland, I was wondering if I could get some help with this script, I was hoping someone could help me with a script that is a Chest, that will give a random reward (by whatever items you put in the script, example magic plate armor, crystal coins, potions) but to open this chest you will need to use a key.. that will break after 1 use after you get your reward for example the key would be ID: 16015, but the chest Is a chest that can be opened as many times as long as you have this key of ID :16015. Any help is greatly appreciated!
 
after 3 years of being a member id think you know where to post requests
 
Give him a break, he's only an expert mapper. :rolleyes: ;)

Because the idea is fun, I created this script for all to use.

As I'm unsure how @froie wants the script to exactly run..
This script requires the key to be in the player inventory, when the chest is used.
If the player does not have a key on them, it simply gives out no items.

As this is a very simple script, I'm not adding additional checks, for weight/capacity/inventory space.
If the player does not have enough of the above, the item will fall to the players feet.

Untested, like all my scripts.

TFS 0.3.7

Code:
-- chest requires the actionid
<action actionid="45001" event="script" value="xikini_example_scripts/key_required_chest.lua"/>
Code:
local config = {
   [1] = { item_id = 2152, item_count = 1 },
   [2] = { item_id = 2152, item_count = 2 },
   [3] = { item_id = 2152, item_count = 3 },
   [4] = { item_id = 2152, item_count = 4 }
}

local key_id = 16015 -- item_id of the key required to obtain reward

function onUse(cid, item, fromPosition, itemEx, toPosition)

   if getPlayerItemCount(cid, key_id) <= 0 then
       doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
       doPlayerSendCancel(cid, "I only open to those whom have acquired a means in which to fill my heart.")
       return true
   end
  
   doPlayerRemoveItem(cid, key_id, 1)
   local rand = math.random(#config)
   doPlayerAddItem(cid, config[rand].item_id, config[rand].item_count, true)
   doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You've found one of my keys!\nYou shall be rewarded handsomely!\nYou have received " .. config[rand].item_count .. " " .. config[rand].item_id .. ".")

   return true
end
 
Code:
local t = {
    ['Loot Chest [Tier 1]'] = {
    effect = CONST_ME_FIREAREA,
    color  = TEXTCOLOR_RED,
    items  = {
            {id = 2146, minCount = 1, maxCount = 30},
            {id = 2147, minCount = 5, maxCount = 45},
            {id = 2149, minCount = 1, maxCount = 50},
            {id = 2150, minCount = 10, maxCount = 30}
        }
    },
    ['Loot Chest [Tier 2]'] = {
    effect = CONST_ME_HOLYAREA,
    color  = TEXTCOLOR_YELLOW,
    items  = {
            {id = 7759, minCount = 1, maxCount = 30},
            {id = 7760, minCount = 5, maxCount = 45},
            {id = 7761, minCount = 1, maxCount = 50},
            {id = 7762, minCount = 10, maxCount = 30}
        }
    }
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local boxName = getItemName(itemEx.uid)
    local tmp     = t[boxName]
    if tmp then
        local tab  = tmp.items
        local size = #tab
        while getContainerSize(itemEx.uid) < (tmp.num or getContainerCap(itemEx.uid)) do
            local rItem    = tab[math.random(size)]
            local min, max = rItem.minCount, rItem.maxCount
            doTransformItem(doAddContainerItem(itemEx.uid, 1685), rItem.id, (min and max) and math.random(min, max) or -1)
        end
        doPlayerSay(cid, 'Opened:\n'.. boxName, TALKTYPE_MONSTER)
        doItemSetAttribute(itemEx.uid, 'name', 'Opened Loot Chest')
        doSendCreatureSquare(cid, tmp.color)
        doSendMagicEffect(getThingPos(itemEx.uid), tmp.effect)
    else
        return false
    end
    return true
end
 
Code:
local t = {
    ['Loot Chest [Tier 1]'] = {
    effect = CONST_ME_FIREAREA,
    color  = TEXTCOLOR_RED,
    items  = {
            {id = 2146, minCount = 1, maxCount = 30},
            {id = 2147, minCount = 5, maxCount = 45},
            {id = 2149, minCount = 1, maxCount = 50},
            {id = 2150, minCount = 10, maxCount = 30}
        }
    },
    ['Loot Chest [Tier 2]'] = {
    effect = CONST_ME_HOLYAREA,
    color  = TEXTCOLOR_YELLOW,
    items  = {
            {id = 7759, minCount = 1, maxCount = 30},
            {id = 7760, minCount = 5, maxCount = 45},
            {id = 7761, minCount = 1, maxCount = 50},
            {id = 7762, minCount = 10, maxCount = 30}
        }
    }
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local boxName = getItemName(itemEx.uid)
    local tmp     = t[boxName]
    if tmp then
        local tab  = tmp.items
        local size = #tab
        while getContainerSize(itemEx.uid) < (tmp.num or getContainerCap(itemEx.uid)) do
            local rItem    = tab[math.random(size)]
            local min, max = rItem.minCount, rItem.maxCount
            doTransformItem(doAddContainerItem(itemEx.uid, 1685), rItem.id, (min and max) and math.random(min, max) or -1)
        end
        doPlayerSay(cid, 'Opened:\n'.. boxName, TALKTYPE_MONSTER)
        doItemSetAttribute(itemEx.uid, 'name', 'Opened Loot Chest')
        doSendCreatureSquare(cid, tmp.color)
        doSendMagicEffect(getThingPos(itemEx.uid), tmp.effect)
    else
        return false
    end
    return true
end
 
Back
Top