Unknown Soldier
Mapping a map
- Joined
- Oct 30, 2010
- Messages
- 293
- Solutions
- 11
- Reaction score
- 664
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!
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