• 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 Count items in chest [HELP]

darkangel1

New Member
Joined
Oct 31, 2023
Messages
53
Reaction score
4
Hello community, how to add the number of items from a chest. I used this script, but I still got 1 throwing star, not 100
Lua:
--[[

    Anni-style chests.
  
    This script goes into data/scripts as a .lua file

    1) Place 2+ chests into map editor
    2) Place Same ActionId onto all chests. (example: 45000)
    3) Place UniqueId onto all chests. (example: 45001, 45002, 45003...)
    4) Update config table here, to match the chests in map editor (ActionId, UniqueId's)
    5) Update table with itemId's, count's and an unused storage number
    6) Restart Server, and test

]]--

local rewardChests = {
----[actionId] = {
----    [uniqueId] = {itemId = ####, count = #},
----    [uniqueId] = {itemId = ####, count = #},
----    [uniqueId] = {itemId = ####, count = #},
----    [uniqueId] = {itemId = ####, count = #},
----    storage = #####
----},
    [45000] = { -- 45000 as Action id on ALL 3 chests
        [45001] = {itemId = 2190, count = 1}, --45001 as UNIQUE id on chest for item 2190
        [45002] = {itemId = 2191, count = 1}, --45002 as UNIQUE id on chest for item 2191
        [45003] = {itemId = 2399, count = 100}, --45003 as UNIQUE id on chest for item 2399
        storage = 45000
    }
}


function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local index = rewardChests[item:getActionId()][item:getUniqueId()]
    if not index then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Inform gamemaster of error with quest.")
        print("Error: UniqueId does not exist in table.")
        return true
    end
  
    if player:getStorageValue(index.storage) == 1 then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "It is empty.")
        return true
    end

    local reward = ItemType(index.itemId)
    local rewardWeight = reward:getWeight() * index.count
    if rewardWeight > player:getFreeCapacity() then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found a " .. reward:getName() .. " weighing " .. rewardWeight .. " oz. It's too heavy.")
        return true
    end
  
    player:addItem(index.itemId, index.count, true)
    player:setStorageValue(index.storage, 1)
  
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found a " .. reward:getName() .. ".")
    return true
end
 

Attachments

tested and working OK... try it :)

Just throw it in the data/scripts/actions folder... it's REVSCRIPTS, ok?
Lua:
local config = {
    [45003] = {rewardItemId = 2399, rewardItemCount = 100, storageValue = 45000},
    [45001] = {rewardItemId = 2190, rewardItemCount = 1, storageValue = 45001},
    [45002] = {rewardItemId = 2191, rewardItemCount = 1, storageValue = 45002}
}

for uniqueId, rewards in pairs(config) do
    local rewardChests = Action()

    function rewardChests.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        if item:getUniqueId() == uniqueId then
            local storageValue = rewards.storageValue

            if player:getStorageValue(storageValue) ~= 1 then
                local rewardItemId = rewards.rewardItemId
                local rewardItemCount = rewards.rewardItemCount
                local itemName = getItemNameById(rewardItemId)

                if player:addItem(rewardItemId, rewardItemCount) then
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found " .. rewardItemCount .. " units of " .. itemName .. ".")
                    player:setStorageValue(storageValue, 1)
                    return true
                else
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough inventory space to receive the item.")
                    return true
                end
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already received the reward from this chest.")
                return true
            end
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This chest has no reward set.")
            return true
        end
    end

    rewardChests:uid(uniqueId)
    rewardChests:register()
end
 
tested and working OK... try it :)

Just throw it in the data/scripts/actions folder... it's REVSCRIPTS, ok?
Lua:
local config = {
    [45003] = {rewardItemId = 2399, rewardItemCount = 100, storageValue = 45000},
    [45001] = {rewardItemId = 2190, rewardItemCount = 1, storageValue = 45001},
    [45002] = {rewardItemId = 2191, rewardItemCount = 1, storageValue = 45002}
}

for uniqueId, rewards in pairs(config) do
    local rewardChests = Action()

    function rewardChests.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        if item:getUniqueId() == uniqueId then
            local storageValue = rewards.storageValue

            if player:getStorageValue(storageValue) ~= 1 then
                local rewardItemId = rewards.rewardItemId
                local rewardItemCount = rewards.rewardItemCount
                local itemName = getItemNameById(rewardItemId)

                if player:addItem(rewardItemId, rewardItemCount) then
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found " .. rewardItemCount .. " units of " .. itemName .. ".")
                    player:setStorageValue(storageValue, 1)
                    return true
                else
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough inventory space to receive the item.")
                    return true
                end
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already received the reward from this chest.")
                return true
            end
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This chest has no reward set.")
            return true
        end
    end

    rewardChests:uid(uniqueId)
    rewardChests:register()
end
They will be able to take all items once with this script though because different storage values.
 
They will be able to take all items once with this script though because different storage values.
What I did was the script. You can add multiple missions whenever you want. It's just a roadmap for multiple missions. It's working fine for me. Storage is working, I can only check out items once. There are no errors. :)
 
What I did was the script. You can add multiple missions whenever you want. It's just a roadmap for multiple missions. It's working fine for me. Storage is working, I can only check out items once. There are no errors. :)
Sure, I'm just saying since he wanted Annihilator-type chests he probably only want the players to take a reward from one of the chests. Was mainly just a heads up for @darkangel1 if he decided to use your script.
 
I think I misunderstood, it seems to me that he only wants to do it once.

There's only one storage there and you can only use it once, so...
Lua:
local config = {
    [45003] = {rewardItemId = 2399, rewardItemCount = 100},
    [45001] = {rewardItemId = 2190, rewardItemCount = 1},
    [45002] = {rewardItemId = 2191, rewardItemCount = 1}
}

local REWARD_STORAGE = 45000

for uniqueId, rewards in pairs(config) do
    local rewardChests = Action()

    function rewardChests.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        if item:getUniqueId() == uniqueId then
            if player:getStorageValue(REWARD_STORAGE) ~= 1 then
                local rewardItemId = rewards.rewardItemId
                local rewardItemCount = rewards.rewardItemCount
                local itemName = getItemNameById(rewardItemId)
                if player:addItem(rewardItemId, rewardItemCount) then
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found " .. rewardItemCount .. " units of " .. itemName .. ".")
                    player:setStorageValue(REWARD_STORAGE, 1)
                    return true
                else
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough inventory space to receive the item.")
                    return true
                end
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already received the reward from this chest.")
                return true
            end
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This chest has no reward set.")
            return true
        end
    end

    rewardChests:uid(uniqueId)
    rewardChests:register()
end
 
Last edited:
Я написал сценарий. Вы можете добавить несколько миссий, когда захотите. Это просто дорожная карта для нескольких миссий. У меня это работает нормально. Хранилище работает, я могу проверить товары только один раз. Ошибок нет.:)
Very good. Thanks! This real good
Post automatically merged:

Конечно, я просто говорю, что, поскольку ему нужны сундуки типа «Аннигилятор», он, вероятно, хочет, чтобы игроки получали награду только из одного из сундуков. В основном это было предупреждение для @darkangel1, если он решит использовать ваш сценарий.
yeah
Post automatically merged:

С помощью этого скрипта они смогут забрать все предметы один раз, поскольку у них разные значения хранения.
Or you can put a present in the chest and have several items in it. and after using the chest you were given a present, and there are already objects inside it?
 
Last edited:
Or you can put a present in the chest and have several items in it. and after using the chest you were given a present, and there are already objects inside it?
I'm not sure what you are asking, but it's possible to edit this one or make a new script that can do that, yes.
 
I think I misunderstood, it seems to me that he only wants to do it once.

There's only one storage there and you can only use it once, so...
Lua:
local config = {
    [45003] = {rewardItemId = 2399, rewardItemCount = 100},
    [45001] = {rewardItemId = 2190, rewardItemCount = 1},
    [45002] = {rewardItemId = 2191, rewardItemCount = 1}
}

local REWARD_STORAGE = 45000

for uniqueId, rewards in pairs(config) do
    local rewardChests = Action()

    function rewardChests.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        if item:getUniqueId() == uniqueId then
            if player:getStorageValue(REWARD_STORAGE) ~= 1 then
                local rewardItemId = rewards.rewardItemId
                local rewardItemCount = rewards.rewardItemCount
                local itemName = getItemNameById(rewardItemId)
                if player:addItem(rewardItemId, rewardItemCount) then
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found " .. rewardItemCount .. " units of " .. itemName .. ".")
                    player:setStorageValue(REWARD_STORAGE, 1)
                    return true
                else
                    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You do not have enough inventory space to receive the item.")
                    return true
                end
            else
                player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have already received the reward from this chest.")
                return true
            end
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "This chest has no reward set.")
            return true
        end
    end

    rewardChests:uid(uniqueId)
    rewardChests:register()
end
There was a bug, so I made a fix and now it's working 100%, receiving the item only once.
 
Back
Top