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

Item desappears if moved

Lucas Durais

New Member
Joined
Jan 13, 2017
Messages
40
Reaction score
1
Hello otland!

How can I make a item desappears if the player moves it inside the backpack or if he take it out of the backpack??

My tfs version is 1.2

Thanks!!
 
Solution
data/events/scripts/player.lua

replace the function onMoveItem already there with this:
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if (item:getId() == ITEMID HERE) and (fromPosition.x == CONTAINER_POSITION) then
        item:remove()
        self:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "text here")
        return false
    end
    return true
end

data/events/events.xml

make sure this is enabled:
XML:
<event class="Player" method="onMoveItem" enabled="1" />
Sorry for the empty explanation.

I'm actually doing a quest that when the player uses a item on the floor, he gets a item inside his backpack.
But if he moves the item inside his backpack or even throws the item on the floor, the item disappears and a message appears.
 
data/events/scripts/player.lua

replace the function onMoveItem already there with this:
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if (item:getId() == ITEMID HERE) and (fromPosition.x == CONTAINER_POSITION) then
        item:remove()
        self:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "text here")
        return false
    end
    return true
end

data/events/events.xml

make sure this is enabled:
XML:
<event class="Player" method="onMoveItem" enabled="1" />
 
Solution
Works very well if the player moves the item manually. But if he moves another item inside the backpack, nothing happens.
The item that was supposed to desappears moves inside the backpack as well.
 
Yeah... I've changed my mind.. I'll leave it as it is.

Thanks very much for your help as always Xeraphus
well poop
i already made it to where it gets removed like that
well anyways if you want to use it:
Lua:
local questItem = 2160

local function deleteQuestItem(container)
    for index = container:getSize()-1, 0, -1 do
        local item = container:getItem(index)
        if ItemType(item:getId()):isContainer() then
            deleteQuestItem(item)
        elseif (item:getId() == questItem) then
            item:remove()
            return true
        end
    end
end

debug.sethook(
    function()
        local info = debug.getinfo(2)
        if info and info.name and (info.name == "addItem") then
            local _, player = debug.getlocal(2, 1)
            if player then
                local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
                if backpack and (backpack:getItemCountById(questItem) >= 1) then
                    deleteQuestItem(backpack)
                end
            end
            return true
        end
    end, "c"
)

function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if (item:getId() == questItem) and (fromPosition.x == CONTAINER_POSITION) then
        item:remove()
        self:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "text here")
        return false
    end
    if (toCylinder:getItemCountById(questItem) >= 1) then
        deleteQuestItem(toCylinder)
    end
    return true
end
 
Just for curiosity I tried and it works flawless...
You are awesome.

But I'm going to let it as it was.. I think it would become too hard for the players..

Thanks!
 
Back
Top