• 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.2 Exception "use only on items in the backpack"

Svira

Active Member
Joined
Jan 27, 2008
Messages
263
Solutions
11
Reaction score
35
Hello,
I have the following script which is responsible for selling items after use:

Lua:
local items = {
   [XXXx] = 21500, -- [ItemID] = PriceOfItem
   }


function onUse(player, item, fromPosition, itemEx, target, toPosition, isHotkey)
    local tmpItem = items[itemEx.itemid]
    if not tmpItem then
        player:sendCancelMessage("You can't sell this item.")
        return true
    end
 
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
    player:removeItem(itemEx.itemid, 1)
    player:addMoney(tmpItem)
    player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    return true
end

the problem is that if we sell an item in a monster's body, we will receive money and the item will remain in a corpse.

QUESTION:
What should the exception look like that allows you to sell items only from the player's backpack?
 
Solution
E
something like this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if tmpItem then
        if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
        
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
        target:remove(1)
        player:addMoney(tmpItem)
        player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    else
        player:sendCancelMessage("You can't sell this item.")
    end
    return true
end
Lua:
        if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/other/magicalorb.lua:onUse
data/actions/scripts/other/magicalorb.lua:155: attempt to call method 'getTopParent' (a nil value)
stack traceback:
        [C]: in function 'getTopParent'
        data/actions/scripts/other/magicalorb.lua:155: in function <data/actions/scripts/other/magicalorb.lua:153>

Lua:
function onUse(player, item, fromPosition, itemEx, target, toPosition, isHotkey)
    local tmpItem = items[itemEx.itemid]
    if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
    if not tmpItem then
        player:sendCancelMessage("You can't sell this item.")
        return true
    end
    
 
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
    player:removeItem(itemEx.itemid, 1)
    player:addMoney(tmpItem)
    player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    return true
end
 
something like this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if tmpItem then
        if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
        
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
        target:remove(1)
        player:addMoney(tmpItem)
        player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    else
        player:sendCancelMessage("You can't sell this item.")
    end
    return true
end
 
Solution
something like this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if tmpItem then
        if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
       
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
        target:remove(1)
        player:addMoney(tmpItem)
        player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    else
        player:sendCancelMessage("You can't sell this item.")
    end
    return true
end

for some reason scripts with else looks always weird for me xD
 
for some reason scripts with else looks always weird for me xD
yeah it can be done this way too :p

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if not tmpItem then
        player:sendCancelMessage("You can't sell this item.")
        return true
    end
    
    if target:getTopParent() == player then
        player:sendCancelMessage("nope")
    end
    
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
    target:remove(1)
    player:addMoney(tmpItem)
    player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    return true
end
 
yeah it can be done this way too :p

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if not tmpItem then
        player:sendCancelMessage("You can't sell this item.")
        return true
    end
   
    if target:getTopParent() == player then
        player:sendCancelMessage("nope")
    end
   
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
    target:remove(1)
    player:addMoney(tmpItem)
    player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    return true
end

better :D
 
something like this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local tmpItem = items[target.itemid]
    if tmpItem then
        if target:getTopParent() == player then
            player:sendCancelMessage("nope")
        end
       
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have sold your item!')
        target:remove(1)
        player:addMoney(tmpItem)
        player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    else
        player:sendCancelMessage("You can't sell this item.")
    end
    return true
end
thank you wonderful man :)
 
Back
Top