• 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.4]NPC that gives you items in exchange for items

royalpala

Well-Known Member
Joined
Dec 31, 2019
Messages
85
Solutions
1
Reaction score
68
the base code I used is the following, i changed the items id to be different items... my issue is that i want the npc to give you more items but when you asked for items #2, #3, #4 etc the npc says you dont have enough items to exchange, originally the npc worked with only one item, hope you can help me and tell me where i did the mistake

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
 ---- item 1
    if(msgcontains(msg, 'helmet') or msgcontains(msg, 'upgrade')) then
        selfSay('Do you want to give me 500 small diamonds and one leather helmet to {upgrade} in a golden helmet?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if(getPlayerItemCount(cid, 2145) >= 500 and getPlayerItemCount(cid, 2461) >= 1) then
            if(doPlayerRemoveItem(cid, 2145, 500) and doPlayerRemoveItem(cid, 2461, 1)) then
                doPlayerAddItem(cid, 2471, 1)
                selfSay('Here you are.', cid)
            else
                selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
            end

        else
            selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
        end

        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
        talkState[talkUser] = 0
        selfSay('Ok then.', cid)
    end
---item 2
    if(msgcontains(msg, 'helmet') or msgcontains(msg, 'upgrade')) then
        selfSay('Do you want to give me 500 small diamonds and one leather helmet to {upgrade} in a golden helmet?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if(getPlayerItemCount(cid, 2145) >= 500 and getPlayerItemCount(cid, 2461) >= 1) then
            if(doPlayerRemoveItem(cid, 2145, 500) and doPlayerRemoveItem(cid, 2461, 1)) then
                doPlayerAddItem(cid, 2471, 1)
                selfSay('Here you are.', cid)
            else
                selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
            end

        else
            selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
        end

        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
        talkState[talkUser] = 0
        selfSay('Ok then.', cid)
    end
--- item 3
    if(msgcontains(msg, 'helmet') or msgcontains(msg, 'upgrade')) then
        selfSay('Do you want to give me 500 small diamonds and one leather helmet to {upgrade} in a golden helmet?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if(getPlayerItemCount(cid, 2145) >= 500 and getPlayerItemCount(cid, 2461) >= 1) then
            if(doPlayerRemoveItem(cid, 2145, 500) and doPlayerRemoveItem(cid, 2461, 1)) then
                doPlayerAddItem(cid, 2471, 1)
                selfSay('Here you are.', cid)
            else
                selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
            end

        else
            selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
        end

        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser])) then
        talkState[talkUser] = 0
        selfSay('Ok then.', cid)
    end
--- item 4
    if(msgcontains(msg, 'helmet') or msgcontains(msg, 'upgrade')) then
        selfSay('Do you want to give me 500 small diamonds and one leather helmet to {upgrade} in a golden helmet?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if(getPlayerItemCount(cid, 2145) >= 500 and getPlayerItemCount(cid, 2461) >= 1) then
            if(doPlayerRemoveItem(cid, 2145, 500) and doPlayerRemoveItem(cid, 2461, 1)) then
                doPlayerAddItem(cid, 2471, 1)
                selfSay('Here you are.', cid)
            else
                selfSay('Sorry, you don\'t have enough items for the upgrade.', cid)
            end

        else
            selfSay('Sorry, you don\'t have enough items for the upgrade.', 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())
 
Solution
E
you need to use different
talkState[talkUser] = 1

for each if statement, for example

for first if:
talkState[talkUser] = 1

for second one:
talkState[talkUser] = 2

etc etc
you need to use different
talkState[talkUser] = 1

for each if statement, for example

for first if:
talkState[talkUser] = 1

for second one:
talkState[talkUser] = 2

etc etc
 
Solution
Back
Top