• 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 Problem with Quest System [1.2]

Extrodus

|| Blazera.net ||
Premium User
Joined
Dec 22, 2008
Messages
2,740
Solutions
7
Reaction score
541
Location
Canada
So, here we are having yet another weird issue with a script. It works great, don't get me wrong.. if you place any item that doesn't have "count" as a value, the chests work as intended. 1 use, then will report it is empty.

But with some chests, for example with rewards: 1x small ruby and a battle hammer.
The chest will be able to be looted multiple times, its quite strange.

Error when using a chest with a count item in it:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/quests/reward.lua:onUse
data/actions/scripts/quests/reward.lua:33: attempt to call method 'setActionId'
(a nil value)
stack traceback:
  [C]: in function 'setActionId'
  data/actions/scripts/quests/reward.lua:33: in function <data/actions/scr
ipts/quests/reward.lua:1>

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)

    if not chest then
        return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local reward = nil
    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)

    for i = start, chest:getSize() do
        reward = chest:getItem(i)
        if not reward then
            break
        end

        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(uniqueid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                player:addItem(reward:getId(), reward:getCount(), false, reward:getCharges()):setActionId(reward:getActionId())
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')


            player:setStorageValue(uniqueid, -2)
        end
    end

    return true
end

Personally, I think it has something to do with this line, but Im not quite sure at the moment:
player:addItem(reward:getId(), reward:getCount(), false, reward:getCharges()):setActionId(reward:getActionId())

Any ideas are greatly appreciated!
 
Last edited:
The error seems to be that your asking for the actionid of an item that does not already have 1 set
Code:
reward:getActionId()

The issue regarding looting multiple times is because addItem will create the item for the player regardless if setActionId(reward:getActionId()) works or not.

Think about the execution of the chained function like this, player calls addItem in which its arguments are called next and pass to addItem which returns the required item(s) then the setActionId is called and then the item get actionid is called and passed to setActionId which is then applied to the item.

It can be confusing to explain at least for me :p
But although I see your trying to created an extremely dynamic script you don't quite understand its execution & it needs to be broken up a bit to execute properly or conditions need to be set.

You shouldn't try to squeeze everything into 1 function especially when writing dynamic code because it makes the code very trouble some to debug.
 
Back
Top