• 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 Simple NPC Trade TFS 1.3

Unknown Soldier

Mapping a map
Joined
Oct 30, 2010
Messages
263
Solutions
11
Reaction score
621
Hello,

I'm having a problem with npc script selling equipment, wrapped furniture. It will be probably very easy for every lua scripter... It is about item:setAttribute("wrapid", itemsList[j].wrapid), and probably also the for loop where this line is (line 39).
So I want the wrapid to be corresponding with certain item, cause now it is always taking the wrapid from the last item in the table.

Thanks in advance!

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)


function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:onThink()                    end


function getTable()
     itemsList = {
        {name = "item 1", id = 26129, buy = 50, wrapid = 27088},
        {name = "item 2", id = 26129, buy = 150, wrapid = 31345},
        {name = "item 3", id = 26129, buy = 500, wrapid = 26347}
    }
    return itemsList
end

local function setNewTradeTable(table)
    local items, item = {}
    for i = 1, #table do
        item = table[i]
        items[item.id] = {itemId = item.id, buyPrice = item.buy, sellPrice = item.sell, subType = 0, realName = item.name}
    end
    return items
end

local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
    local player = Player(cid)
    local items = setNewTradeTable(getTable())
    local count = 0
    for i = 1, amount do
        local item = Game.createItem(items[item].itemId, subType)
    
        for j = 1, #itemsList do
            if item then
                item:setAttribute("wrapid", itemsList[j].wrapid)
                item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, 'It contains ' .. item:getName() .. '.')
            end
        end
        
        if player:addItemEx(item, false) ~= RETURNVALUE_NOERROR then
            npcHandler:say('First make sure you have enough space in your inventory.', cid)
            break
        end
        count = i
    end

    if count == 0 then
        return true
    end

    player:removeMoney(items[item].buyPrice * count)
    player:sendTextMessage(MESSAGE_INFO_DESCR, string.format('Bought %dx %s for %d gold.', count, items[item].realName, items[item].buyPrice * count))

    return true
end
 
Okay so yeah.. the for loop is going through and assigning the attributes 3 times over, and the last one in the list is obviously what it ends on.

A bigger problem is that because all of these items have the same itemid, when you are using the function setNewTradeTable the items are over-writing themselves.

on line 39 inside the for loop, you are grabbing the itemsList directly.. and only because you've made itemsList a global table inside of the getTable function are you not getting an error..

I'm not even sure what to do here to help, since every function is broken. xD

-- edit

ugh I hate npc buy system. breaking my brain.
The only thing I can tell you, is that you'd want to basically use an index to identify the item chosen to be bought.. then grab the information using the index, as opposed to using the itemid.

But.. I don't understand the npc shop system enough to actually know how to grab the index of the item in the shop.. 🤷‍♀️
 
Last edited:
I thought it would be easier xD Yee, the loop is totally stupid in this case... I shouldn't be playing with scripts that late xd
I guess I have two solutions then, create x number of clones of package item, which I don't really like, or stay with classic buying style, without modal window. You wouldn't even see the items in the window, only name and package, if I would decide to make the clones...

Thanks!
 
I thought it would be easier xD Yee, the loop is totally stupid in this case... I shouldn't be playing with scripts that late xd
I guess I have two solutions then, create x number of clones of package item, which I don't really like, or stay with classic buying style, without modal window. You wouldn't even see the items in the window, only name and package, if I would decide to make the clones...

Thanks!
Check if user has money before looping.
 
Back
Top