• 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 Sell more items after finish a quest.

nefinoo

Carnage.flv
Joined
Sep 11, 2010
Messages
429
Solutions
1
Reaction score
35
Location
Lo Mochis, Sinaloa
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

local function getTable(player)
    local itemsList = {
        {name='mana potion', id=7620, buy=50},
        {name='health potion', id=7618, buy=50},
        {name='spirit rune', id=2309, buy=50}
    }

    local tomes = {
        {
        {name='strong mana potion', id=7589, buy=80},
        {name='strong health potion', id=7588, buy=80},
        {name='strong spirit rune', id=2312, buy=80}
        }
        -- {
        -- {name="bone shoulderplate", id=11321, sell=150},
        -- {name="broken draken mail", id=12616, sell=340},
        -- {name="broken halberd", id=11335, sell=100}
        -- }
    }

    if player:getStorageValue(123123) >= 1 then
        for i = 1, #tomes[1] do
            itemsList[#itemsList] = tomes[1][i]
        end
    end

    if player:getStorageValue(123123) >= 2 then
        for i = 1, #tomes[2] do
            itemsList[#itemsList] = tomes[2][i]
        end
    end

    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 = item.subType, realName = item.name}
    end
    return items
end

local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
    local player = Player(cid)
    local items = setNewTradeTable(getTable(player))
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack or backpack:getEmptySlots(true) < 1 then
        player:sendCancelMessage(RETURNVALUE_NOTENOUGHROOM)
        return false
    end
    if not ignoreCap and player:getFreeCapacity() < ItemType(items[item].itemId):getWeight(amount) then
        return player:sendTextMessage(MESSAGE_FAILURE, 'You don\'t have enough cap.')
    end
    if not player:removeMoneyNpc(items[item].buyPrice * amount) then
        selfSay("You don't have enough money.", cid)
    else
        player:addItem(items[item].itemId, amount, true, subType)
        return player:sendTextMessage(MESSAGE_TRADE, '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)
    local player = Player(cid)
    local items = setNewTradeTable(getTable(player))
    if items[item].sellPrice and player:removeItem(items[item].itemId, amount) then
        player:addMoney(items[item].sellPrice * amount)
        return player:sendTextMessage(MESSAGE_TRADE, 'Sold '..amount..'x '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.')
    else
        selfSay("You don't have item to sell.", cid)
    end
    return true
end

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

    if msgcontains(msg, "trade") then
        local player = Player(cid)
        local items = setNewTradeTable(getTable(player))
        openShopWindow(cid, getTable(player), onBuy, onSell)
        npcHandler:say("Of course, just browse through my wares.", cid)
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, 'Hello, |PLAYERNAME| and welcome to my little forge.')
npcHandler:setMessage(MESSAGE_FAREWELL, 'Bye.')

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

I am using this script to the player get more items after finish a quest and get a new storage value, but when i get the new value and trade again with the npc this one does not show me some new items.

beforegetstorage.png
This image are before get the storage value

aftergetstorage.png
This image are after get the new storage value
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,682
Solutions
406
Reaction score
3,792
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

local function getTable(player)
    local itemsList = {
        {name='mana potion', id=7620, buy=50},
        {name='health potion', id=7618, buy=50},
        {name='spirit rune', id=2309, buy=50}
    }

    local tomes = {
        {
        {name='strong mana potion', id=7589, buy=80},
        {name='strong health potion', id=7588, buy=80},
        {name='strong spirit rune', id=2312, buy=80}
        }
        -- {
        -- {name="bone shoulderplate", id=11321, sell=150},
        -- {name="broken draken mail", id=12616, sell=340},
        -- {name="broken halberd", id=11335, sell=100}
        -- }
    }

    if player:getStorageValue(123123) >= 1 then
        for i = 1, #tomes[1] do
            itemsList[#itemsList] = tomes[1][i]
        end
    end

    if player:getStorageValue(123123) >= 2 then
        for i = 1, #tomes[2] do
            itemsList[#itemsList] = tomes[2][i]
        end
    end

    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 = item.subType, realName = item.name}
    end
    return items
end

local function onBuy(cid, item, subType, amount, ignoreCap, inBackpacks)
    local player = Player(cid)
    local items = setNewTradeTable(getTable(player))
    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack or backpack:getEmptySlots(true) < 1 then
        player:sendCancelMessage(RETURNVALUE_NOTENOUGHROOM)
        return false
    end
    if not ignoreCap and player:getFreeCapacity() < ItemType(items[item].itemId):getWeight(amount) then
        return player:sendTextMessage(MESSAGE_FAILURE, 'You don\'t have enough cap.')
    end
    if not player:removeMoneyNpc(items[item].buyPrice * amount) then
        selfSay("You don't have enough money.", cid)
    else
        player:addItem(items[item].itemId, amount, true, subType)
        return player:sendTextMessage(MESSAGE_TRADE, '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)
    local player = Player(cid)
    local items = setNewTradeTable(getTable(player))
    if items[item].sellPrice and player:removeItem(items[item].itemId, amount) then
        player:addMoney(items[item].sellPrice * amount)
        return player:sendTextMessage(MESSAGE_TRADE, 'Sold '..amount..'x '..items[item].realName..' for '..items[item].sellPrice * amount..' gold coins.')
    else
        selfSay("You don't have item to sell.", cid)
    end
    return true
end

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

    if msgcontains(msg, "trade") then
        local player = Player(cid)
        local items = setNewTradeTable(getTable(player))
        openShopWindow(cid, getTable(player), onBuy, onSell)
        npcHandler:say("Of course, just browse through my wares.", cid)
    end
    return true
end

npcHandler:setMessage(MESSAGE_GREET, 'Hello, |PLAYERNAME| and welcome to my little forge.')
npcHandler:setMessage(MESSAGE_FAREWELL, 'Bye.')

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

I am using this script to the player get more items after finish a quest and get a new storage value, but when i get the new value and trade again with the npc this one does not show me some new items.

View attachment 62378
This image are before get the storage value

View attachment 62379
This image are after get the new storage value
I mean, it looks correct.

Did you forget to un-greentext it?
 

Leesne

i r peekay
Premium User
Joined
Dec 4, 2017
Messages
342
Solutions
33
Reaction score
186
Location
UK
You are just adding the value to the end of the table. #itemList never changes as you are not inserting.

Lua:
    if player:getStorageValue(123123) >= 1 then
        for i = 1, #tomes[1] do
            table.insert(itemsList, tomes[1][i])
        end
    end
1632825313344.png
 
Top