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

Quest system, no space no item TFS 1.3

Mr Noxi

Noxus Otserver
Joined
May 13, 2010
Messages
272
Solutions
3
Reaction score
94
Location
Sweden
Hellos,

So i just came across this issue in my quest system.

Say a player has no space in backpack and opens the chest, player gets the EXP added but the item get dropped on floor

Part of the code below
Lua:
        expReward = 9000000,
        item_reward = {
        [1] = {itemid = 11304, count = 1} --Mighty Ring quest Knight
        }
    },


The chest system code below

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player = Player(player)
    chest = questChests[item:getAttribute('uid')]
    if not chest then return true end
 
    --------------Check if player has already opened the chest-----
    if player:getStorageValue(item:getAttribute('uid')) == 1 then
        return player:sendCancelMessage("The chest is empty.")
    end
 
    --------------Check if player has quest status-----------------
    if chest.storage and chest.status then
        if player:getStorageValue(chest.storage) ~= chest.status then
            return player:sendCancelMessage(chest.questMsg_fail)
        end
    end
 
    ---------------Check if player has level requirement------------
    if chest.levelReq then
        if player:getLevel() < chest.levelReq then
            return player:sendCancelMessage("You must be level "..chest.levelReq.. " to open this chest.")
        end
    end
 
    --------------Check if player has key required-------------------
    if chest.keyReq then
        if player:getItemCount(chest.keyReq) < 1 then
            return player:sendCancelMessage("This chest requires a key to open.")
        end
    end
 
    ------------Give player EXP reward-------------------------------
    if chest.vocation then
    if not table.contains(chest.vocation.id, player:getVocation():getId()) then
        return player:sendCancelMessage("Your vocation cannot open this chest.")
    end
end
   
    if chest.expReward then
        player:addExperience(chest.expReward, true)
    end
 
    ------------Give player reward items---------------------
    if chest.item_reward then
        for i = 1, #chest.item_reward do
            player:addItem(chest.item_reward[i].itemid, chest.item_reward[i].count, 1)
        end
    end
 
    ------------Give player outfit reward--------------------
    if chest.outfit_reward then
        for i = 1, #chest.outfit_reward do
            if player:getSex() == 0 then
                player:addOutfit(chest.outfit_reward[i].female, chest.outfit_reward[i].addons)
            else
                player:addOutfit(chest.outfit_reward[i].male, chest.outfit_reward[i].addons)
            end
        end
    end
 
    -----------Give player addon reward-----------------------
    if chest.addon_reward then
        for i = 1, #chest.addon_reward do
            if player:getSex() == 0 then
                if chest.addon_reward.addons == 1 then
                    player:addOutfitAddon(chest.addon_reward[i].female, 1)
                else
                    player:addOutfitAddon(chest.addon_reward[i].female, 1)
                    player:addOutfitAddon(chest.addon_reward[i].female, 2)
                end
            else
                if chest.addon_reward.addons == 1 then
                    player:addOutfitAddon(chest.addon_reward[i].male, 1)
                else
                    player:addOutfitAddon(chest.addon_reward[i].male, 1)
                    player:addOutfitAddon(chest.addon_reward[i].male, 2)
                end
            end
        end
    end
 
    ----------Give player mount reward--------------------------
    if chest.mount_reward then
        for i = 1, #chest.mount_reward do
            player:addMount(chest.mount_reward[i].mountId)
        end
    end
 
    if chest.questStatus then
        player:setStorageValue(chest.questStorage, chest.questStatus + 1)
    end
 
    player:sendCancelMessage("You have recieved your reward.")
    player:setStorageValue(item:getAttribute('uid'), 1)
    return false
end


Any idea what i might need to add so the player can get a msg that he needs to clear a spot for the reward item before getting the item?


Appreciate the help!
 
Last edited:
Hello bro! i got the same problem a few weeks ago and this is one of alot solutions.

Add to your libs this function

Lua:
function getFreeSlotsFromContainer(item) -- by Musztang
    local slots = 0
    local containers = {}

    if not item then return false end
    if item:isContainer() then
        table.insert(containers, item)
    end
    while #containers > 0 do
        local firstContainer = containers[1]
        if(firstContainer) then
            for i = firstContainer:getSize() - 1, 0, -1 do
                local item = firstContainer:getItem(i)
                if item:isContainer() then
                    table.insert(containers, item)
                end
            end
        end
        slots = slots + (firstContainer:getCapacity() - firstContainer:getSize())
        table.remove(containers, 1)
    end
    return slots
end

And then, in you action script add this checks

Code:
        if not player:getSlotItem(CONST_SLOT_BACKPACK) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You need a container equiped (backpack or bag) in your character to recieve this reward.")
            fromPosition:sendMagicEffect(CONST_ME_POFF)
            return true
        end

        if(getFreeSlotsFromContainer(player:getSlotItem(CONST_SLOT_BACKPACK)) <= 0) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You dont have slots free in your backpack to claim this reward, please make some space.")
            fromPosition:sendMagicEffect(CONST_ME_POFF)
            return true
        end
 
You can try this, it will also pack items to a bag if you write more than 1. Moreover it will tell how many cap player needs.
Lua:
if chest.item_reward then
    if #chest.item_reward == 1 then
        local reward = Game.createItem(chest.item_reward[i].itemid, chest.item_reward[i].count)
        if player:addItemEx(reward) ~= RETURNVALUE_NOERROR then
            local weight = reward:getWeight()
            if player:getFreeCapacity() < weight then
                return player:sendCancelMessage(string.format('You have found %s weighing %.2f oz. You have no capacity.', result, (weight / 100)))
            else
                return player:sendCancelMessage('You have found something, but you have no room to take it.')
            end
        end
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found " .. getItemName(chest.item_reward[i].itemid) .. ".")
    else
        local bagId = 1987
        local bag = Container(doCreateItemEx(bagId))
        for i = 1, #chest.item_reward do
            bag:addItem(chest.item_reward[i].itemid, chest.item_reward[i].count)
        end

        if player:addItemEx(bag) ~= RETURNVALUE_NOERROR then
            local weight = bag:getWeight()
            if player:getFreeCapacity() < weight then
                local text = string.format('You have found a bag weighing %.2f oz. You have no capacity.', (weight / 100))
                return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, text)
            else
                return player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have found a bag, but you have no room to take it.')
            end
        end
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have found a bag with items inside.")
    end
end
 
Back
Top