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

Lua Help with NPC

Joined
Jul 18, 2014
Messages
193
Solutions
2
Reaction score
15
Hi!, i was creating a new NPC with too many answers, but when i finished and tested in game, looks like if he ignore something or i don't know because at the begin starts correctly saying: "I think you are |PLAYERNAME|", but when i say "yes" he doesn't respond me. What's wrong?

Here is the 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
local player = Player(cid)
function onThink()         npcHandler:onThink()             end

function creatureSayCallback(cid, type, msg)
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    if not npcHandler:isFocused(cid) then
    if msg == "hi" then
        if getPlayerStorageValue(cid, 300987) == -1 then
            npcHandler:say("I think you are |PLAYERNAME| eh?", cid)
            talkState[talkUser] = 1
        elseif getPlayerStorageValue(cid, 300987) == 0 then
            npcHandler:say("Again you?? Get out of here!", cid)
            setPlayerStorageValue(cid, 300987, 1)
            talkState[talkUser] = 1
            player:popupFYI("Answers: A) No no wait, i'm sorry for said that")
        end
    end
    if getPlayerStorageValue(cid, 300987) == 3 and msg == "hi" and getPlayerItemCount(cid, 2798, 10) then
        npcHandler:say("Do you have the 10 {blood herbs}?", cid)
        talkState[talkUser] = 1
        player:popupFYI("Answers: A) Yes")
    elseif getPlayerStorageValue(cid, 300987) == 3 and msg == "hi" then
        npcHandler:say("What the hell are you doing here? Go for the 10 blood herbs!", cid)
        npcHandler:releaseFocus(cid)
    end
    npcHandler:addFocus(cid)
    else
        return false
    end


    if msgcontains(msg, "yes") and talkState[talkUser] == 1 then
        if getPlayerStorageValue(cid, 300987) == -1 then
            npcHandler:say("Well, you remind me a great guy...", cid)
            player:popupFYI("Answers: A) Which guy? / B) I don't care, just go to the point!")
            talkState[talkUser] = 1
        elseif msgcontains(msg, "A") and talkState[talkUser] == 1 then
            npcHandler:say("A guy called Frey Stroud, maybe you heard from him", cid)
            talkState[talkUser] = 2
            player:popupFYI("Answers: A) Yes / B) No, who was him?")
        elseif msgcontains(msg, "B") and talkState[talkUser] == 1 then
            npcHandler:say("Who fuck are you to speak me like that?? Get out of here!.", cid)
            talkState[talkUser] = 0
            setPlayerStorageValue(cid, 300987, 0)
            npcHandler:releaseFocus(cid)
        elseif msgcontains(msg, "A") and talkState[talkUser] == 2 then
            npcHandler:say("Well, i think you know all from him, right?", cid)
            talkState[talkUser] = 3
            player:popupFYI("Answers: A) No, just a little / B) In Absolute")
        elseif (msgcontains(msg, "A") or msgcontains(msg, "B") and talkState[talkUser] == 3) or (msgcontains(msg, "B") and talkState[talkUser] == 2) then
            npcHandler:say("Ok, Frey Stroud was a Skandinavian Citizen five years ago, leader of the clan Stroud...", cid)
            talkState[talkUser] = 4
            player:popupFYI("Answers: A) Clan Stroud?")
        elseif msgcontains(msg, "A") and talkState[talkUser] == 4 then
            npcHandler:say("Yes, conformed by him, his wife: Anya Stroud and his three brothers: Woody Stroud, Audrey Stroud and Matty Stroud. They was a powerful clan, the best of Skandinavia.", cid)
            talkState[talkUser] = 5
            player:popupFYI("Answers: A) Anya? / B) The best of Skandinavia?")
        elseif msgcontains(msg, "A") and talkState[talkUser] == 5 then
            npcHandler:say("All that i know about Anya is that she was the most powerful girl in Skandinavia, nobody can beat her...", cid)
            talkState[talkUser] = 6
            player:popupFYI("Answers: A) Really? / B) Some special weapon?")
        elseif (msgcontains(msg, "A") and talkState[talkUser] == 6) or (msgcontains(msg, "B") and talkState[talkUser] == 5) then
            npcHandler:say("Yes! You don't have idea about the power!", cid)
            player:popupFYI("Answers: B) Some special weapon?")
        elseif msgcontains(msg, "B") and talkState[talkUser] == 6 then
            npcHandler:say("I think Frey Stroud and Anya Stroud had some special weapons... *cof* *cof*... Sorry, i can't keep talking, *cof* take this and run while you can...", cid)
            talkState[talkUser] = 0
            setPlayerStorageValue(cid, 300987, 1)
            doPlayerAddItem(cid, 1949, 1)
            doSetItemText(1949, "??????????????????????????")
            npcHandler:releaseFocus(cid)
        end
    end

    if msgcontains(msg, "A") and talkState[talkUser] == 1 then
        if getPlayerStorageValue(cid, 300987) == 1  then
            npcHandler:say("You sorry?? That's not sufficient...", cid)
            talkState[talkUser] = 2
            player:popupFYI("Answers: A) Money / B) What do you want?")
        elseif msgcontains(msg, "A") and talkState[talkUser] == 2 then
            npcHandler:say("Money? Hahaha, you see me like a poor person or what?...", cid)
            player:popupFYI("Answers: B) What do you want?")
        elseif msgcontains(msg, "B") and talkState[talkUser] == 2 then
            npcHandler:say("Well, i can have everything, but i don't have time. So, if you want my information, bring me 10 blood herbs for my daughter...", cid)
            talkState[talkUser] = 3
            player:popupFYI("Answers: A) Where can i find it? / B) Daughter?")
        elseif msgcontains(msg, "A") and talkState[talkUser] == 3 then
            npcHandler:say("I don't know, just go for them!", cid)
            setPlayerStorageValue(cid, 300987, 3)
            talkState[talkUser] = 0
            npcHandler:releaseFocus(cid)
        elseif msgcontains(msg, "B") and talkState[talkUser] == 3 then
            npcHandler:say("Yes, my daughter. You can find those that i said or not?")
            player:popupFYI("Answers: A) Where can i find it?")
        end
    end

    if msgcontains(msg, "A") and talkState[talkUser] == 1 then
        if getPlayerStorageValue(cid, 300987) == 3 and getPlayerItemCount(cid, 2798, 10) then
            npcHandler:say("Alright, come back when you're ready. I'll do like if i'd never saw you before. See ya.", cid)
            doPlayerRemoveItem(cid, 2798, 10)
            setPlayerStorageValue(cid, 300987, -1)
            talkState[talkUser] = 0
            npcHandler:releaseFocus(cid)
        end
    end
        return true
    end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

Please help me!.
Thanks :)
 
I just cleaned it up a bit to make it easier to read and moved the yes segment block of if
Just got home from work and thought I could go through your code but im tired :p
But maybe my little contribution although not complete may help you find the proper execution :)

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
local player = Player(cid)
function onThink()         npcHandler:onThink()             end

-- this will allow you to assign the storage values a bit easier by setting them
-- here and using storage's index to reference them
local storage = {
    300987
}

function checkMsg(msg, state, vmsg, vstate)
    return msgcontains(msg, vmsg) and state == vstate
end



function creatureSayCallback(cid, type, msg)

local function npcSayPop(msg, state, pop)
    npcHandler:say(msg, cid)
    if state ~= nil then
        talkState[talkUser] = state
    end
    if pop ~= nil then
        player:popupFYI(pop)
    end
end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    if not npcHandler:isFocused(cid) then
    if msg == "hi" then
        if getPlayerStorageValue(cid, storage[1]) == -1 then
            npcSayPop("I think you are |PLAYERNAME| eh?", 1, nil)
         
            if checkMsg(msg, talkState[talkUser], "yes", 1) then
                if getPlayerStorageValue(cid, storage[1]) == -1 then
                    npcSayPop("Well, you remind me a great guy...", 1, "Answers: A) Which guy? / B) I don't care, just go to the point!")
                end
             
                if checkMsg(msg, talkState[talkUser], "A", 1) then
                    npcSayPop("A guy called Frey Stroud, maybe you heard from him", 2, "Answers: A) Yes / B) No, who was him?")
                 
                elseif checkMsg(msg, talkState[talkUser], "B", 1) then
                    npcSayPop("Who fuck are you to speak me like that?? Get out of here!.", 0, nil)
                    setPlayerStorageValue(cid, storage[1], 0)
                    npcHandler:releaseFocus(cid)
                 
                elseif checkMsg(msg, talkState[talkUser], "A", 2) then
                    npcSayPop("Well, i think you know all from him, right?", 3, "Answers: A) No, just a little / B) In Absolute")
                 
                elseif (msgcontains(msg, "A") or msgcontains(msg, "B") and talkState[talkUser] == 3) or checkMsg(msg, talkState[talkUser], "B", 2) then
                    npcSayPop("Ok, Frey Stroud was a Skandinavian Citizen five years ago, leader of the clan Stroud...", 4, "Answers: A) Clan Stroud?")
                 
                elseif checkMsg(msg, talkState[talkUser], "A", 4) then
                    npcSayPop("Yes, conformed by him, his wife: Anya Stroud and his three brothers: Woody Stroud, Audrey Stroud and Matty Stroud. They was a powerful clan, the best of Skandinavia.", 5, "Answers: A) Anya? / B) The best of Skandinavia?")

                elseif checkMsg(msg, talkState[talkUser], "A", 5) then
                    npcSayPop("All that i know about Anya is that she was the most powerful girl in Skandinavia, nobody can beat her...", 6, "Answers: A) Really? / B) Some special weapon?")

                elseif checkMsg(msg, talkState[talkUser], "A", 6) or checkMsg(msg, talkState[talkUser], "B", 5) then
                    npcSayPop("Yes! You don't have idea about the power!", nil, "Answers: B) Some special weapon?")
                 
                elseif checkMsg(msg, talkState[talkUser], "B", 6) then
                    npcSayPop("I think Frey Stroud and Anya Stroud had some special weapons... *cof* *cof*... Sorry, i can't keep talking, *cof* take this and run while you can...", 0, nil)
                    setPlayerStorageValue(cid, storage[1], 1)
                    doPlayerAddItem(cid, 1949, 1)
                    doSetItemText(1949, "??????????????????????????")
                    npcHandler:releaseFocus(cid)
                end
            end
        elseif getPlayerStorageValue(cid, storage[1]) == 0 then
            npcSayPop("Again you?? Get out of here!", 1, "Answers: A) No no wait, i'm sorry for said that")
            setPlayerStorageValue(cid, storage[1], 1)
        end
    end
    if getPlayerStorageValue(cid, storage[1]) == 3 and msg == "hi" and getPlayerItemCount(cid, 2798, 10) then
        npcSayPop("Do you have the 10 {blood herbs}?", 1, "Answers: A) Yes")
     
    elseif getPlayerStorageValue(cid, storage[1]) == 3 and msg == "hi" then
        npcSayPop("What the hell are you doing here? Go for the 10 blood herbs!", nil, nil)
        npcHandler:releaseFocus(cid)
    end
    npcHandler:addFocus(cid)
    else
        return false
    end

    if checkMsg(msg, talkState[talkUser], "A", 1) then
        if getPlayerStorageValue(cid, storage[1]) == 1  then
            npcSayPop("You sorry?? That's not sufficient...", 2, "Answers: A) Money / B) What do you want?")

        elseif checkMsg(msg, talkState[talkUser], "A", 2) then
            npcSayPop("Money? Hahaha, you see me like a poor person or what?...", nil, "Answers: B) What do you want?")

        elseif checkMsg(msg, talkState[talkUser], "B", 2) then
            npcSayPop("Well, i can have everything, but i don't have time. So, if you want my information, bring me 10 blood herbs for my daughter...", 3, "Answers: A) Where can i find it? / B) Daughter?")

        elseif checkMsg(msg, talkState[talkUser], "A", 3) then
            npcSayPop("I don't know, just go for them!", 0, nil)
            setPlayerStorageValue(cid, storage[1], 3)
            npcHandler:releaseFocus(cid)
         
        elseif checkMsg(msg, talkState[talkUser], "B", 3) then
            npcSayPop("Yes, my daughter. You can find those that i said or not?", nil, "Answers: A) Where can i find it?")

        end
    end

    if checkMsg(msg, talkState[talkUser], "A", 1) then
        if getPlayerStorageValue(cid, storage[1]) == 3 and getPlayerItemCount(cid, 2798, 10) then
            npcSayPop("Alright, come back when you're ready. I'll do like if i'd never saw you before. See ya.", 0, nil)
            doPlayerRemoveItem(cid, 2798, 10)
            setPlayerStorageValue(cid, storage[1], -1)
            npcHandler:releaseFocus(cid)
        end
    end
        return true
    end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
I just cleaned it up a bit to make it easier to read and moved the yes segment block of if
Just got home from work and thought I could go through your code but im tired :p
But maybe my little contribution although not complete may help you find the proper execution :)

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
local player = Player(cid)
function onThink()         npcHandler:onThink()             end

-- this will allow you to assign the storage values a bit easier by setting them
-- here and using storage's index to reference them
local storage = {
    300987
}

function checkMsg(msg, state, vmsg, vstate)
    return msgcontains(msg, vmsg) and state == vstate
end



function creatureSayCallback(cid, type, msg)

local function npcSayPop(msg, state, pop)
    npcHandler:say(msg, cid)
    if state ~= nil then
        talkState[talkUser] = state
    end
    if pop ~= nil then
        player:popupFYI(pop)
    end
end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
    if not npcHandler:isFocused(cid) then
    if msg == "hi" then
        if getPlayerStorageValue(cid, storage[1]) == -1 then
            npcSayPop("I think you are |PLAYERNAME| eh?", 1, nil)
        
            if checkMsg(msg, talkState[talkUser], "yes", 1) then
                if getPlayerStorageValue(cid, storage[1]) == -1 then
                    npcSayPop("Well, you remind me a great guy...", 1, "Answers: A) Which guy? / B) I don't care, just go to the point!")
                end
            
                if checkMsg(msg, talkState[talkUser], "A", 1) then
                    npcSayPop("A guy called Frey Stroud, maybe you heard from him", 2, "Answers: A) Yes / B) No, who was him?")
                
                elseif checkMsg(msg, talkState[talkUser], "B", 1) then
                    npcSayPop("Who fuck are you to speak me like that?? Get out of here!.", 0, nil)
                    setPlayerStorageValue(cid, storage[1], 0)
                    npcHandler:releaseFocus(cid)
                
                elseif checkMsg(msg, talkState[talkUser], "A", 2) then
                    npcSayPop("Well, i think you know all from him, right?", 3, "Answers: A) No, just a little / B) In Absolute")
                
                elseif (msgcontains(msg, "A") or msgcontains(msg, "B") and talkState[talkUser] == 3) or checkMsg(msg, talkState[talkUser], "B", 2) then
                    npcSayPop("Ok, Frey Stroud was a Skandinavian Citizen five years ago, leader of the clan Stroud...", 4, "Answers: A) Clan Stroud?")
                
                elseif checkMsg(msg, talkState[talkUser], "A", 4) then
                    npcSayPop("Yes, conformed by him, his wife: Anya Stroud and his three brothers: Woody Stroud, Audrey Stroud and Matty Stroud. They was a powerful clan, the best of Skandinavia.", 5, "Answers: A) Anya? / B) The best of Skandinavia?")

                elseif checkMsg(msg, talkState[talkUser], "A", 5) then
                    npcSayPop("All that i know about Anya is that she was the most powerful girl in Skandinavia, nobody can beat her...", 6, "Answers: A) Really? / B) Some special weapon?")

                elseif checkMsg(msg, talkState[talkUser], "A", 6) or checkMsg(msg, talkState[talkUser], "B", 5) then
                    npcSayPop("Yes! You don't have idea about the power!", nil, "Answers: B) Some special weapon?")
                
                elseif checkMsg(msg, talkState[talkUser], "B", 6) then
                    npcSayPop("I think Frey Stroud and Anya Stroud had some special weapons... *cof* *cof*... Sorry, i can't keep talking, *cof* take this and run while you can...", 0, nil)
                    setPlayerStorageValue(cid, storage[1], 1)
                    doPlayerAddItem(cid, 1949, 1)
                    doSetItemText(1949, "??????????????????????????")
                    npcHandler:releaseFocus(cid)
                end
            end
        elseif getPlayerStorageValue(cid, storage[1]) == 0 then
            npcSayPop("Again you?? Get out of here!", 1, "Answers: A) No no wait, i'm sorry for said that")
            setPlayerStorageValue(cid, storage[1], 1)
        end
    end
    if getPlayerStorageValue(cid, storage[1]) == 3 and msg == "hi" and getPlayerItemCount(cid, 2798, 10) then
        npcSayPop("Do you have the 10 {blood herbs}?", 1, "Answers: A) Yes")
    
    elseif getPlayerStorageValue(cid, storage[1]) == 3 and msg == "hi" then
        npcSayPop("What the hell are you doing here? Go for the 10 blood herbs!", nil, nil)
        npcHandler:releaseFocus(cid)
    end
    npcHandler:addFocus(cid)
    else
        return false
    end

    if checkMsg(msg, talkState[talkUser], "A", 1) then
        if getPlayerStorageValue(cid, storage[1]) == 1  then
            npcSayPop("You sorry?? That's not sufficient...", 2, "Answers: A) Money / B) What do you want?")

        elseif checkMsg(msg, talkState[talkUser], "A", 2) then
            npcSayPop("Money? Hahaha, you see me like a poor person or what?...", nil, "Answers: B) What do you want?")

        elseif checkMsg(msg, talkState[talkUser], "B", 2) then
            npcSayPop("Well, i can have everything, but i don't have time. So, if you want my information, bring me 10 blood herbs for my daughter...", 3, "Answers: A) Where can i find it? / B) Daughter?")

        elseif checkMsg(msg, talkState[talkUser], "A", 3) then
            npcSayPop("I don't know, just go for them!", 0, nil)
            setPlayerStorageValue(cid, storage[1], 3)
            npcHandler:releaseFocus(cid)
        
        elseif checkMsg(msg, talkState[talkUser], "B", 3) then
            npcSayPop("Yes, my daughter. You can find those that i said or not?", nil, "Answers: A) Where can i find it?")

        end
    end

    if checkMsg(msg, talkState[talkUser], "A", 1) then
        if getPlayerStorageValue(cid, storage[1]) == 3 and getPlayerItemCount(cid, 2798, 10) then
            npcSayPop("Alright, come back when you're ready. I'll do like if i'd never saw you before. See ya.", 0, nil)
            doPlayerRemoveItem(cid, 2798, 10)
            setPlayerStorageValue(cid, storage[1], -1)
            npcHandler:releaseFocus(cid)
        end
    end
        return true
    end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

First, thank you very much for your time and second, i don't understand the function npcSayPop and the checkmsg, can you explain me?
 
Code:
function checkMsg(msg, state, vmsg, vstate)
  return msgcontains(msg, vmsg) and state == vstate
end

checkMsg returns true or false base on the arguments sent, its just a short cut means of finding out if msg and what the player said in the message are equal and what is the present value of talkstate in comparison to the one we're looking for

Rather than writing out every time msgcontains(msg, "yes") and talkState[talkUser] == 1
We just send the function the arguments checkMsg(msg, talkState[talkUser], "yes", 1) and it returns true or false


Code:
local function npcSayPop(msg, state, pop)
  npcHandler:say(msg, cid)
  if state ~= nil then
  talkState[talkUser] = state
  end
  if pop ~= nil then
  player:popupFYI(pop)
  end
end

npcSayPop is a local function that is defined within creatureSayCallback, this should provide a shortcut means of executing what the npc says, setting the talkstate & what the popupFYI will hold, provided if state or pop are not nil

So rather than writing every single time we need too
Code:
   npcHandler:say("some msg here", cid)
   talkState[talkUser] = 1
   player:popupFYI("some popup msg")

   or

   npcHandler:say("some msg here", cid)
   talkState[talkUser] = 1

   or

   npcHandler:say("some msg here", cid)
   player:popupFYI("some popup msg")

   or

   npcHandler:say("some msg here", cid)

We call npcSayPop and just pass the values to it, now the code above becomes
Code:
   -- npcSayPop(msg, state, pop)
   npcSayPop("some msg here", 1, "some popup msg")

   or

   npcSayPop("some msg here", 1, nil)

   or

   npcSayPop("some msg here", nil, "some popup msg")

   or

   npcSayPop("some msg here", nil, nil)


If any of the last 2 arguments are set to nil then that aspect of npcSayPop will not execute when npcSayPop is called.

The reason we do not need to define talkState[talkUser], cid or player is because npcSayPop is defined locally within the scope of the function creatureSayCallback, so all those local objects (tables, variables etc...) become global to npcSayPop when its called

I will give you an example
Code:
function outterFunction()
   local text = "This was defined inside of outterFunction"

   -- define innerFunction to show how the scope of even a local variable
   -- can change
   local function innerFunction()
     -- although text was defined in outterFunction
     -- we can use it inside of this local function
     return text..", but then I added this text in innerFunction :)"
   end
   return innerFunction()
end

print(outterFunction())

This will be the output of this example
Code:
This was defined inside of outterFunction, but then I added this text in innerFunction :)

All functions we write make an attempt to cut down on re-wrting excessive code
 
I'll be completely honest I have no idea what procedure you want this script to execute so I am just going to share with you what I have thus far, this is a way it could be written.
It is only half the script, or things i changed.. maybe I am still tired :(

Instead of using all these if / else if you can use a table and cycle through the table for the keywords and the npc state
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
local player = Player(cid)
function onThink()  npcHandler:onThink()  end

-- this will allow you to assign the storage values a bit easier by setting them
-- here and using storage's index to reference them
local storage = {
  300987
}

--[[
   template for npcTable:
   {   
     msg = "", -- the msg to look out for from the player
     state = 1, -- the talkstate to check for
     npcsay = "", -- what the npc will say in response
     talkstate = 1, -- the talkstate to set the npc to, this can be set to nil if you don't want to set the talkstate  
     pop = "", -- the popup text the player will recieve set this to nil if there is no popup
     storage = 12345678, -- storage to look for, set this nil if no storage value - this can be omitted
     setStorage = 1, -- what value you want to set the storage value to - this can be omitted
     giveItem = false -- if the player should recieve an item - to this can be omitted
   }
]]

local npcTable = {
   {
     msg = "yes",
     state = 1,
     npcsay = "Well, you remind me a great guy...",
     talkstate = nil,
     pop = "Answers: A) Which guy? / B) I don't care, just go to the point!"
   },
   {
     msg = "A",
     state = 1,
     npcsay = "A guy called Frey Stroud, maybe you heard from him",
     talkstate = 2,
     pop = "Answers: A) Yes / B) No, who was him?"
   },
   {
     msg = "B",
     state = 1,
     npcsay = "Who fuck are you to speak me like that?? Get out of here!.",
     talkstate = 0,
     pop = nil,
     storage = storage[1],
     setStorage = 0
   },
   {
     msg = "A",
     state = 2,
     npcsay = "Well, i think you know all from him, right?",
     talkstate = 3,
     pop = "Answers: A) No, just a little / B) In Absolute"
   },
   {
     msg = "A",
     state = 4,
     npcsay = "Yes, conformed by him, his wife: Anya Stroud and his three brothers: Woody Stroud, Audrey Stroud and Matty Stroud. They was a powerful clan, the best of Skandinavia.",
     talkstate = 5,
     pop = "Answers: A) Anya? / B) The best of Skandinavia?"
   },
   {
     msg = "A",
     state = 5,
     npcsay = "All that i know about Anya is that she was the most powerful girl in Skandinavia, nobody can beat her...",
     talkstate = 6,
     pop = "Answers: A) Really? / B) Some special weapon?"
   }
}


-- checks to see if the msg contains certain keywords and checks the talkstate of the npc, returns true or false based on the outcome
function checkMsg(msg, state, vmsg, vstate)
  return msgcontains(msg, vmsg) and state == vstate
end

local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

function creatureSayCallback(cid, type, msg)

   if not npcHandler:isFocused(cid) then
     return false
   end
   
   local function npcSayPop(msg, state, pop)
     npcHandler:say(msg, cid)
     if state ~= nil then
       talkState[talkUser] = state
     end
     if pop ~= nil then
       player:popupFYI(pop)
     end
   end
   
  if msgcontains(msg, "hi") then
     -- check the players storage value 300987 and see if it is equal to -1
  if getPlayerStorageValue(cid, storage[1]) == -1 then -- this only needs to be check 1 time
       -- greet the player and set talkstate to 1
  npcSayPop("I think you are |PLAYERNAME| eh?", 1, nil)
       for k, _ in pairs(npcTable) do
         if checkMsg(msg, talkState[talkUser], npcTable[k]['msg'], npcTable[k]['state']) then
           npcSayPop(npcTable[k]['npcsay'], npcTable[k]['talkstate'], npcTable[k]['pop']
           if npcTable[k]['talkstate'] == 0 then
             if npcTable[k]['storage'] ~= nil then
               setPlayerStorageValue(cid, npcTable[k]['storage'], npcTable[k]['setStorage'])
             end
             if npcTable[k]['giveItem'] then
               doPlayerAddItem(cid, 1949, 1)
               doSetItemText(1949, "??????????????????????????")
             end
           npcHandler:releaseFocus(cid)
           break
           end
         end
       end
     end
   end
  return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
I'll be completely honest I have no idea what procedure you want this script to execute so I am just going to share with you what I have thus far, this is a way it could be written.
It is only half the script, or things i changed.. maybe I am still tired :(

Instead of using all these if / else if you can use a table and cycle through the table for the keywords and the npc state
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
local player = Player(cid)
function onThink()  npcHandler:onThink()  end

-- this will allow you to assign the storage values a bit easier by setting them
-- here and using storage's index to reference them
local storage = {
  300987
}

--[[
   template for npcTable:
   {  
     msg = "", -- the msg to look out for from the player
     state = 1, -- the talkstate to check for
     npcsay = "", -- what the npc will say in response
     talkstate = 1, -- the talkstate to set the npc to, this can be set to nil if you don't want to set the talkstate 
     pop = "", -- the popup text the player will recieve set this to nil if there is no popup
     storage = 12345678, -- storage to look for, set this nil if no storage value - this can be omitted
     setStorage = 1, -- what value you want to set the storage value to - this can be omitted
     giveItem = false -- if the player should recieve an item - to this can be omitted
   }
]]

local npcTable = {
   {
     msg = "yes",
     state = 1,
     npcsay = "Well, you remind me a great guy...",
     talkstate = nil,
     pop = "Answers: A) Which guy? / B) I don't care, just go to the point!"
   },
   {
     msg = "A",
     state = 1,
     npcsay = "A guy called Frey Stroud, maybe you heard from him",
     talkstate = 2,
     pop = "Answers: A) Yes / B) No, who was him?"
   },
   {
     msg = "B",
     state = 1,
     npcsay = "Who fuck are you to speak me like that?? Get out of here!.",
     talkstate = 0,
     pop = nil,
     storage = storage[1],
     setStorage = 0
   },
   {
     msg = "A",
     state = 2,
     npcsay = "Well, i think you know all from him, right?",
     talkstate = 3,
     pop = "Answers: A) No, just a little / B) In Absolute"
   },
   {
     msg = "A",
     state = 4,
     npcsay = "Yes, conformed by him, his wife: Anya Stroud and his three brothers: Woody Stroud, Audrey Stroud and Matty Stroud. They was a powerful clan, the best of Skandinavia.",
     talkstate = 5,
     pop = "Answers: A) Anya? / B) The best of Skandinavia?"
   },
   {
     msg = "A",
     state = 5,
     npcsay = "All that i know about Anya is that she was the most powerful girl in Skandinavia, nobody can beat her...",
     talkstate = 6,
     pop = "Answers: A) Really? / B) Some special weapon?"
   }
}


-- checks to see if the msg contains certain keywords and checks the talkstate of the npc, returns true or false based on the outcome
function checkMsg(msg, state, vmsg, vstate)
  return msgcontains(msg, vmsg) and state == vstate
end

local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

function creatureSayCallback(cid, type, msg)

   if not npcHandler:isFocused(cid) then
     return false
   end
  
   local function npcSayPop(msg, state, pop)
     npcHandler:say(msg, cid)
     if state ~= nil then
       talkState[talkUser] = state
     end
     if pop ~= nil then
       player:popupFYI(pop)
     end
   end
  
  if msgcontains(msg, "hi") then
     -- check the players storage value 300987 and see if it is equal to -1
  if getPlayerStorageValue(cid, storage[1]) == -1 then -- this only needs to be check 1 time
       -- greet the player and set talkstate to 1
  npcSayPop("I think you are |PLAYERNAME| eh?", 1, nil)
       for k, _ in pairs(npcTable) do
         if checkMsg(msg, talkState[talkUser], npcTable[k]['msg'], npcTable[k]['state']) then
           npcSayPop(npcTable[k]['npcsay'], npcTable[k]['talkstate'], npcTable[k]['pop']
           if npcTable[k]['talkstate'] == 0 then
             if npcTable[k]['storage'] ~= nil then
               setPlayerStorageValue(cid, npcTable[k]['storage'], npcTable[k]['setStorage'])
             end
             if npcTable[k]['giveItem'] then
               doPlayerAddItem(cid, 1949, 1)
               doSetItemText(1949, "??????????????????????????")
             end
           npcHandler:releaseFocus(cid)
           break
           end
         end
       end
     end
   end
  return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

Wow, thats a great alternative! Its easy, but i dont know for what is the function: for k_, ipairs" i never understood that. Can you explain me too? Please:)
 
Back
Top