• 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 not waiting for reply

  • Thread starter Thread starter Xikini
  • Start date Start date
X

Xikini

Guest
The Forgotten Server, version 0.3.7_SVN (Crying Damson)

The npc doesn't stop and wait for the yes/no keywords.
If I say backpack it says this..
Code:
11:13 Xikini [17]: backpack
11:13 Addon Checker: You like my backpack do you? I can make you one for 15,000 gold coins! Would you like to do that?
11:13 Addon Checker: That was not an available keyword. Let's start from the beginning.
As always,
I would like to thank everyone in advance for any feedback, criticism, or help given!
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

function creatureSayCallback(cid, type, msg)
    if(not npcHandler:isFocused(cid)) then
        return false
    end
   
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    function greet(cid)
        talkState[cid] = 0
        return true
    end

    local player_gold     = getPlayerItemCount(cid,2148)
    local player_plat     = getPlayerItemCount(cid,2152)*100
    local player_crys     = getPlayerItemCount(cid,2160)*10000
    local player_money    = player_gold + player_plat + player_crys
   
    if msgcontains(msg, "job") then
        selfSay("My job is to test peoples grammar.", cid)
   
    elseif msgcontains(msg, "backpack") and (getPlayerStorageValue(cid,45052) < 1) then
            selfSay("You like my backpack do you? I can make you one for 15,000 gold coins! Would you like to do that?", cid)
            talkState[talkUser] = 1
        if msgcontains(msg, "yes") and talkState[talkUser] == 1 then
            if player_money >= 15000 then
                doPlayerRemoveMoney(cid,15000)
                selfSay("You carry that much money on you without a backpack? Imagine what you can carry now! Take care my good friend.", cid)
                doPlayerAddOutfit(cid, 136, 1)
                doPlayerAddOutfit(cid, 128, 1)
                setPlayerStorageValue(cid,45052,1)
                talkState[talkUser] = 0
            else
                selfSay("You simply don't have enough money my friend.", cid)
                talkState[talkUser] = 0
            end
        elseif msgcontains(msg, "no") and talkState[talkUser] == 1 then
            selfSay("Ah. That is a shame! I'll have to find some other fool to buy it then.", cid)
            talkState[talkUser] = 0
        else
            selfSay("That was not an available keyword. Let's start from the beginning.", cid)
            talkState[talkUser] = 0
        end
   
        -- QUEST COMPLETE MESSAGES --
    elseif msgcontains(msg, "backpack") and (getPlayerStorageValue(cid,45052) == 1) then   
            selfSay("You already have a backpack mate. I don't think you need another.", cid)   
   
    elseif msgcontains(msg, "reset") then
        selfSay("Reset complete.", cid)
        setPlayerStorageValue(cid,45052,-1)
    end
    return true   
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Atm you have the if statement for yes inside the elseif statement for backpack, so after someone says backpack, it already looks like if someone says yes.
Ofc this is not true because someone said backpack and not yes and also not no, so that's why it says the message after else.
Code:
if msgcontains(msg, "yes") and talkState[talkUser] == 1 then
Change if to elseif, remove the end for that if and move the else under the reset part.
 
if I were to add more quests to the npc would the 'else' have to be generic enough to respond to all phrases?
you need to separe the messages with elseif like limos said like
Code:
if msgcontains(msg, "yes") and talkState[talkUser] == 1 then do something
elseif msgcontains(msg, "no") and talkState[talkUser] == 1 then do another thing end
 
@Xikini

Why is the greet function inside of the creatureSayCallback function?

Every time you say something to the NPC the creatureSayCallback will be called.
So if you say 'backpack' it will answare 'You like my backpack do you?...' and then change the talkstate to 1.
Then the if statement will check if the message contains 'yes' as well (the same message we just found 'backpack' in).
Since the message doesn't contain 'yes' it will jump down to the next elseif.
The message doesn't contain 'no' either so it will jump to the else statement and say 'That was not an available keyword...'.

This is how you can fix it:
Code:
elseif msgcontains(msg, "backpack") and (getPlayerStorageValue(cid,45052) < 1) then
    selfSay("You like my backpack do you? I can make you one for 15,000 gold coins! Would you like to do that?", cid)
    talkState[talkUser] = 1
elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
    if player_money >= 15000 then
        doPlayerRemoveMoney(cid,15000)
        selfSay("You carry that much money on you without a backpack? Imagine what you can carry now! Take care my good friend.", cid)
        doPlayerAddOutfit(cid, 136, 1)
        doPlayerAddOutfit(cid, 128, 1)
        setPlayerStorageValue(cid,45052,1)
        talkState[talkUser] = 0
    else
        selfSay("You simply don't have enough money my friend.", cid)
        talkState[talkUser] = 0
    end
elseif msgcontains(msg, "no") and talkState[talkUser] == 1 then
    selfSay("Ah. That is a shame! I'll have to find some other fool to buy it then.", cid)
    talkState[talkUser] = 0

Edit:
I took a long break in the middle of this post and didn't see all the replies =P
 
Last edited:
Would like to thank everyone for responding!
Closing the thread. :P
(In general I think I 'know' now that instead of nesting if statements, use elseif, and if you need the same keyword as in 'yes' & 'no' elevate the talkState further. 2,3,4.
If I'm wrong, throw a brick at me :P
 
@Xikini

Did you move the greet function? =P
I have not. It was put there because players that had walked away or waited until timeout would still retain the elevated talkstate. when they say hi again they could potentially walk to another npc and get somewhere they were not meant to be at.
 
I have not. It was put there because players that had walked away or waited until timeout would still retain the elevated talkstate. when they say hi again they could potentially walk to another npc and get somewhere they were not meant to be at.

Yes, but it should be placed outside of the creatureSayCallback function together with onCreatureAppear, onCreatureDisappear, onCreatureSay and onThink.
 
Yes, but it should be placed outside of the creatureSayCallback function together with onCreatureAppear, onCreatureDisappear, onCreatureSay and onThink.
Like this?
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

    function greet(cid)
        talkState[cid] = 0
        return true
    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
 
Back
Top