• 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 [TFS 1.X] Quest Container Reward with Multiple Items

Extrodus

|| Blazera.net ||
Premium User
Joined
Dec 22, 2008
Messages
2,737
Solutions
7
Reaction score
541
Location
Canada
So I'm creating this simple quest script but I have run into a slight problem. I am trying to add items to be inside the bag when they use the chest but it only adds the first item in the list "8849".

(Current script gives reward regardless of storage, so feel free to test) -- this is done on purpose since I was trying to figure out why it isnt adding the way I want it to.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  if item:getActionId(2600) and player:getStorageValue(2600) == 2 then
     player:sendTextMessage(MESSAGE_INFO_DESCR, "You have already chosen your reward.")
     elseif player:getStorageValue(2600) <= 2 then
  player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a green bag containing: a modified cross bow, a royal spear, a dragon shield and 1 crystal coin!")
       player:addItem(1991, 1):addItem(8849, 1):addItem(7378, 1):addItem(2516, 1):addItem(2160, 1)
       player:setStorageValue(2600, 1)
     else
       player:sendTextMessage(MESSAGE_INFO_DESCR, "Something went wrong.")
   end
  return true
end

The error I get says theres a nil value, but I dont see it.

[Error]
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/aragon/quests/reward room/act i/paladin.lua:onUse
...ions/scripts/aragon/quests/reward room/act i/paladin.lua:6: attempt to call m
ethod 'addItem' (a nil value)
stack traceback:
  [C]: in function 'addItem'
  ...ions/scripts/aragon/quests/reward room/act i/paladin.lua:6: in functi
on <...ions/scripts/aragon/quests/reward room/act i/paladin.lua:1>

If someone with more than half a brain can help us lesser beings out, that would be wonderful!
 
Last edited:
So I'm creating this simple quest script but I have run into a slight problem. I am trying to add items to be inside the bag when they use the chest but it only adds the first item in the list "8849".

(Current script gives reward regardless of storage, so feel free to test) -- this is done on purpose since I was trying to figure out why it isnt adding the way I want it to.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  if item:getActionId(2600) and player:getStorageValue(2600) == 2 then
     player:sendTextMessage(MESSAGE_INFO_DESCR, "You have already chosen your reward.")
     elseif player:getStorageValue(2600) <= 2 then
  player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a modified cross bow, a royal spear, a dragon shield and 1 crystal coin!")
       player:addItem(1991, 1):addItem(8849, 1):addItem(7378, 1):addItem(2516, 1):addItem(2160, 1)
       player:setStorageValue(2600, 1)
     else
       player:sendTextMessage(MESSAGE_INFO_DESCR, "Something went wrong.")
   end
  return true
end

The error I get says theres a nil value, but I dont see it.

[Error]
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/aragon/quests/reward room/act i/paladin.lua:onUse
...ions/scripts/aragon/quests/reward room/act i/paladin.lua:6: attempt to call m
ethod 'addItem' (a nil value)
stack traceback:
  [C]: in function 'addItem'
  ...ions/scripts/aragon/quests/reward room/act i/paladin.lua:6: in functi
on <...ions/scripts/aragon/quests/reward room/act i/paladin.lua:1>

If someone with more than half a brain can help us lesser beings out, that would be wonderful!

You are trying to add many items to player, but once you get to the second addItem() the method is calling it from the item class now... like this

player:addItem(2160, 1):remove() <--- this is now calling method for crystal coin (item class)
so if you do this
player:addItem(2160, 1) it is calling player class to find method, but
player:addItem(2160, 1):addItem(2161, 1) will call the first items class...
try this...

player:addItem()
player:addItem()
player:addItem()
player:addItem()
player:addItem()

and it should work
 
@Call Me Taffy - I'm using 1.2, and I already use that script; but I am making a multiple choice quest where the same storage is used for all 4 chests and that script doesnt support it so I have to make this :) Also, it is vocational in a sense; one is for knight, one for paladin, one for mages (druid/sorc) and one for all vocs.

Btw this doesnt work, they are added outside the bag.
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  if item:getActionId(2600) and player:getStorageValue(2600) == 2 then
     player:sendTextMessage(MESSAGE_INFO_DESCR, "You have already chosen your reward.")
     elseif player:getStorageValue(2600) <= 2 then
  player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a green bag containing: a modified cross bow, a royal spear, a dragon shield and 1 crystal coin!")
       player:addItem(1991, 1):addItem(8849, 1)
       player:addItem(7378, 1)
       player:addItem(2516, 1)
       player:addItem(2160, 1)
       player:setStorageValue(2600, 1)
     else
       player:sendTextMessage (MESSAGE_INFO_DESCR, "Something went wrong.")
   end
  return true
end
 
Last edited:
You can do it like this with a loop and a table.
This will add all items inside your bag (1991)

Code:
local cfgItems = {{8849, 1}, {7378, 1}, {2516, 1}, {2160, 1}}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item:getActionId(2600) and player:getStorageValue(2600) == 2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have already chosen your reward.")
    elseif player:getStorageValue(2600) <= 2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a modified cross bow, a royal spear, a dragon shield and 1 crystal coin!")
         
        local bag = player:addItem(1991) -- Bag id
        for i = 1, #cfgItems do
            bag:addItem(cfgItems[i][1], cfgItems[i][2])
        end
        player:setStorageValue(2600, 1)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Something went wrong.")
       end
end
 
@HalfAway - Haven't tested it yet, but that looks exactly the way it should. Thank you so much for your help, I will definitely keep this in mind for future use!
BTW: Remember that you'll be able to use that chest unlimited times because it checks for storagevalue is equal to 2 and you set it to 1 after you get the items. You also got another check that checks for less or equal to 2.
 
Last edited:
@HalfAway - Yeah the less or equal to is supposed to be set to 0 to check if the player hasnt done the quest yet, then the if 2 is supposed to be 1, was set that way just so we can test. Its all set back to normal, and indeed it works! Thank you very much for the help and the learning experience!
 
need similar help. Trying to have a single .lua file with all the simple right click quests in the World so its easy to add new quests with a single line
tfs 1.2 forgotten server

Lua:
local rewardConfig = {
    [1300] = {2511, 1}, {2227, 1}, {2148, 55},
    [1301] = {2511, 1}, {2227, 1}, {2148, 55},
    [1302] = {2511, 1}, {2227, 1}, {2148, 55},
    [1303] = {2511, 1}, {2227, 1}, {2148, 55}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemType = ItemType(item.uid)
    local itemWeight = itemType:getWeight()
    local playerCap = player:getFreeCapacity()

    if itemType:getId() == 0 then
        return false
    end

    local lootBag = player:addItem(1987, 1)
    local config = rewardConfig[item.actiondid]
    if player:getStorageValue(item.actiondid) == -1 then
        for i = 1, #config do
            lootBag:addItem(config[i][1], config[i][2])
        end
        player:setStorageValue(item.actiondid, 1)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end
return true
end

I get this error: "attempt to get length of local 'config' (a nil value)"
 
need similar help. Trying to have a single .lua file with all the simple right click quests in the World so its easy to add new quests with a single line
tfs 1.2 forgotten server

Lua:
local rewardConfig = {
    [1300] = {2511, 1}, {2227, 1}, {2148, 55},
    [1301] = {2511, 1}, {2227, 1}, {2148, 55},
    [1302] = {2511, 1}, {2227, 1}, {2148, 55},
    [1303] = {2511, 1}, {2227, 1}, {2148, 55}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemType = ItemType(item.uid)
    local itemWeight = itemType:getWeight()
    local playerCap = player:getFreeCapacity()

    if itemType:getId() == 0 then
        return false
    end

    local lootBag = player:addItem(1987, 1)
    local config = rewardConfig[item.actiondid]
    if player:getStorageValue(item.actiondid) == -1 then
        for i = 1, #config do
            lootBag:addItem(config[i][1], config[i][2])
        end
        player:setStorageValue(item.actiondid, 1)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end
return true
end

I get this error: "attempt to get length of local 'config' (a nil value)"
Lua:
local rewardConfig = {
    [1300] = {{2511, 1}, {2227, 1}, {2148, 55}},
    [1301] = {{2511, 1}, {2227, 1}, {2148, 55}},
    [1302] = {{2511, 1}, {2227, 1}, {2148, 55}},
    [1303] = {{2511, 1}, {2227, 1}, {2148, 55}}
}
 
Sorry, I tried that already after I posted.. same error

edit:

my brother solved it:

solution

Lua:
local rewardConfig = {
    [1300] = {{2511, 1}, {2227, 1}, {2148, 55}}
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemType = ItemType(item.uid)
    local itemWeight = itemType:getWeight()
    local playerCap = player:getFreeCapacity()

    if itemType:getId() == 0 then
        return false
    end

    local config = {}
    config = rewardConfig[item:getActionId()]

    if player:getStorageValue(item:getActionId()) == -1 then
        local lootBag = player:addItem(1987, 1)
        for index, value in pairs(config) do
            lootBag:addItem(config[index][1], config[index][2])
        end
        player:setStorageValue(item:getActionId(), 1)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end
    return true
end
 
Last edited:
Back
Top