• 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 Problem with npc script

New sell

New Member
Joined
Mar 29, 2015
Messages
1
Reaction score
0
Hey Guys i have problem with my npc script, he works but not corecttly
if i say item1 he works but if i say item2 or item3 he give me item1 please help me
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

if(msgcontains(msg, 'item1') or msgcontains(msg, 'item11')) then
selfSay('Test msg', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(getPlayerItemCount(cid, 11400) >= 5) then
if(doPlayerRemoveItem(cid, 11400, 5)) then
doPlayerAddItem(cid, 2157, 5)
selfSay('Here you are.', cid)
else
selfSay('Test msg', cid)
end
else
selfSay('Test msg', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
talkState[talkUser] = 0
selfSay('Ok then.', cid)
end


if(msgcontains(msg, 'item2') or msgcontains(msg, 'item22')) then
selfSay('Test msg2', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(getPlayerItemCount(cid, 11400) >= 5) then
if(doPlayerRemoveItem(cid, 11400, 5)) then
doPlayerAddItem(cid, 3954, 1)
selfSay('Here you are.', cid)
else
selfSay('Test msg2', cid)
end
else
selfSay('Test msg2', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
talkState[talkUser] = 0
selfSay('Ok then.', cid)
end

if(msgcontains(msg, 'item3') or msgcontains(msg, 'item33')) then
selfSay('Test msg3', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(getPlayerItemCount(cid, 11400) >= 5) then
if(doPlayerRemoveItem(cid, 11400, 5)) then
doPlayerAddItem(cid, 3954, 1)
selfSay('Here you are.', cid)
else
selfSay('Test msg3', cid)
end
else
selfSay('Test msg3', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
talkState[talkUser] = 0
selfSay('Ok then.', cid)
end

return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited by a moderator:
I haven't tested this but see if this works :p
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 playerItems = {
        {
            item = 11400,
            removeAmount = 5,
            addItem = 2157,
            addItemAmount = 5,
            itemMsg = {
                'item1', 'item11'
            },
            say = {
                advertisement = 'Would you like to purchase this item?',
                giveItem = 'Here you are.',
                notEnoughItems = 'Sorry you don\'t have the required amount of items',
                nextTime = 'ok then, see you next time'
            }
        },
        {
            item = 11400,
            removeAmount = 5,
            addItem = 3954,
            addItemAmount = 1,
            itemMsg = {
                'item2', 'item22'
            },
            say = {
                advertisement = 'Would you like to purchase this item?',
                giveItem = 'Here you are.',
                notEnoughItems = 'Sorry you don\'t have the required amount of items',
                nextTime = 'ok then, see you next time'
            }
        }
    }

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    for i = 1, #playerItems do
        if(msgcontains(msg, isInArray(playerItems[i].itemMsg, msg)) then
            selfSay(playerItems[i].say['advertisement'], cid)
            talkState[talkUser] = 1
            if(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
                if(getPlayerItemCount(cid, playerItems[i].item) >= playerItems[i].removeAmount) then
                    if(doPlayerRemoveItem(cid, playerItems[i].item, playerItems[i].removeAmount)) then
                        doPlayerAddItem(cid, playerItems[i].addItem, playerItems[i].addItemAmount)
                        selfSay(playerItems[i].say['giveItem'], cid)
                    end
                else
                    selfSay(playerItems[i].say['notEnoughItems'], cid)
                end
                talkState[talkUser] = 0
            elseif(msgcontains(msg, 'no') and talkState[talkUser] == 1) then
                talkState[talkUser] = 0
                selfSay(playerItems[i].say['nextTime'], cid)
            end
        return true
        end
    end
    npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
    npcHandler:addModule(FocusModule:new())

  function isInArray(array, lookFor)
     if(type(array) == 'table') then
       for key, value in pairs(array) do
         if value == lookFor then
           return value
         end
       end
     end
     return nil
   end
 
Last edited:
Back
Top