• 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 NPC - TRADING AFTER QUEST

Diemorko

New Member
Joined
Jan 31, 2011
Messages
64
Reaction score
0
Code:
local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)
if(getPlayerStorageValue(cid, 10102) == -1) then
shopModule:addBuyableItem({'spear'}, 2389, 8, 1, 'spear')
elseif(getPlayerStorageValue(cid, 10102) == 1) then
shopModule:addBuyableItem({'royal spear'}, 7378, 25, 1, 'royal spear')
end
Why everyone see on 'trade' spear and royal spears.. all whos have done quest and not done ;p
Any idea? USING TFS 1.0
 
look at grizzly adams, he have trade with storages

Code:
if isInArray({"offer", "trade"}, msg:lower()) then
if getPlayerRank(cid) >= 2 then
        if getPlayerRank(cid) == 2 or getPlayerRank(cid) == 3 then
            tradeRank = grizzlyAdamsConfig.ranks.huntsMan_rank
        elseif getPlayerRank(cid) == 4 then
            tradeRank = grizzlyAdamsConfig.ranks.bigGameHunter_rank
            setNewLineTable(grizzlyAdamsConfig.ranks.huntsMan_rank, grizzlyAdamsConfig.ranks.bigGameHunter_rank)
        elseif getPlayerRank(cid) == 5 or getPlayerRank(cid) == 6 then
            tradeRank = grizzlyAdamsConfig.ranks.trophyHunter_rank
            setNewLineTable(grizzlyAdamsConfig.ranks.huntsMan_rank, grizzlyAdamsConfig.ranks.bigGameHunter_rank)
            setNewLineTable(grizzlyAdamsConfig.ranks.huntsMan_rank, grizzlyAdamsConfig.ranks.trophyHunter_rank)
        end
        local items = setNewTradeTable(tradeRank)
            local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
            if ignoreCap == false and (getPlayerFreeCap(cid) < getItemWeight(items[item].itemId, amount) or inBackpacks and getPlayerFreeCap(cid) < (getItemWeightById(items[item].itemId, amount) + getItemWeightById(1988, 1))) then
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You don\'t have enough cap.')
            end
            if items[item].buyPrice then
                doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
                for i = 1, amount do
                    if inBackpacks then
                        local backpack = doCreateItemEx(1988, 1)
                        doAddContainerItem(backpack, items[item].itemId, amount)
                    else
                        doPlayerAddItem(cid, items[item].itemId, amount, true)
                    end
                end
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You bought '..amount..'x '..items[item].realName..' for '..items[item].buyPrice * amount..' gold coins.')
            end
            return true
            end

            local function onSell(cid, item, subType, amount, ignoreCap, inBackpacks)
            if items[item].sellPrice then
                doPlayerAddMoney(cid, items[item].sellPrice * amount)
                doPlayerRemoveItem(cid, items[item].itemId, amount)
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You sold '..amount..'x '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.')
            end
            return true
            end
        openShopWindow(cid, tradeRank, onBuy, onSell)
        return selfSay('It\'s my offer.', cid)
    else
        return selfSay('You don\'t have any rank.', cid)
        end
end


The ranks:

Code:
local grizzlyAdamsConfig = {
                            ranks = {
                                    huntsMan_rank = {
                                                    {id=11208, buy=0, sell=50, name='antlers'},
                                                    {id=10549, buy=0, sell=100, name='bloody pincers'},
                                                    {id=11183, buy=0, sell=35, name='crab pincers'},
                                                    {id=10573, buy=0, sell=55, name='cyclops toe'},
                                                    {id=10564, buy=0, sell=30, name='frosty ear of a troll'},
                                                    {id=11193, buy=0, sell=600, name='hydra head'},
                                                    {id=11366, buy=0, sell=80, name='lancer beetle shell'},
                                                    {id=10578, buy=0, sell=420, name='mutated bat ear'},
                                                    {id=11222, buy=0, sell=400, name='sabretooth'},
                                                    {id=11367, buy=0, sell=20, name='sandcrawler shell'},
                                                    {id=10547, buy=0, sell=280, name='scarab pincers'},
                                                    {id=11365, buy=0, sell=60, name='terramite legs'},
                                                    {id=11363, buy=0, sell=170, name='terramite shell'},
                                                    {id=11184, buy=0, sell=30000, name='terrorbird beak'},
                                                    {id=5907, buy=34000, sell=0, name='slingshot'},
                                                                                                        {id=7398, buy=0, sell=500, name='cyclops trophy'},
                                                    {id=11315, buy=0, sell=15000, name='draken trophy'},
                                                    {id=11330, buy=0, sell=8000, name='lizard trophy'},
                                                    {id=7401, buy=0, sell=500, name='minotaur trophy'}
                                                    },

                                    bigGameHunter_rank = {
                                                    {id=7397, buy=0, sell=3000, name='deer trophy'},
                                                    {id=7400, buy=0, sell=3000, name='lion trophy'},
                                                    {id=7394, buy=0, sell=3000, name='wolf trophy'}
                                                    },

                                    trophyHunter_rank = {
                                                    {id=7393, buy=0, sell=40000, name='demon trophy'},
                                                    {id=7396, buy=0, sell=20000, name='behemoth trophy'},
                                                    {id=7399, buy=0, sell=10000, name='dragon lord trophy'},

                                                    {id=10518, buy=1000, sell=0, name='demon backpack'},
                                                    },
                                    }
                            }

Maybe tomorrow i try fix a simple storage trade for you
 
Well, i did the part you needed, now I don't know if your npc has other scripts in it, then I'll post a full npc that i've edited now

In npc.xml, you can edit parameters if you want, but removes the shop from there.

In npc.lua:

EDIT: I fixed the bug when buying with backpack also :D

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

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

local config = {
ranks = {
rank1 = {{id=2389, buy=9, sell=0, name='spear'}, ----- EDIT HERE THE ITEMS IF YOU WANT WITHOUT STORAGE
         {id=2389, buy=9, sell=0, name='spear'},
         {id=2389, buy=9, sell=0, name='spear'},
         {id=2389, buy=9, sell=0, name='spear'},},
       
rank2 = {{id=7378, buy=15, sell=0, name='royal spear'}, ----- EDIT HERE THE ITEMS IF YOU WANT WITH STORAGE
         {id=7378, buy=15, sell=0, name='royal spear'},
         {id=7378, buy=15, sell=0, name='royal spear'},
         {id=7378, buy=15, sell=0, name='royal spear'},},
}
}

local Topic = {}
function greetCallback(cid)
    Topic[cid] = 0
    return true
end

local function setNewTradeTable(table)
local items = {}
for _, v in ipairs(table) do
    items[v.id] = {itemId = v.id, buyPrice = v.buy, sellPrice = v.sell, subType = 0, realName = v.name}
end
return items
end

local function setNewLineTable(oldTable, newTable)
for k, v in pairs(oldTable) do
    table.insert(newTable, k, v)
end
return true
end

function creatureSayCallback(cid, type, msg)
        if(not npcHandler:isFocused(cid)) then
    return false
end

local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid



if isInArray({"trade"}, msg:lower()) then
    if getPlayerStorageValue(cid, 10102) == 1 then
        tradeConfig = config.ranks.rank2
        else
        tradeConfig = config.ranks.rank1
    end
                local items = setNewTradeTable(tradeConfig)
            local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
            if ignoreCap == false and (getPlayerFreeCap(cid) < getItemWeight(items[item].itemId, amount) or inBackpacks and getPlayerFreeCap(cid) < (getItemWeight(items[item].itemId, amount) + getItemWeight(1988, 1))) then
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You don\'t have enough cap.')
            end
            if items[item].buyPrice then
                for i = 1, amount do
                    if inBackpacks then
                        doPlayerRemoveMoney(cid, amount * items[item].buyPrice + 20)
                        local backpack = doPlayerAddItem(cid, 1988, 1)
                        doAddContainerItem(backpack, items[item].itemId, amount)
                        return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You bought '..amount..'x '..items[item].realName..' for '..(items[item].buyPrice * amount + 20)..' gold coins.')
                    else
                                                doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
                                                doPlayerAddItem(cid, items[item].itemId, amount, true)
                                                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You bought '..amount..'x '..items[item].realName..' for '..items[item].buyPrice * amount..' gold coins.')
                    end
                end
            end
            return true
            end

            local function onSell(cid, item, subType, amount, ignoreCap, inBackpacks)
            if items[item].sellPrice then
                doPlayerAddMoney(cid, items[item].sellPrice * amount)
                doPlayerRemoveItem(cid, items[item].itemId, amount)
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You sold '..amount..'x '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.')
            end
            return true
            end
        openShopWindow(cid, tradeConfig, onBuy, onSell)
        return selfSay('It\'s my offer.', cid)
end

return TRUE
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Boby its work perfect but u miss my script for addon :D
Code:
  local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

  if(getPlayerStorageValue(cid, 10101) == -1) then
  if(msgcontains(msg, 'help') or msgcontains(msg, 'mission')) then
  npcHandler:say("I\'m looking for {sniper gloves}, I can give you a nice addon if you bring it to me. Will you help me with this gloves?", cid)
  talkState[talkUser] = 1
  elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
  if(getPlayerStorageValue(cid, 10101) == -1)  then
  npcHandler:say("Come back when you get the legendary {sniper gloves}, I think sometime you can kill some hunters in hunter tower.", cid)
     setPlayerStorageValue(cid, 10100, 1)
  setPlayerStorageValue(cid, 10101, 1)
  else
  npcHandler:say("Then dont lose my time.", cid)
  end
  elseif(msgcontains(msg, 'no') and talkState[talkUser] == 1) then
  npcHandler:say("Come back if you got that gloves.", cid)
  end
  elseif(getPlayerStorageValue(cid, 10101) == 1 and getPlayerStorageValue(cid, 10102) == -1) then
  if(msgcontains(msg, 'help') or msgcontains(msg, 'mission') or msgcontains(msg, 'sniper gloves')) then
  npcHandler:say("Did you get a {sniper gloves} for me?", cid)
  talkState[talkUser] = 1
  elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
  if(doPlayerRemoveItem(cid, 5875, 1)) then
  npcHandler:say("Thank you, there is your reward. And from now you can {trade} with me.", cid)
  doSendMagicEffect(getPlayerPosition(cid), 13)
  setPlayerStorageValue(cid,10103,1)
  doPlayerAddOutfit(cid, 129, 2)
  doPlayerAddOutfit(cid, 137, 1)
  setPlayerStorageValue(cid, 10101, 1)
       setPlayerStorageValue(cid, 10102, 1)
       setPlayerStorageValue(cid, 10103, 1)
  setPlayerStorageValue(cid, 10100, -1)
  else
  npcHandler:say("Come back when you get {sniper gloves} for me!", cid)
  end
  elseif(msgcontains(msg, 'no') and talkState[talkUser] == 1) then
  npcHandler:say("Come back if you got that gloves.", cid)
  end
  end
  return true
end
Where put it to make it works with ur code?:p
 
Test:

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

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

local config = {
ranks = {
rank1 = {{id=2389, buy=9, sell=0, name='spear'},
        {id=2389, buy=9, sell=0, name='spear'},
        {id=2389, buy=9, sell=0, name='spear'},
        {id=2389, buy=9, sell=0, name='spear'},},
       
rank2 = {{id=7378, buy=15, sell=0, name='royal spear'},
        {id=7378, buy=15, sell=0, name='royal spear'},
        {id=7378, buy=15, sell=0, name='royal spear'},
        {id=7378, buy=15, sell=0, name='royal spear'},},
}
}

local Topic = {}
function greetCallback(cid)
    Topic[cid] = 0
    return true
end

local function setNewTradeTable(table)
local items = {}
for _, v in ipairs(table) do
    items[v.id] = {itemId = v.id, buyPrice = v.buy, sellPrice = v.sell, subType = 0, realName = v.name}
end
return items
end

local function setNewLineTable(oldTable, newTable)
for k, v in pairs(oldTable) do
    table.insert(newTable, k, v)
end
return true
end

function creatureSayCallback(cid, type, msg)
        if(not npcHandler:isFocused(cid)) then
    return false
end

local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid



if isInArray({"trade"}, msg:lower()) then
    if getPlayerStorageValue(cid, 10102) ~= 1 then
        tradeConfig = config.ranks.rank2
        else
        tradeConfig = config.ranks.rank1
    end
                local items = setNewTradeTable(tradeConfig)
            local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
            if ignoreCap == false and (getPlayerFreeCap(cid) < getItemWeight(items[item].itemId, amount) or inBackpacks and getPlayerFreeCap(cid) < (getItemWeight(items[item].itemId, amount) + getItemWeight(1988, 1))) then
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You don\'t have enough cap.')
            end
            if items[item].buyPrice then
                for i = 1, amount do
                    if inBackpacks then
                        doPlayerRemoveMoney(cid, amount * items[item].buyPrice + 20)
                        local backpack = doPlayerAddItem(cid, 1988, 1)
                        doAddContainerItem(backpack, items[item].itemId, amount)
                        return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You bought '..amount..'x '..items[item].realName..' for '..(items[item].buyPrice * amount + 20)..' gold coins.')
                    else
                                                doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
                                                doPlayerAddItem(cid, items[item].itemId, amount, true)
                                                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You bought '..amount..'x '..items[item].realName..' for '..items[item].buyPrice * amount..' gold coins.')
                    end
                end
            end
            return true
            end

            local function onSell(cid, item, subType, amount, ignoreCap, inBackpacks)
            if items[item].sellPrice then
                doPlayerAddMoney(cid, items[item].sellPrice * amount)
                doPlayerRemoveItem(cid, items[item].itemId, amount)
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You sold '..amount..'x '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.')
            end
            return true
            end
        openShopWindow(cid, tradeConfig, onBuy, onSell)
        return selfSay('It\'s my offer.', cid)
end

if(getPlayerStorageValue(cid, 10101) == -1) then
  if(msgcontains(msg, 'help') or msgcontains(msg, 'mission')) then
  npcHandler:say("I\'m looking for {sniper gloves}, I can give you a nice addon if you bring it to me. Will you help me with this gloves?", cid)
  talkState[talkUser] = 1
  elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
  if(getPlayerStorageValue(cid, 10101) == -1)  then
  npcHandler:say("Come back when you get the legendary {sniper gloves}, I think sometime you can kill some hunters in hunter tower.", cid)
    setPlayerStorageValue(cid, 10100, 1)
  setPlayerStorageValue(cid, 10101, 1)
  else
  npcHandler:say("Then dont lose my time.", cid)
  end
  elseif(msgcontains(msg, 'no') and talkState[talkUser] == 1) then
  npcHandler:say("Come back if you got that gloves.", cid)
  end
  elseif(getPlayerStorageValue(cid, 10101) == 1 and getPlayerStorageValue(cid, 10102) == -1) then
  if(msgcontains(msg, 'help') or msgcontains(msg, 'mission') or msgcontains(msg, 'sniper gloves')) then
  npcHandler:say("Did you get a {sniper gloves} for me?", cid)
  talkState[talkUser] = 1
  elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
  if(doPlayerRemoveItem(cid, 5875, 1)) then
  npcHandler:say("Thank you, there is your reward. And from now you can {trade} with me.", cid)
  doSendMagicEffect(getPlayerPosition(cid), 13)
  setPlayerStorageValue(cid,10103,1)
  doPlayerAddOutfit(cid, 129, 2)
  doPlayerAddOutfit(cid, 137, 1)
  setPlayerStorageValue(cid, 10101, 1)
      setPlayerStorageValue(cid, 10102, 1)
      setPlayerStorageValue(cid, 10103, 1)
  setPlayerStorageValue(cid, 10100, -1)
  else
  npcHandler:say("Come back when you get {sniper gloves} for me!", cid)
  end
  elseif(msgcontains(msg, 'no') and talkState[talkUser] == 1) then
  npcHandler:say("Come back if you got that gloves.", cid)
  end
  end
 
return TRUE
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

^^
 
Back
Top