• 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!

[TFS 1.5] NPC buying rare equipped item

popa45

Member
Joined
Oct 14, 2010
Messages
75
Reaction score
9
Location
Brazil
GitHub
vilelafred
I have implemented a Rarity system in my Server where players have items classified as rare, epic, and legendary. Currently, when players sell items to NPCs, the NPC prioritizes buying the equipped item in the slot. I would like to modify this behavior so that only items from the backpack are eligible for sale, and equipped items are not purchased by NPCs.

How can I make this adjustment in the code?​
 
Last edited:
Maybe this helps:
 
Maybe this helps:
its TFS 0.3 bro :/
 
So, I made a script from scratch that is quite practical and configurable. Check out the video showcasing how the item selling to the NPC works.

This NPC is functional. It doesn't sell items by slots, only by backpack. So, add the items you want to sell and complete the process there.


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

local items = {
    ["two handed sword"] = {itemid = 2377, price = 450},
    ["battle axe"] = {itemid = 2378, price = 80},
   -- Add more items as needed
}

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 creatureSayCallback(cid, type, msg)
    if (not npcHandler:isFocused(cid)) then
        return false
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    local player = Player(cid)

    if msgcontains(msg, 'sell') or msgcontains(msg, 'sells') or msgcontains(msg, 'vende') then
        local itemList = "{"
        local isFirstItem = true
        for itemName, _ in pairs(items) do
            if isFirstItem then
                itemList = itemList .. itemName
                isFirstItem = false
            else
                itemList = itemList .. ", " .. itemName
            end
        end
        itemList = itemList .. "}"
        npcHandler:say("I'm interested in the items here: " .. itemList, cid)
        talkState[talkUser] = 1
    elseif talkState[talkUser] == 1 then
        local chosenItem = string.lower(msg)
        local itemData = items[chosenItem]

        local slotItem = player:getSlotItem(SLOT_ARMOR)

        if itemData and player:removeItem(itemData.itemid, 1, -1, true) then
            player:addMoney(itemData.price)
            npcHandler:say("So will you sell me " .. chosenItem .. " for " .. itemData.price .. " gold coins.", cid)
        else
            npcHandler:say("You do not have the item in your backpack or it cannot be in the sell slots.", cid)
        end

        talkState[talkUser] = 0
    elseif msgcontains(msg, 'offer') or msgcontains(msg, 'trade') then
        local offerList = "two handed sword - 450 gold coins\nbattle - 80 gold coins"
        npcHandler:say("Here are the available offers:\n" .. offerList, cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
So, I made a script from scratch that is quite practical and configurable. Check out the video showcasing how the item selling to the NPC works.

This NPC is functional. It doesn't sell items by slots, only by backpack. So, add the items you want to sell and complete the process there.


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

local items = {
    ["two handed sword"] = {itemid = 2377, price = 450},
    ["battle axe"] = {itemid = 2378, price = 80},
   -- Add more items as needed
}

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 creatureSayCallback(cid, type, msg)
    if (not npcHandler:isFocused(cid)) then
        return false
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    local player = Player(cid)

    if msgcontains(msg, 'sell') or msgcontains(msg, 'sells') or msgcontains(msg, 'vende') then
        local itemList = "{"
        local isFirstItem = true
        for itemName, _ in pairs(items) do
            if isFirstItem then
                itemList = itemList .. itemName
                isFirstItem = false
            else
                itemList = itemList .. ", " .. itemName
            end
        end
        itemList = itemList .. "}"
        npcHandler:say("I'm interested in the items here: " .. itemList, cid)
        talkState[talkUser] = 1
    elseif talkState[talkUser] == 1 then
        local chosenItem = string.lower(msg)
        local itemData = items[chosenItem]

        local slotItem = player:getSlotItem(SLOT_ARMOR)

        if itemData and player:removeItem(itemData.itemid, 1, -1, true) then
            player:addMoney(itemData.price)
            npcHandler:say("So will you sell me " .. chosenItem .. " for " .. itemData.price .. " gold coins.", cid)
        else
            npcHandler:say("You do not have the item in your backpack or it cannot be in the sell slots.", cid)
        end

        talkState[talkUser] = 0
    elseif msgcontains(msg, 'offer') or msgcontains(msg, 'trade') then
        local offerList = "two handed sword - 450 gold coins\nbattle - 80 gold coins"
        npcHandler:say("Here are the available offers:\n" .. offerList, cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Thanks bro! You're the best.
 
Back
Top