• 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

Member
Joined
Jan 27, 2008
Messages
137
Reaction score
9
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
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
OP
OP
Svira

Svira

Member
Joined
Jan 27, 2008
Messages
137
Reaction score
9
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
 

Evil Puncker

I know nothing
TFS Developer
Joined
May 30, 2009
Messages
8,128
Solutions
219
Reaction score
4,053
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

Levi999x

AoT
Joined
Dec 14, 2017
Messages
1,884
Solutions
37
Reaction score
1,099
Location
Germany
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
 

Evil Puncker

I know nothing
TFS Developer
Joined
May 30, 2009
Messages
8,128
Solutions
219
Reaction score
4,053
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
 

Levi999x

AoT
Joined
Dec 14, 2017
Messages
1,884
Solutions
37
Reaction score
1,099
Location
Germany
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
 
OP
OP
Svira

Svira

Member
Joined
Jan 27, 2008
Messages
137
Reaction score
9
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 :)
 
Top