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

Solved NPC wont respond on "word"

Klank

Althea ¤ A New World Developer
Joined
Feb 21, 2008
Messages
1,113
Reaction score
705
Location
Norway
Hey.

I just made this one, and got this problem:
When i say hi the npc responds with "how did you find me?! (brother)
But when i say brother he wont respond..

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 storage = 5204

function creatureSayCallback(cid, type, msg)
     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    if(not npcHandler:isFocused(cid)) then
    
         if(msg == 'hi' or msg == 'hello') then 
             if(getPlayerStorageValue(cid, storage) == -1) then
                 selfSay('How did you find me here?! ({brother})', cid)
                 talkState[talkUser] = 1
             elseif(getPlayerStorageValue(cid, storage) == 2) then
            selfSay('Hey '..getPlayerName(cid)..', Did you get what i asked for?({yes})',cid)
            talkState[talkUser] = 5
             else
                 selfSay('Hello '..getPlayerName(cid)..',Good to see you again.', cid)
             end
             npcHandler:addFocus(cid)
         else
             return false
         end
    end

    
  
  
if(msgcontains(msg,'brother') and talkState[talkUser] == 1) then
             selfSay('My brother? I havn\'t seen him in so long! So what brings you down here?({potion})', cid)
             talkState[talkUser] = 2
             elseif(msgcontains(msg, 'potion') and talkState[talkUser] == 2) then
             selfSay('Oh! Did he tell you about it? You must have helped him.. This was our secret... So tell me, do you want to grow a beard yourself? ({yes})',cid)
            talkState[talkUser] = 3
                elseif(msgcontains(msg,'yes') and talkState[talkUser] == 3) then
                selfSay('For me to make this potion i need various types of ingredients, but im missing some.. If you want me to make it you have to bring me: {5x troll hair, 1x bottle of beer, 6x shadow herbs} and {10 honeycombs}. Will you manage to do that? ({yes}/{no})',cid)
                talkState[talkUser] = 4
                elseif(msgcontains(msg,'yes') and talkState[talkUser] == 4) then
                SelfSay('Very well then, ill get ready to make the potion.',cid)
                setPlayerStorageValue(cid, storage, 2)
              
                elseif(msgcontains(msg,'no') and talkState[talkUser] == 4) then
                SelfSay('Well, no beard for you then',cid)
                talkState[talkUser] = 9
                elseif(msgcontains(msg,'yes') and talkState[talkUser] == 5) then
    if     getPlayerItemCount(cid,5902) >= 10 and getPlayerItemCount(cid,2804) >= 6 and getPlayerItemCount(cid,2015) >= 1 and                  getPlayerItemCount(cid,10606) >= 5 then               
        if doPlayerRemoveItem(cid,5902,10) and doPlayerRemoveItem(cid,2804,6) and doPlayerRemoveItem(cid,2015,1) and doPlayerRemoveItem        (cid,10606,5)then
                SelfSay('Thank you, drink this potion and your beard will grow out faster then you\'ve ever seen!',cid)
                doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have obtained Beggar beard addon.")
                doSendMagicEffect(getPlayerPosition(cid), CONST_ME_FIREWORK_RED)
                 doPlayerAddOutfit(cid, 153, 1)
                 doPlayerAddOutfit(cid, 157, 1)
                else
                SelfSay('You don\'t have the required ingredients with you', cid)
                
end  
    end  
        end
      
          
              

         talkState[talkUser] = 0
     if(msgcontains(msg, 'no') and isInArray({1,5}, talkState[talkUser])) then
         selfSay('Well, then i canv\'t help you...', cid)
         talkState[talkUser] = 0
     elseif(msgcontains(msg, 'bye')) then
         selfSay('Good bye.', cid)
         npcHandler:releaseFocus(cid)
        end
     return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
I'm going with the obvious.. maybe the storage value 5204 is not -1?

Use this and check in your console for the value of the storage after saying "hi" to the NPC.
The best thing to resolve issues, is to debug it instead of just trying to guess.

Code:
if(msg == 'hi' or msg == 'hello') then
            print(getPlayerStorageValue(cid, storage))
            if(getPlayerStorageValue(cid, storage) == -1) then
                selfSay('How did you find me here?! ({brother})', cid)
                talkState[talkUser] = 1
            elseif(getPlayerStorageValue(cid, storage) == 2) then
            selfSay('Hey '..getPlayerName(cid)..', Did you get what i asked for?({yes})',cid)
            talkState[talkUser] = 5


see ya!
 
I'm going with the obvious.. maybe the storage value 5204 is not -1?

Use this and check in your console for the value of the storage after saying "hi" to the NPC.
The best thing to resolve issues, is to debug it instead of just trying to guess.

Code:
if(msg == 'hi' or msg == 'hello') then
            print(getPlayerStorageValue(cid, storage))
            if(getPlayerStorageValue(cid, storage) == -1) then
                selfSay('How did you find me here?! ({brother})', cid)
                talkState[talkUser] = 1
            elseif(getPlayerStorageValue(cid, storage) == 2) then
            selfSay('Hey '..getPlayerName(cid)..', Did you get what i asked for?({yes})',cid)
            talkState[talkUser] = 5

see ya!
I thought local storage was used for seperate npc's from eachother..?
Ive used the storage 5203 on the same way with no errors..
Anyway i will try it in a few hours when im home:)
Thanks for replying:)
 
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 storage = 5204

function creatureSayCallback(cid, type, msg)

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
   
     if(not npcHandler:isFocused(cid)) then
         if(msg == 'hi' or msg == 'hello') then
             if(getPlayerStorageValue(cid, storage) == -1) then
                 selfSay('How did you find me here?! ({brother})', cid)
                 talkState[talkUser] = 1
             elseif(getPlayerStorageValue(cid, storage) == 2) then
                 selfSay('Hey '..getPlayerName(cid)..', Did you get what I asked for?({yes})',cid)
                 talkState[talkUser] = 5
             else
                 selfSay('Hello '..getPlayerName(cid)..', Good to see you again.', cid)
             end
             npcHandler:addFocus(cid)
         else
             return false
         end
     end

     if(msgcontains(msg,'brother') and talkState[talkUser] == 1) then
         selfSay('My brother? I haven\'t seen him in so long! So what brings you down here?({potion})', cid)
         talkState[talkUser] = 2
     elseif(msgcontains(msg, 'potion') and talkState[talkUser] == 2) then
         selfSay('Oh! Did he tell you about it? You must have helped him.. This was our secret... So tell me, do you want to grow a beard yourself? ({yes})',cid)
         talkState[talkUser] = 3
     elseif(msgcontains(msg,'yes')) then
         if(talkState[talkUser] == 3) then
             selfSay('For me to make this potion I need various types of ingredients, but im missing some.. If you want me to make it you have to bring me: {5x troll hair, 1x bottle of beer, 6x shadow herbs} and {10 honeycombs}. Will you manage to do that? ({yes}/{no})',cid)
             talkState[talkUser] = 4
         elseif(talkState[talkUser] == 4) then
             selfSay('Very well then, I\'ll get ready to make the potion.',cid)
             setPlayerStorageValue(cid, storage, 2)       
         elseif(talkState[talkUser] == 5) then 
             if doPlayerRemoveItem(cid,5902,10) and doPlayerRemoveItem(cid,2804,6) and doPlayerRemoveItem(cid,2015,1) and doPlayerRemoveItem(cid,10606,5) then
                 selfSay('Thank you, drink this potion and your beard will grow out faster then you\'ve ever seen!',cid)
                 doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have obtained Beggar beard addon.")
                 doSendMagicEffect(getPlayerPosition(cid), CONST_ME_FIREWORK_RED)
                 doPlayerAddOutfit(cid, 153, 1)
                 doPlayerAddOutfit(cid, 157, 1)
                 setPlayerStorageValue(cid, storage, 3) 
             else
                 selfSay('You don\'t have the required ingredients with you', cid)
             end
             talkState[talkUser] = 0
         end
     elseif(msgcontains(msg, 'no')) then
         if(isInArray({3,5}, talkState[talkUser])) then
             selfSay('Well, then I can\'t help you...', cid)
         elseif(talkState[talkUser] == 4) then
             selfSay('Well, no beard for you then',cid)
         end
         talkState[talkUser] = 0
     elseif(msgcontains(msg, 'bye')) then
         selfSay('Good bye.', cid)
         npcHandler:releaseFocus(cid)
     end
     return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

The reason why it didn't respond is because it was setting the talkstate to 0 for the whole script.
 
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 storage = 5204

function creatureSayCallback(cid, type, msg)

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
  
     if(not npcHandler:isFocused(cid)) then
         if(msg == 'hi' or msg == 'hello') then
             if(getPlayerStorageValue(cid, storage) == -1) then
                 selfSay('How did you find me here?! ({brother})', cid)
                 talkState[talkUser] = 1
             elseif(getPlayerStorageValue(cid, storage) == 2) then
                 selfSay('Hey '..getPlayerName(cid)..', Did you get what I asked for?({yes})',cid)
                 talkState[talkUser] = 5
             else
                 selfSay('Hello '..getPlayerName(cid)..', Good to see you again.', cid)
             end
             npcHandler:addFocus(cid)
         else
             return false
         end
     end

     if(msgcontains(msg,'brother') and talkState[talkUser] == 1) then
         selfSay('My brother? I haven\'t seen him in so long! So what brings you down here?({potion})', cid)
         talkState[talkUser] = 2
     elseif(msgcontains(msg, 'potion') and talkState[talkUser] == 2) then
         selfSay('Oh! Did he tell you about it? You must have helped him.. This was our secret... So tell me, do you want to grow a beard yourself? ({yes})',cid)
         talkState[talkUser] = 3
     elseif(msgcontains(msg,'yes')) then
         if(talkState[talkUser] == 3) then
             selfSay('For me to make this potion I need various types of ingredients, but im missing some.. If you want me to make it you have to bring me: {5x troll hair, 1x bottle of beer, 6x shadow herbs} and {10 honeycombs}. Will you manage to do that? ({yes}/{no})',cid)
             talkState[talkUser] = 4
         elseif(talkState[talkUser] == 4) then
             selfSay('Very well then, I\'ll get ready to make the potion.',cid)
             setPlayerStorageValue(cid, storage, 2)      
         elseif(talkState[talkUser] == 5) then
             if doPlayerRemoveItem(cid,5902,10) and doPlayerRemoveItem(cid,2804,6) and doPlayerRemoveItem(cid,2015,1) and doPlayerRemoveItem(cid,10606,5) then
                 selfSay('Thank you, drink this potion and your beard will grow out faster then you\'ve ever seen!',cid)
                 doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have obtained Beggar beard addon.")
                 doSendMagicEffect(getPlayerPosition(cid), CONST_ME_FIREWORK_RED)
                 doPlayerAddOutfit(cid, 153, 1)
                 doPlayerAddOutfit(cid, 157, 1)
                 setPlayerStorageValue(cid, storage, 3)
             else
                 selfSay('You don\'t have the required ingredients with you', cid)
             end
             talkState[talkUser] = 0
         end
     elseif(msgcontains(msg, 'no')) then
         if(isInArray({3,5}, talkState[talkUser])) then
             selfSay('Well, then I can\'t help you...', cid)
         elseif(talkState[talkUser] == 4) then
             selfSay('Well, no beard for you then',cid)
         end
         talkState[talkUser] = 0
     elseif(msgcontains(msg, 'bye')) then
         selfSay('Good bye.', cid)
         npcHandler:releaseFocus(cid)
     end
     return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

The reason why it didn't respond is because it was setting the talkstate to 0 for the whole script.

Thanks..
Was the ends on wrong places? :P
 
Yes, elseif should also be on the same line as the if and the end.
And if you want different responses on the same word with different talkstate, you can just make if, elseif statements with only the talkstate under it.
 
Back
Top