• 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+ How to remove items from purse/store inbox?

Albert José

Rambocop Infernus
Joined
Jun 10, 2008
Messages
87
Solutions
1
Reaction score
13
Location
Portugal
GitHub
albertjose
I have created a system to give to the players a "coin" their store inbox (purse), the idea is that they can't remove the item from there, allowing them to buy some special items using a lever system, but for some reason the function player:removeItem(id, count) isn't working.

There is the code snippet from my lua code to add items at store:

Lua:
function Player:addStoreItemEx(item)
    local storeInbox = self:getStoreInbox()
    if storeInbox then
        item:moveTo(storeInbox)
        return true
    end
    return false
end
 
Do you have a code with Player:removeItem(id, count)? Because I can't see it in the code above.

Or do you just want a code with removeItem in it?
 
Do you have a code with Player:removeItem(id, count)? Because I can't see it in the code above.

Or do you just want a code with removeItem in it?

I want to remove the item inside the player's store inbox, I have tried in two ways:

Lua:
RESET_COIN = 38228
-- 1
player:removeStoreItem(RESET_COIN, 1)
-- 2
player:removeItem(RESET_COIN, 1)

There is the code from removeStoreItem:
Lua:
function Player:removeStoreItem(item, quantity)
    local storeInbox = self:getStoreInbox()
    if storeInbox then
        return storeInbox:addItemEx(item, -quantity)
    end
    return FALSE
end
 
I have resolved the problem by myself, I'm using the getItems() Lua:function to get the items inside the players's store inbox (it's container) and I remove them using the item:remove(count) function, I know that I could use a recursive function, but I'm too lazy to think about this, so if anyone have the same problem there is the code:

Lua:
function Player:getStoreInboxItemCount(itemId)
    local storeInbox = self:getStoreInbox()
   
    return storeInbox:getItemCountById(itemId)
end

function Player:removeStoreItem(itemId, quantity)
    local storeInbox = self:getStoreInbox()
    if storeInbox then
        local items = storeInbox:getItems()

        if quantity and storeInbox:getItemCountById(itemId) < quantity then
            return false
        end

        local countRemove = quantity
        for i=1, #items do
            if items[i] and items[i]:getId() == itemId then
                if items[i]:getCount() >= countRemove then
                    items[i]:remove(countRemove)
                else
                    items[i]:remove(items[i]:getCount())
                    countRemove = countRemove - items[i]:getCount()
                end
            end
        end
    end
    return true
end

Example of usage:
Lua:
RESET_COIN = 38228

local mounts = {
    [25] = {name = "Rented Horse", ID = 25, price = 5},
    [16] = {name = "Crystal Wolf", ID = 16, price = 15},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 10029 then
        local mount = mounts[25]
        if player:getStoreInboxItemCount(RESET_COIN) >= mount.price then
            if not player:hasMount(mount.ID) then
                player:addMount(mount.ID)
                player:removeStoreItem(RESET_COIN, mount.price)
                player:say(string.format("You receive the permission to ride a %s.", mount.name), TALKTYPE_MONSTER_SAY)
                player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
            else
                player:sendCancelMessage("You already have this mount.")
                player:getPosition():sendMagicEffect(CONST_ME_POFF)
            end
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You must have a %i reset coins in your backpack!", mount.price))
            return false
        end
    end
    item:transform(item.itemid == 10029)
    return true
end

Btw, for some reason the player:removeItem doesn't work with items inside the inbox.
 
Back
Top