• 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 Npc that changes items

kikos

Well-Known Member
Joined
May 30, 2009
Messages
546
Reaction score
77
Location
Barcelona
First of all i'm using tfs 0.3.6

I want to do a npc that asks for 4 items (for example) and removes it from you, then adds to you a complete different item (just one)

I don't have any idea on how to .lua. I was just entering things that seemed logical but i can't make it work!
thats why i'm here


I'm using this one, the npc answers to everything but keeps saying You don't have the required items (i have them all)

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, 'acces') or msgcontains(msg, 'quests')) then
        selfSay('Did you bring me 10 golden nuggets, 10 magic plate armor, 10 golden legs and 10 mastermind shields?', cid)
        talkState[talkUser] = 1
    elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
        if(getPlayerItemCount(cid, 2157) >= 1 and getPlayerItemCount(cid, 2470) >= 1 and getPlayerItemCount(cid, 2472) >= 1 and getPlayerItemCount(cid, 2514)) >= 1 then
            if (doPlayerRemoveItem(cid,2157,1) and doPlayerRemoveItem(cid,2470,1) and doPlayerRemoveItem(cid,2472,1) and doPlayerRemoveItem(cid,2514,1)) == 0 then
                doPlayerAddItem(cid, 11401)
                selfSay('Use this item to gain acces for the quests.', cid)
            else
                selfSay('You dont have the required items.', cid)
            end
        else
            selfSay('You dont have the required items.', cid)
        end
        talkState[talkUser] = 0
    elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser]) == TRUE) then
        talkState[talkUser] = 0
        selfSay('Maybe later?.', cid)
    end

    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

thanks!
 
Last edited:
getPlayerItemCount will return a number.
Code:
if (getPlayerItemCount(cid,2470) >= 1) >= 1
Means (lets suppose you have 1 item)
Code:
if (1 >= 1) >= 1
Now this expression will return a boolean, true or false:
Code:
if (TRUE) >= 1
And then that doesn't make any sense does it?


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, 'acces') or msgcontains(msg, 'quests')) then
selfSay('Did you bring me 10 golden nuggets, 10 magic plate armor, 10 golden legs and 10 mastermind shields?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(getPlayerItemCount(cid, 2157) >= 1 and getPlayerItemCount(cid, 2470) >= 1 and getPlayerItemCount(cid, 2472) >= 1 and getPlayerItemCount(cid, 2514)) then
doPlayerRemoveItem(cid,2157,1)
doPlayerRemoveItem(cid,2470,1)
doPlayerRemoveItem(cid,2472,1)
doPlayerRemoveItem(cid,2514,1)
doPlayerAddItem(cid, 11401, 1)
selfSay('Use this item to gain acces for the quests.', cid)
else
selfSay('You dont have the required items.', cid)
end
else
selfSay('You dont have the required items.', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser]) == TRUE) then
talkState[talkUser] = 0
selfSay('Maybe later?.', cid)
end

return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
getPlayerItemCount will return a number.
Code:
if (getPlayerItemCount(cid,2470) >= 1) >= 1
Means (lets suppose you have 1 item)
Code:
if (1 >= 1) >= 1
Now this expression will return a boolean, true or false:
Code:
if (TRUE) >= 1
And then that doesn't make any sense does it?


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, 'acces') or msgcontains(msg, 'quests')) then
selfSay('Did you bring me 10 golden nuggets, 10 magic plate armor, 10 golden legs and 10 mastermind shields?', cid)
talkState[talkUser] = 1
elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
if(getPlayerItemCount(cid, 2157) >= 1 and getPlayerItemCount(cid, 2470) >= 1 and getPlayerItemCount(cid, 2472) >= 1 and getPlayerItemCount(cid, 2514)) then
doPlayerRemoveItem(cid,2157,1)
doPlayerRemoveItem(cid,2470,1)
doPlayerRemoveItem(cid,2472,1)
doPlayerRemoveItem(cid,2514,1)
doPlayerAddItem(cid, 11401, 1)
selfSay('Use this item to gain acces for the quests.', cid)
else
selfSay('You dont have the required items.', cid)
end
else
selfSay('You dont have the required items.', cid)
end
talkState[talkUser] = 0
elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser]) == TRUE) then
talkState[talkUser] = 0
selfSay('Maybe later?.', cid)
end

return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

thanks for your answer! now i get this using ur code

Code:
[28/04/2014 01:56:31] [Error - LuaScriptInterface::loadFile] data/npc/scripts/quests.lua:21: '<eof>' expected near 'elseif'
[28/04/2014 01:56:31] [Warning - NpcScript::NpcScript] Cannot load script: data/npc/scripts/quests.lua
[28/04/2014 01:56:31] data/npc/scripts/quests.lua:21: '<eof>' expected near 'elseif'
 
Last edited:
FYI, there's another strange condition test in the script:

elseif(msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser]) == TRUE) then

That one will work ok, but this is the same thing, except easier to read:

elseif (msgcontains(msg, 'no') and isInArray({1}, talkState[talkUser]) ) then

When if's are evaluated, the conditions are resolved to (true or false), then the logical operators (and, or) are applied and the final result is evaluated to (true or false). Also in most languages, the convention is that a function called isXxx() will return one of (true, false).

if (xx == TRUE) then ... and if (xx) then ... are the same.

NB: Just for completeness ... most languages do some optimization to avoid unnecessary evaluations when the and logical operator is used. This changes a detail or two of what I said above, but not the meaning.

@Santi ... is that a "Poison Dart Frog"?
 

Similar threads

Back
Top