• 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 0.X [LUA] Add info lines in talk with NPC

potinho

Well-Known Member
Joined
Oct 11, 2009
Messages
680
Solutions
15
Reaction score
52
Location
Brazil
Hello everyone, everything good? I have a crafting NPC that works perfectly. I would like to add in this NPC a line of "info". Because when players say "hi" the NPC just says what he crafts, but not what each item is. I would like to add lines where, if the player says info and then the name of the item, a text will appear with the information I put. can you help me?

like:
player: hi
player: info
npc: there's a list of items i have info to craft (list)
player: upgrade staff
npc: npcgrade staff its to upgrade ur item in levels, turning into +1,+2...etc

npc.xml
XML:
<?xml version="1.0"?>
<npc name="Drake" script="data/npc/scripts/drake.lua" walkinterval="50000" floorchange="0">
<health now="100" max="100"/>
<look type="129" head="95" body="116" legs="121" feet="115" addons="3"/>
<parameters>
<parameter key="message_greet" value="Hello |PLAYERNAME|. I can {craft} some items for you, but I need specific items to do them." />
</parameters>
</npc>

npc.lua
Lua:
    function getItemsFromList(items)
    local str = ''
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            str = str .. items[i][2] .. ' ' .. getItemNameById(items[i][1])
            if i ~= table.maxn(items) then str = str .. ', ' 
            end 
        end 
    end
    return str
end
function doRemoveItemsFromList(cid,items)
    local count = 0
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            if getPlayerItemCount(cid,items[i][1]) >= items[i][2] then
            count = count + 1 end 
        end 
    end
    if count == table.maxn(items) then
        for i = 1, table.maxn(items) do doPlayerRemoveItem(cid,items[i][1],items[i][2]) end
    else 
        return false 
    end
    return true 
end    
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
function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
    local talkUser,msg = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid, msg:lower()
    local craft = {
                      ["horned helmet"] = {reward = {{2496, 1}}, items = {{2341,1},{2557,1},{2340,1}, {2339,1}}},
                      ["skull axe"] = {reward = {{5111, 1}}, items = {{2431,1},{2229,200},{2557,3}, {2160,100}}},
                      ["pharaoh mace"] = {reward = {{5129, 1}}, items = {{2421,1},{2445,3},{2557,3}, {2160,60}}},
                      ["blade of corruption"] = {reward = {{5130, 1}}, items = {{2400,1},{2351,3},{2557,3}, {2160,100}}},
                      ["temple teleport"] = {reward = {{2361, 1}}, items = {{2542,1},{2158,10},{2156,6}, {2557,6}, {2160,500}}},
                      ["demon doll"] = {reward = {{5220, 1}}, items = {{2520,1},{2495,1},{2494,1}, {2493,1}, {2557,1}, {2160,1000}}},
                      ["golden outfit medal"] = {reward = {{5217, 1}}, items = {{2471,1},{2466,1},{2470,1}, {2646,1}, {2557,3}, {2160,50000}}},
                      ["blue gem"] = {reward = {{2158, 1}}, items = {{2146,500}, {2160,2}, {2557,1}}},
                      ["sd backpack"] = {reward = {{5271, 1}}, items = {{5263,200}}},
                      ["crystal mace"] = {reward = {{2445, 1}}, items = {{2158,5}, {2436,1}, {2557,1}}},
                      ["upgrade staff"] = {reward = {{5114, 1}}, items = {{2557,1}, {2160,100}, {2445,2}, {2151,50}}},
                      ["crystal wand"] = {reward = {{2184, 1}}, items = {{3966,2}, {2160,60}, {2557,3}, {2154,10}}},
                      ["pirate backpack"] = {reward = {{5151, 1}}, items = {{3955,5}, {2160,30}, {2489,1}, {2197,10}}},
                      ["dodge trophy"] = {reward = {{5149, 1}}, items = {{5150,1}, {2160,300}, {5202,1}, {2557,3}}},
                      ["blessed water"] = {reward = {{5164, 1}}, items = {{2327,5}, {2160,500}, {2396,5}, {2557,3}}},
                      ["buff scroll"] = {reward = {{5152, 1}}, items = {{2557,1}, {2160,30}, {2472,5}, {2433,3}}}
                }
    
    if isInArray({"item","craft","itens","items", "iten"}, msg) then
        local text = ""
        for i, v in pairs(craft) do
            text = text .. " {"..i.."}, "
        end
        npcHandler:say("I can make " .. text .. " which one do you want?", cid)    
        talkState[talkUser] = 1 
    elseif talkState[talkUser] == 1 then
      ret = craft[msg] 
        if not ret then
             npcHandler:say("I am not crafting this item, perhaps I can consider your order later.", cid) return true
        end
        npcHandler:say("Oh, " .. msg .. " is a good choice. Let me see... I need " .. getItemsFromList(ret.items) .. ", do you have it?" , cid)        
         talkState[talkUser] = 2 
    elseif talkState[talkUser] == 2 and isInArray({"yes","yeah","sim","si"}, msg) then
      if not doRemoveItemsFromList(cid, ret.items) then
               talkState[talkUser] = 0 
            npcHandler:say("Sorry, but you don't have the items that I need.", cid) return true
        end
    for _, i_i in ipairs(ret.reward) do
        local item, amount = i_i[1], i_i[2]
        if isItemStackable(item) or amount == 1 then
             doPlayerAddItem(cid, item, amount)
        else
            for i = 1, amount do
                 doPlayerAddItem(cid, item, 1)
            end
        end
    end
        talkState[talkUser] = 0 
        doSendMagicEffect(getPlayerPosition(cid), math.random(28,30))
        npcHandler:say("Thank you very much! Here is your item as I promised.", cid) return true                    
    elseif msg == "no" then 
        selfSay("Ok... Bye!! Maybe on the next.", cid) 
        talkState[talkUser] = 0 
        npcHandler:releaseFocus(cid) 
    end
    return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
Lua:
function getItemsFromList(items)
    local str = ''
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            str = str .. items[i][2] .. ' ' .. getItemNameById(items[i][1])
            if i ~= table.maxn(items) then
                str = str .. ', '
            end
        end
    end
    return str
end

function doRemoveItemsFromList(cid,items)
    local count = 0
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            if getPlayerItemCount(cid,items[i][1]) >= items[i][2] then
                count = count + 1
            end
        end
    end
    if count == table.maxn(items) then
        for i = 1, table.maxn(items) do...

Adorius Black

Well-Known Member
Joined
Mar 31, 2020
Messages
183
Solutions
3
Reaction score
76
I dont give you advice to your request but I use this modal window ceafting system and I like it. There is also info.
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,810
Solutions
433
Reaction score
3,949
Lua:
function getItemsFromList(items)
    local str = ''
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            str = str .. items[i][2] .. ' ' .. getItemNameById(items[i][1])
            if i ~= table.maxn(items) then
                str = str .. ', '
            end
        end
    end
    return str
end

function doRemoveItemsFromList(cid,items)
    local count = 0
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            if getPlayerItemCount(cid,items[i][1]) >= items[i][2] then
                count = count + 1
            end
        end
    end
    if count == table.maxn(items) then
        for i = 1, table.maxn(items) do
            doPlayerRemoveItem(cid,items[i][1],items[i][2])
        end
    else
        return false
    end
    return true
end

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

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
    
    local talkUser,msg = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid, msg:lower()
    
    local craft = {
        ["horned helmet"]       = {description = "default text 1", reward = {{2496, 1}}, items = {{2341,1},{2557,1},{2340,1}, {2339,1}}},
        ["skull axe"]           = {description = "default text 2", reward = {{5111, 1}}, items = {{2431,1},{2229,200},{2557,3}, {2160,100}}},
        ["pharaoh mace"]        = {description = "default text 3", reward = {{5129, 1}}, items = {{2421,1},{2445,3},{2557,3}, {2160,60}}},
        ["blade of corruption"] = {description = "default text 4", reward = {{5130, 1}}, items = {{2400,1},{2351,3},{2557,3}, {2160,100}}},
        ["temple teleport"]     = {description = "default text 5", reward = {{2361, 1}}, items = {{2542,1},{2158,10},{2156,6}, {2557,6}, {2160,500}}},
        ["demon doll"]          = {description = "default text 6", reward = {{5220, 1}}, items = {{2520,1},{2495,1},{2494,1}, {2493,1}, {2557,1}, {2160,1000}}},
        ["golden outfit medal"] = {description = "default text 7", reward = {{5217, 1}}, items = {{2471,1},{2466,1},{2470,1}, {2646,1}, {2557,3}, {2160,50000}}},
        ["blue gem"]            = {description = "default text 8", reward = {{2158, 1}}, items = {{2146,500}, {2160,2}, {2557,1}}},
        ["sd backpack"]         = {description = "default text 9", reward = {{5271, 1}}, items = {{5263,200}}},
        ["crystal mace"]        = {description = "default text 0", reward = {{2445, 1}}, items = {{2158,5}, {2436,1}, {2557,1}}},
        ["upgrade staff"]       = {description = "npcgrade staff its to upgrade ur item in levels, turning into +1,+2...etc", reward = {{5114, 1}}, items = {{2557,1}, {2160,100}, {2445,2}, {2151,50}}},
        ["crystal wand"]        = {description = "default text a", reward = {{2184, 1}}, items = {{3966,2}, {2160,60}, {2557,3}, {2154,10}}},
        ["pirate backpack"]     = {description = "default text b", reward = {{5151, 1}}, items = {{3955,5}, {2160,30}, {2489,1}, {2197,10}}},
        ["dodge trophy"]        = {description = "default text c", reward = {{5149, 1}}, items = {{5150,1}, {2160,300}, {5202,1}, {2557,3}}},
        ["blessed water"]       = {description = "default text d", reward = {{5164, 1}}, items = {{2327,5}, {2160,500}, {2396,5}, {2557,3}}},
        ["buff scroll"]         = {description = "default text e", reward = {{5152, 1}}, items = {{2557,1}, {2160,30}, {2472,5}, {2433,3}}}
    }
    
    -- Xikini's edit start
    for v, k in pairs(craft) do
        if msg == "info " .. v then
            npcHandler:say(k.description, cid)
            return true
        end
    end
    -- Xikini's edit end
    
    if isInArray({"item","craft","itens","items", "iten"}, msg) then
        local text = ""
        for i, v in pairs(craft) do
            text = text .. " {"..i.."}, "
        end
        npcHandler:say("I can make " .. text .. " which one do you want?", cid)
        talkState[talkUser] = 1
    elseif talkState[talkUser] == 1 then
        ret = craft[msg]
        if not ret then
            npcHandler:say("I am not crafting this item, perhaps I can consider your order later.", cid) return true
        end
        npcHandler:say("Oh, " .. msg .. " is a good choice. Let me see... I need " .. getItemsFromList(ret.items) .. ", do you have it?" , cid)
        talkState[talkUser] = 2
    elseif talkState[talkUser] == 2 and isInArray({"yes","yeah","sim","si"}, msg) then
        if not doRemoveItemsFromList(cid, ret.items) then
            talkState[talkUser] = 0
            npcHandler:say("Sorry, but you don't have the items that I need.", cid) return true
        end
        for _, i_i in ipairs(ret.reward) do
            local item, amount = i_i[1], i_i[2]
            if isItemStackable(item) or amount == 1 then
                doPlayerAddItem(cid, item, amount)
            else
                for i = 1, amount do
                    doPlayerAddItem(cid, item, 1)
                end
            end
        end
        talkState[talkUser] = 0
        doSendMagicEffect(getPlayerPosition(cid), math.random(28,30))
        npcHandler:say("Thank you very much! Here is your item as I promised.", cid) return true
    elseif msg == "no" then
        selfSay("Ok... Bye!! Maybe on the next.", cid)
        talkState[talkUser] = 0
        npcHandler:releaseFocus(cid)
    end
    return true
end
            
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
OP
OP
potinho

potinho

Well-Known Member
Joined
Oct 11, 2009
Messages
680
Solutions
15
Reaction score
52
Location
Brazil
Lua:
function getItemsFromList(items)
    local str = ''
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            str = str .. items[i][2] .. ' ' .. getItemNameById(items[i][1])
            if i ~= table.maxn(items) then
                str = str .. ', '
            end
        end
    end
    return str
end

function doRemoveItemsFromList(cid,items)
    local count = 0
    if table.maxn(items) > 0 then
        for i = 1, table.maxn(items) do
            if getPlayerItemCount(cid,items[i][1]) >= items[i][2] then
                count = count + 1
            end
        end
    end
    if count == table.maxn(items) then
        for i = 1, table.maxn(items) do
            doPlayerRemoveItem(cid,items[i][1],items[i][2])
        end
    else
        return false
    end
    return true
end

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

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
   
    local talkUser,msg = NPCHANDLER_CONVbehavior == CONVERSATION_DEFAULT and 0 or cid, msg:lower()
   
    local craft = {
        ["horned helmet"]       = {description = "default text 1", reward = {{2496, 1}}, items = {{2341,1},{2557,1},{2340,1}, {2339,1}}},
        ["skull axe"]           = {description = "default text 2", reward = {{5111, 1}}, items = {{2431,1},{2229,200},{2557,3}, {2160,100}}},
        ["pharaoh mace"]        = {description = "default text 3", reward = {{5129, 1}}, items = {{2421,1},{2445,3},{2557,3}, {2160,60}}},
        ["blade of corruption"] = {description = "default text 4", reward = {{5130, 1}}, items = {{2400,1},{2351,3},{2557,3}, {2160,100}}},
        ["temple teleport"]     = {description = "default text 5", reward = {{2361, 1}}, items = {{2542,1},{2158,10},{2156,6}, {2557,6}, {2160,500}}},
        ["demon doll"]          = {description = "default text 6", reward = {{5220, 1}}, items = {{2520,1},{2495,1},{2494,1}, {2493,1}, {2557,1}, {2160,1000}}},
        ["golden outfit medal"] = {description = "default text 7", reward = {{5217, 1}}, items = {{2471,1},{2466,1},{2470,1}, {2646,1}, {2557,3}, {2160,50000}}},
        ["blue gem"]            = {description = "default text 8", reward = {{2158, 1}}, items = {{2146,500}, {2160,2}, {2557,1}}},
        ["sd backpack"]         = {description = "default text 9", reward = {{5271, 1}}, items = {{5263,200}}},
        ["crystal mace"]        = {description = "default text 0", reward = {{2445, 1}}, items = {{2158,5}, {2436,1}, {2557,1}}},
        ["upgrade staff"]       = {description = "npcgrade staff its to upgrade ur item in levels, turning into +1,+2...etc", reward = {{5114, 1}}, items = {{2557,1}, {2160,100}, {2445,2}, {2151,50}}},
        ["crystal wand"]        = {description = "default text a", reward = {{2184, 1}}, items = {{3966,2}, {2160,60}, {2557,3}, {2154,10}}},
        ["pirate backpack"]     = {description = "default text b", reward = {{5151, 1}}, items = {{3955,5}, {2160,30}, {2489,1}, {2197,10}}},
        ["dodge trophy"]        = {description = "default text c", reward = {{5149, 1}}, items = {{5150,1}, {2160,300}, {5202,1}, {2557,3}}},
        ["blessed water"]       = {description = "default text d", reward = {{5164, 1}}, items = {{2327,5}, {2160,500}, {2396,5}, {2557,3}}},
        ["buff scroll"]         = {description = "default text e", reward = {{5152, 1}}, items = {{2557,1}, {2160,30}, {2472,5}, {2433,3}}}
    }
   
    -- Xikini's edit start
    for v, k in pairs(craft) do
        if msg == "info " .. v then
            npcHandler:say(k.description, cid)
            return true
        end
    end
    -- Xikini's edit end
   
    if isInArray({"item","craft","itens","items", "iten"}, msg) then
        local text = ""
        for i, v in pairs(craft) do
            text = text .. " {"..i.."}, "
        end
        npcHandler:say("I can make " .. text .. " which one do you want?", cid)
        talkState[talkUser] = 1
    elseif talkState[talkUser] == 1 then
        ret = craft[msg]
        if not ret then
            npcHandler:say("I am not crafting this item, perhaps I can consider your order later.", cid) return true
        end
        npcHandler:say("Oh, " .. msg .. " is a good choice. Let me see... I need " .. getItemsFromList(ret.items) .. ", do you have it?" , cid)
        talkState[talkUser] = 2
    elseif talkState[talkUser] == 2 and isInArray({"yes","yeah","sim","si"}, msg) then
        if not doRemoveItemsFromList(cid, ret.items) then
            talkState[talkUser] = 0
            npcHandler:say("Sorry, but you don't have the items that I need.", cid) return true
        end
        for _, i_i in ipairs(ret.reward) do
            local item, amount = i_i[1], i_i[2]
            if isItemStackable(item) or amount == 1 then
                doPlayerAddItem(cid, item, amount)
            else
                for i = 1, amount do
                    doPlayerAddItem(cid, item, 1)
                end
            end
        end
        talkState[talkUser] = 0
        doSendMagicEffect(getPlayerPosition(cid), math.random(28,30))
        npcHandler:say("Thank you very much! Here is your item as I promised.", cid) return true
    elseif msg == "no" then
        selfSay("Ok... Bye!! Maybe on the next.", cid)
        talkState[talkUser] = 0
        npcHandler:releaseFocus(cid)
    end
    return true
end
           
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Thanks again @Xikini , works perfectly.
 
Top