• 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!

TFS 1.X+ 1.4.2 Quest with Multiple Items issue

steelzord

New Member
Joined
May 29, 2013
Messages
55
Reaction score
1
Hey, trying to create a quest script which gives multiple items from one chest but at the same time a character can get rewards from only one of eg. four chests.

The player gets the first item from the list and that's it.

Lua:
--[[
    
    inquistion-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 = #####
----},
    [17440] = {
        [27440] = {itemId = 2323, count = 1}, {itemId = 8870, count = 1}, {itemId = 8900, count = 1}, {itemId = 2187, count = 1},
        [27441] = {itemId = 2323, count = 1}, {itemId = 8870, count = 1}, {itemId = 8900, count = 1}, {itemId = 2183, count = 1},
        [27442] = {itemId = 2491, count = 1}, {itemId = 2515, count = 1}, {itemId = 2664, count = 1}, {itemId = 7438, count = 1},
        [27443] = {itemId = 2491, count = 1}, {itemId = 2515, count = 1}, {itemId = 2486, count = 1}, {itemId = 2438, count = 1}, {itemId = 3962, count = 1}, {itemId = 7437, count = 1},
        storage = 17440
    },
}

local yala_style_chest = Action()

function yala_style_chest.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
    
    print("Checking storage..")
    print(index.storage)
    print(player:getStorageValue(index.storage))
    if player:getStorageValue(index.storage) == 1 then
        print("Player storage == 1. Quest has been completed before.")
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "It is empty.")
        return true
    end
    print("Player storage ~= 1. Continuing..")
    
    local reward = ItemType(index.itemId)
    local rewardWeight = reward:getWeight() * index.count
    if rewardWeight > player:getFreeCapacity() then
        print("Player does not have enough free capacity. No reward or storage given.")
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found a " .. reward:getName() .. " weighing " .. rewardWeight .. " oz. It's too heavy.")
        return true
    end
    
    print("Player storage updated to 1. Reward given.")
    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

for v, k in pairs(rewardChests) do
    yala_style_chest:aid(v)
end
yala_style_chest:register()


tried to put it in one table, eg:
Code:
{itemId = 2491, count = 1, itemId = 2515, count = 1, itemId = 2486, count = 1, itemId = 2438, count = 1, itemId = 3962, count = 1, itemId = 7437, count = 1},
or
Code:
{itemId = 2491, count = 1; itemId = 2515, count = 1; itemId = 2486, count = 1; itemId = 2438, count = 1; itemId = 3962, count = 1; itemId = 7437, count = 1},

Then the player gets only the last item from the table. Any solutions?
 
Solution
Thanks mate, I have given up on this system because no matter what
Lua:
storage = xxxxx
I put, it always registers the first one and treats everything after as completed
Storage was returning nil always

Here is, I made it only with containers tho
Lua:
local rewardChests = {
    [17440] = {

        [27440] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2187, count = 1},
        },

        [27441] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2183, count = 1}
        },

        [27442] = {
            {itemid = 2491, count = 1}...
Lua:
{
{itemId = 2491, count = 1}, {itemId = 2515, count = 1}, {itemId = 2486, count = 1}, {itemId = 2438, count = 1}, {itemId = 3962, count = 1}, {itemId = 7437, count = 1}
},

I saw something like that before, no time to give a correct answer now, but try that
 
Lua:
{
{itemId = 2491, count = 1}, {itemId = 2515, count = 1}, {itemId = 2486, count = 1}, {itemId = 2438, count = 1}, {itemId = 3962, count = 1}, {itemId = 7437, count = 1}
},

I saw something like that before, no time to give a correct answer now, but try that
Thanks mate, I have given up on this system because no matter what
Lua:
storage = xxxxx
I put, it always registers the first one and treats everything after as completed
 
Thanks mate, I have given up on this system because no matter what
Lua:
storage = xxxxx
I put, it always registers the first one and treats everything after as completed
Storage was returning nil always

Here is, I made it only with containers tho
Lua:
local rewardChests = {
    [17440] = {

        [27440] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2187, count = 1},
        },

        [27441] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2183, count = 1}
        },

        [27442] = {
            {itemid = 2491, count = 1},
            {itemid = 2515, count = 1},
            {itemid = 2664, count = 1},
            {itemid = 7438, count = 1}
        },

        [27443] = {
            {itemid = 2491, count = 1},
            {itemid = 2515, count = 1},
            {itemid = 2486, count = 1},
            {itemid = 2438, count = 1},
            {itemid = 3962, count = 1},
            {itemid = 7437, count = 1}
        },

        container = 1988,
        storage = 17440
    },
}

local yala_style_chest = Action()
function yala_style_chest.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local rewardChest = rewardChests[item:getActionId()]
    local rewardItems = rewardChest[item:getUniqueId()]

    if not rewardItems then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Missing configuration, please report to a gamemaster.")
    end

    local storage = rewardChest.storage
    if player:getStorageValue(storage) == 1 then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "It is empty.")
    end

    local rewards = {}
    local virtualContainer = Game.createItem(rewardChest.container)

    for i = 1, #rewardItems do
        local reward = rewardItems[i]
        local newItem = ItemType(reward.itemid)

        if newItem:getId() ~= 0 then
            virtualContainer:addItem(newItem:getId(), reward.count)
            rewards[#rewards + 1] = (newItem:isStackable() and reward.count or newItem:getArticle()) .. " " .. newItem:getName()
        end
    end

    local containerWeight = virtualContainer:getWeight()
    if containerWeight > player:getFreeCapacity() then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have found a %s that weights %.2f oz. It's too heavy."):format(virtualContainer:getName(), containerWeight / 100)),
            virtualContainer:remove()
    end
   
    player:setStorageValue(storage, 1)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have found a %s with %s inside."):format(virtualContainer:getName(), table.concat(rewards, ", "):gsub("(.*),", "%1 and")))

    player:addItemEx(virtualContainer)
    return true
end

for v, k in pairs(rewardChests) do
    yala_style_chest:aid(v)
end
yala_style_chest:register()
 
Solution
Storage was returning nil always

Here is, I made it only with containers tho
Lua:
local rewardChests = {
    [17440] = {

        [27440] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2187, count = 1},
        },

        [27441] = {
            {itemid = 2323, count = 1},
            {itemid = 8870, count = 1},
            {itemid = 8900, count = 1},
            {itemid = 2183, count = 1}
        },

        [27442] = {
            {itemid = 2491, count = 1},
            {itemid = 2515, count = 1},
            {itemid = 2664, count = 1},
            {itemid = 7438, count = 1}
        },

        [27443] = {
            {itemid = 2491, count = 1},
            {itemid = 2515, count = 1},
            {itemid = 2486, count = 1},
            {itemid = 2438, count = 1},
            {itemid = 3962, count = 1},
            {itemid = 7437, count = 1}
        },

        container = 1988,
        storage = 17440
    },
}

local yala_style_chest = Action()
function yala_style_chest.onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local rewardChest = rewardChests[item:getActionId()]
    local rewardItems = rewardChest[item:getUniqueId()]

    if not rewardItems then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Missing configuration, please report to a gamemaster.")
    end

    local storage = rewardChest.storage
    if player:getStorageValue(storage) == 1 then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "It is empty.")
    end

    local rewards = {}
    local virtualContainer = Game.createItem(rewardChest.container)

    for i = 1, #rewardItems do
        local reward = rewardItems[i]
        local newItem = ItemType(reward.itemid)

        if newItem:getId() ~= 0 then
            virtualContainer:addItem(newItem:getId(), reward.count)
            rewards[#rewards + 1] = (newItem:isStackable() and reward.count or newItem:getArticle()) .. " " .. newItem:getName()
        end
    end

    local containerWeight = virtualContainer:getWeight()
    if containerWeight > player:getFreeCapacity() then
        return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have found a %s that weights %.2f oz. It's too heavy."):format(virtualContainer:getName(), containerWeight / 100)),
            virtualContainer:remove()
    end
  
    player:setStorageValue(storage, 1)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, ("You have found a %s with %s inside."):format(virtualContainer:getName(), table.concat(rewards, ", "):gsub("(.*),", "%1 and")))

    player:addItemEx(virtualContainer)
    return true
end

for v, k in pairs(rewardChests) do
    yala_style_chest:aid(v)
end
yala_style_chest:register()
You're an absolute champ, works like a charm. Thank you so much for that man!
 
Back
Top