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

RevScripts Throw item or backpack full of items to specified tile and sell it.

nefinoo

Carnage.flv
Joined
Sep 11, 2010
Messages
554
Solutions
1
Reaction score
61
Location
Lo Mochis, Sinaloa
As the title says, if you could help me with this script that works to sell items by throwing them on a tile, or if the player throws some bp full of items, he will sell all of them and receive his bp back.

Tfs 1.4
 
Solution
Could do something like this.
Lua:
local sellables = {
    --[itemid] = price,
    [2643] = 50,
}

local SOME_ACTIONID = 6543

local function sellItems(player, item)
    local itemCount, totalValue = 0, 0
    if item:isContainer() then
        for _, it in ipairs(item:getItems()) do
            local count, value = sellItems(player, it)
            itemCount = itemCount + count
            totalValue = totalValue + value
        end
        return itemCount, totalValue
    else
        local value = sellables[item:getId()]
        if value then
            local count = item:getCount()
            item:remove()
            return count, count * value
        end
        return 0, 0
    end
end

local ev = EventCallback

function...
Could do something like this.
Lua:
local sellables = {
    --[itemid] = price,
    [2643] = 50,
}

local SOME_ACTIONID = 6543

local function sellItems(player, item)
    local itemCount, totalValue = 0, 0
    if item:isContainer() then
        for _, it in ipairs(item:getItems()) do
            local count, value = sellItems(player, it)
            itemCount = itemCount + count
            totalValue = totalValue + value
        end
        return itemCount, totalValue
    else
        local value = sellables[item:getId()]
        if value then
            local count = item:getCount()
            item:remove()
            return count, count * value
        end
        return 0, 0
    end
end

local ev = EventCallback

function ev.onItemMoved(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local ground = toCylinder:isTile() and toCylinder:getGround()
    if ground and ground:getActionId() == SOME_ACTIONID then
        local count, value = sellItems(player, item)
        if item:isContainer() then
            item:moveTo(player)
        end
        player:addMoney(value)
        player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %s item(s) for %s gold.", count, value))
    end
    return true
end

ev:register()
Make sure that Player:onItemMoved() is enabled in data/events/events.xml. Put the action id on the ground where you throw the items/backpack. If you have some sort of counter or table there, make sure to put the action id on the ground under it.
 
Solution
Could do something like this.
Lua:
local sellables = {
    --[itemid] = price,
    [2643] = 50,
}

local SOME_ACTIONID = 6543

local function sellItems(player, item)
    local itemCount, totalValue = 0, 0
    if item:isContainer() then
        for _, it in ipairs(item:getItems()) do
            local count, value = sellItems(player, it)
            itemCount = itemCount + count
            totalValue = totalValue + value
        end
        return itemCount, totalValue
    else
        local value = sellables[item:getId()]
        if value then
            local count = item:getCount()
            item:remove()
            return count, count * value
        end
        return 0, 0
    end
end

local ev = EventCallback

function ev.onItemMoved(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local ground = toCylinder:isTile() and toCylinder:getGround()
    if ground and ground:getActionId() == SOME_ACTIONID then
        local count, value = sellItems(player, item)
        if item:isContainer() then
            item:moveTo(player)
        end
        player:addMoney(value)
        player:sendTextMessage(MESSAGE_INFO_DESCR, string.format("Sold %s item(s) for %s gold.", count, value))
    end
    return true
end

ev:register()
Make sure that Player:onItemMoved() is enabled in data/events/events.xml. Put the action id on the ground where you throw the items/backpack. If you have some sort of counter or table there, make sure to put the action id on the ground under it.
thanks, works perfect!
 
Last edited:
Back
Top