• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Add action id to Item in shop.

noshirtstan

New Member
Joined
Aug 2, 2020
Messages
68
Reaction score
2
Hey folks, I'm trying to make an npc that will sell an item with a specific aid. Is there a way to add this to the .lua file?

I am currently using TFS 1.3

Thanks in advance for any help!
 
Solution
@noshirtstan This is not so hard. I have commented my changes if you are interested in knowing.
Using files from:
otland/forgottenserver (https://github.com/otland/forgottenserver/tree/master/data/npc/lib)

Copy and paste into each file below:

npc.lua
dwf-npc.lua - Pastebin.com (https://pastebin.com/37Nk6spz)

modules.lua
dwf-modules.lua - Pastebin.com (https://pastebin.com/YwPi1mPJ)

Now you can add items following the example:
LUA:
shopModule:addBuyableItem({'boots of haste'}, 2195, 15, 1, 'boots of haste', 8745)

where we have: name, id, price, subtype, realname, actionid

As you can see, it works fine.
dwfaid.gif
We'd have to see the script, but you can probably do it on your own.

To set an actionid to an item, you'd just need to use this function.
LUA:
item:setActionId(actionId)
 
Here is the code that I am using, not quite sure where to put that code.
LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local TopicState = {}

-- OTServ event handling functions start
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 onPlayerEndTrade(cid)              npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)          npcHandler:onPlayerCloseChannel(cid) end
function onThink()                          npcHandler:onThink() end
-- OTServ event handling functions end

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

    -- Buy
    shopModule:addBuyableItem({"Steel Sword Recipe"}, 26384, 50)


function creatureSayCallback(cid, type, msg)

    if (msgcontains(msg, "hello") or msgcontains(msg, "hi")) and (not npcHandler:isFocused(cid)) then
        npcHandler:say("Heelo adventurer. I sell various recipes for crafting.", cid)
        npcHandler:addFocus(cid)
        TopicState[cid] = 0

    elseif(not npcHandler:isFocused(cid)) then
        return false

    elseif msgcontains(msg, "bye") or msgcontains(msg, "farewell") then
        npcHandler:say("Goodbye!", cid, TRUE)
        npcHandler:releaseFocus(cid)

    end

    return true

end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:setMessage(MESSAGE_WALKAWAY, "How rude!")
npcHandler:setMessage(MESSAGE_SENDTRADE, "What would you like to buy?")
npcHandler:setMessage(MESSAGE_ONCLOSESHOP, "Thank you for your business.")
npcHandler:setMessage(MESSAGE_IDLETIMEOUT, "Goodbye!")
 
perhaps you could add a new parameter "actionID to the addBuyableItem function here
Post automatically merged:

or even easier, edit this function with something like "if item id = XXX then item:setActionId(actionId)"
 
Last edited by a moderator:
I dont have the advanced NPC done up, i will have to add that first lol.
Post automatically merged:

Where can I find that NPC system?
 
Last edited:
Im trying to add the fucntion but I'm not 100% sure where to put it. Ive tried after the game.createitem index like so:

LUA:
-- Including the Advanced NPC System
dofile('data/npc/lib/npcsystem/npcsystem.lua')

function msgcontains(message, keyword)
    local message, keyword = message:lower(), keyword:lower()
    if message == keyword then
        return true
    end

    return message:find(keyword) and not message:find('(%w+)' .. keyword)
end

function doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
    local amount = amount or 1
    local subType = subType or 0
    local item = 0
    if ItemType(itemid):isStackable() then
        if inBackpacks then
            stuff = Game.createItem(backpack, 1)
            item = stuff:addItem(itemid, math.min(100, amount))
        else
            stuff = Game.createItem(itemid, math.min(100, amount))
        end
        return Player(cid):addItemEx(stuff, ignoreCap) ~= RETURNVALUE_NOERROR and 0 or amount, 0
            if itemId = 26384 then
                item:setActionId(50501)
            end
    end

    local a = 0
    if inBackpacks then
        local container, b = Game.createItem(backpack, 1), 1
        for i = 1, amount do
            local item = container:addItem(itemid, subType)
            if table.contains({(ItemType(backpack):getCapacity() * b), amount}, i) then
                if Player(cid):addItemEx(container, ignoreCap) ~= RETURNVALUE_NOERROR then
                    b = b - 1
                    break
                end

                a = i
                if amount > i then
                    container = Game.createItem(backpack, 1)
                    b = b + 1
                end
            end
        end
        return a, b
    end

    for i = 1, amount do -- normal method for non-stackable items
        local item = Game.createItem(itemid, subType)
        if Player(cid):addItemEx(item, ignoreCap) ~= RETURNVALUE_NOERROR then
            break
        end
        a = i
    end
    return a, 0
end

But it keeps kicking out errors on me, no matter where i put it =/
 
Im trying to add the fucntion but I'm not 100% sure where to put it. Ive tried after the game.createitem index like so:

LUA:
-- Including the Advanced NPC System
dofile('data/npc/lib/npcsystem/npcsystem.lua')

function msgcontains(message, keyword)
    local message, keyword = message:lower(), keyword:lower()
    if message == keyword then
        return true
    end

    return message:find(keyword) and not message:find('(%w+)' .. keyword)
end

function doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
    local amount = amount or 1
    local subType = subType or 0
    local item = 0
    if ItemType(itemid):isStackable() then
        if inBackpacks then
            stuff = Game.createItem(backpack, 1)
            item = stuff:addItem(itemid, math.min(100, amount))
        else
            stuff = Game.createItem(itemid, math.min(100, amount))
        end
        return Player(cid):addItemEx(stuff, ignoreCap) ~= RETURNVALUE_NOERROR and 0 or amount, 0
            if itemId = 26384 then
                item:setActionId(50501)
            end
    end

    local a = 0
    if inBackpacks then
        local container, b = Game.createItem(backpack, 1), 1
        for i = 1, amount do
            local item = container:addItem(itemid, subType)
            if table.contains({(ItemType(backpack):getCapacity() * b), amount}, i) then
                if Player(cid):addItemEx(container, ignoreCap) ~= RETURNVALUE_NOERROR then
                    b = b - 1
                    break
                end

                a = i
                if amount > i then
                    container = Game.createItem(backpack, 1)
                    b = b + 1
                end
            end
        end
        return a, b
    end

    for i = 1, amount do -- normal method for non-stackable items
        local item = Game.createItem(itemid, subType)
        if Player(cid):addItemEx(item, ignoreCap) ~= RETURNVALUE_NOERROR then
            break
        end
        a = i
    end
    return a, 0
end

But it keeps kicking out errors on me, no matter where i put it =/
Note, that editing this function will cause ALL items with itemid 2190 to have actionid 45001 attached to them if bought from an npc.

So, make sure that the item you choose can't be gotten anywhere, except for that single npc.

This is definitely a poor way to add it into the shop system, but I don't know enough about the shop system to change it better.. xD

There is 2 spots you need to change, if you want to change the itemid and actionid that I tested with.

LUA:
function doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
    local amount = amount or 1
    local subType = subType or 0
    local item = 0
    if ItemType(itemid):isStackable() then
        if inBackpacks then
            stuff = Game.createItem(backpack, 1)
            item = stuff:addItem(itemid, math.min(100, amount))
        else
            stuff = Game.createItem(itemid, math.min(100, amount))
        end
        return Player(cid):addItemEx(stuff, ignoreCap) ~= RETURNVALUE_NOERROR and 0 or amount, 0
    end

    local a = 0
    if inBackpacks then
        local container, b = Game.createItem(backpack, 1), 1
        for i = 1, amount do
            local item = container:addItem(itemid, subType)
            -- XIKINI's EDIT START
            if itemid == 2190 then
                item:setActionId(45001)
            end
            -- XIKINI's EDIT END
            if table.contains({(ItemType(backpack):getCapacity() * b), amount}, i) then
                if Player(cid):addItemEx(container, ignoreCap) ~= RETURNVALUE_NOERROR then
                    b = b - 1
                    break
                end

                a = i
                if amount > i then
                    container = Game.createItem(backpack, 1)
                    b = b + 1
                end
            end
        end
        return a, b
    end

    for i = 1, amount do -- normal method for non-stackable items
        local item = Game.createItem(itemid, subType)
        -- XIKINI's EDIT START
        if itemid == 2190 then
            item:setActionId(45001)
        end
        -- XIKINI's EDIT END
        if Player(cid):addItemEx(item, ignoreCap) ~= RETURNVALUE_NOERROR then
            break
        end
        a = i
    end
    return a, 0
end
 
the funny thing is that there is a source function doSellItem that accept actionID as parameter:


//doSellItem(cid, itemid, amount, <optional> subtype, <optional> actionid, <optional: default: 1> canDropOnMap)


but that function is not used anywhere... we really need an NPC lib rewrite...
 
@noshirtstan This is not so hard. I have commented my changes if you are interested in knowing.
Using files from:
otland/forgottenserver (https://github.com/otland/forgottenserver/tree/master/data/npc/lib)

Copy and paste into each file below:

npc.lua
dwf-npc.lua - Pastebin.com (https://pastebin.com/37Nk6spz)

modules.lua
dwf-modules.lua - Pastebin.com (https://pastebin.com/YwPi1mPJ)

Now you can add items following the example:
LUA:
shopModule:addBuyableItem({'boots of haste'}, 2195, 15, 1, 'boots of haste', 8745)

where we have: name, id, price, subtype, realname, actionid

As you can see, it works fine.
dwfaid.gif
 
Solution
Dude that script is genius! Thank you so much!

Thanks to the others who helped out as well!
Post automatically merged:

You would'nt happen to know how to add preset text to said item would ya? lol

Sorry been racking my brain for a bit on it
 
Last edited:
Back
Top