• 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 [0.4] onContainer Action loot buyer

elnelson

Lunaria World Dev
Joined
Jun 20, 2009
Messages
580
Solutions
2
Reaction score
58
Location
México
Good morning, otlanders. im trying to create a script for Action that buy loots when you deposit (or place the container) on certain SQM, then calculate the cost and pay the player.

The algorythm will be something like this.

1-. Players deposit the loot in the box.
2-. This box detects all items (1 mace outside bag) and containers inside (in this case there are 2 bags filled with 15 maces, 30 gp ea)
3-. There is a total of 16 maces inside the box
4-. Player says 'sell loot' or 'sell'
5-. Script calculate 16x35gp = 560 gp and give it to the player and removes the 16 maces from the box
6-. Non-sellable items are left there and message say: i dont want X item!

As title says im using TFS 0.4 rev 3884 and i was looking in the forums and found this: Auto Seller (https://otland.net/threads/auto-seller.111183/)
the struggle is that is not looking for items inside bags, only one by one and i need to sell all loot inside the containers.

EDIT: CHANGED THE REV TO 3777 AND WORKED BUT IM GETTING THIS ERROR WHEN SELLING ITEMS INSIDE CONTAINER. IT ALSO SPAMS THE ERROR POR EACH ITEM AND IT FREEZES

This is the script:
Lua:
local items = { -- you need to set up all items here where :
                    [2466] = {cost = 100},
                    [2446] = {cost = 3000},
                    [2470] = {cost = 4000},
                    }


local basin = {x=999,y=1001,z=7,stackpos = 1} -- basin pos



-- // SCRIPT START \\ --

function getContentDescription(uid, li) -- credits to Cyko for main form of this function
    local ret, i, containers,removes, left = {}, 0, {}, {}, {}

    while i <= getContainerSize(uid) do
        local v = getContainerItem(uid, i)
        local k = v.uid
        local k2 = v.itemid

        local check = items[k2]
        if check then
            table.insert(ret, k2)
            table.insert(removes,k)
        end
        if isContainer(k) then
                table.insert(containers, k)
        end
        i = i + 1
    end
    for i = 1, #containers do
        local bah = getContentDescription(containers[i], li)
        for i = 1,#bah do
            if li == 1 then
                table.insert(removes,bah[i])
            elseif li == 2 then  
                table.insert(ret,bah[i])
            end
        end
    end
    return li== 1 and removes or ret
end

function getKey(t)
    local s = {}
    for k,v in pairs(t) do
        table.insert(s,k)
    end
    return s
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local exist = getThingFromPos(basin).uid
local itm = getThingFromPos(basin).itemid

    if isContainer(exist) then
        local t = getContentDescription(exist,2)
        local t2 = getContentDescription(exist,1)
        if #t > 0 then
            local f = {}
            for i = 1,#t do
                if not isInArray(getKey(f),t[i]) then
                    f[t[i]] = 1
                else
                    f[t[i]] = f[t[i]] + 1
                end
            end
            local str = "Sold items : "
            local money = 0
            for k,v in pairs(f) do
                str = str.."\n".."•••• "..v.."x "..getItemNameById(k).." : ".. ( tonumber(items[k].cost) * tonumber(v) ) .. " gold coins."
                money = money + tonumber(items[k].cost) * tonumber(v)

            end
            for i = 1,#t2 do
                doRemoveItem(t2[i])
            end
            doPlayerSendTextMessage(cid,27,str)
            doPlayerAddMoney(cid,money)
            if #t > 1 then
                doPlayerSendTextMessage(cid,19," Total money : ".. money.." gold coins.")
            end
        else
            doPlayerSendTextMessage(cid,18,"Warning : Sold nothing --> either bag is empty or items included arn't sellable here.")
            doSendMagicEffect(fromPosition,2)

        end
    else
        local merge = items[itm]
        if not merge then
            doPlayerSendTextMessage(cid,18,"Warning : This item isn't sellable here.")
            doSendMagicEffect(fromPosition,2)

        else
            doRemoveItem(exist)
            doPlayerSendTextMessage(cid,27,"Sold item : \n •••• 1x "..getItemNameById(itm).." : "..merge.cost.."." )
            doPlayerAddMoney(cid,merge.cost)
        end

    end
    return doTransformItem(item.uid,item.uid == 1945 and 1946 or 1945)
end



Code:
[7/4/2020 12:59:12] [Error - Action Interface]
[7/4/2020 12:59:12] data/actions/scripts/lootbox_novalis.lua:onUse
[7/4/2020 12:59:12] Description:
[7/4/2020 12:59:12] (luaGetThing) Thing not found

any solution?


I guess this function can be used to detect items?
I hope you guys could help me its an interesting feature for RPGs.




Have a nice day, stay home :D
 
Last edited:
Back
Top