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

Lua NPC release focus

maxilos

New Member
Joined
Nov 1, 2009
Messages
7
Solutions
1
Reaction score
3
Hi. I have trouble with my script.
I want to npc relase focus after talk with player. Here is example code:

LUA:
npcHandler:say({
    "some text",
    "some another text",
    "blablabla"
}, cid)

npcHandler:releaseFocus(cid)

And my problem is: npc release focus on player before he talk something to player.
How do it?
 
Hi. I have trouble with my script.
I want to npc relase focus after talk with player. Here is example code:

LUA:
npcHandler:say({
    "some text",
    "some another text",
    "blablabla"
}, cid)

npcHandler:releaseFocus(cid)

And my problem is: npc release focus on player before he talk something to player.
How do it?
For starters you could post the whole script.
 
Ok sorry.
Here is my code:

LUA:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local voices = {
    { text = "Weak chest? Why not dish out some magic!" },
    { text = "Wield a staff - the magician's weapon!" },
    { text = "Flay your foes with fireballs!" },
    { text = "Weak punch? Deal out some heavy damage with magic!" },
    { text = "Staffs, wands, rods - the true weapons of power!" },
    { text = "Shoot your enemy with energy spheres!" }
}
npcHandler:addModule(VoiceModule:new(voices))

local function creatureSayCallback(cid, type, msg)
    local player = Player(cid)
   
    if msgcontains(msg, "hi") or msgcontains(msg, "hello") and not npcHandler:isFocused(cid) then
        npcHandler:addFocus(cid)
        if player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) < 2 then
            npcHandler:say("Hello there! Interested in magic? Wish to become an acolyte of the arcane, a neophyte of nature? Then the staff is your weapon of choice! Care to try it out? Of course you do. Say {yes}.", cid)
        elseif player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) >= 2 then
            npcHandler:say("Hello again! Do you want me to explain how distance fighting for mages works {again}, or did you lose your {staff}?", cid)
            npcHandler.topic[cid] = 50
        end
    elseif(not npcHandler:isFocused(cid)) then return false end
   
    if msgcontains(msg, "bye") or msgcontains(msg, "farewell") then
        npcHandler:say("<bows> Goodbye. May we meet again!")
        npcHandler:releaseFocus(cid)
    elseif msgcontains(msg, "yes") or msgcontains(msg, "ready") then
        if npcHandler.topic[cid] == 20 then
            npcHandler:say({
                "Alright, let's get startted! <snaps fingers> Here's your staff! Just drag it from your inventory to your weapon slot to use it. ...",
                "<chuckles> Have a blast and just visit Shanoya next door to try out mundane distance weapon, too, afterwards."
            }, cid)
            player:setStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine, 2)
            player:addItem(19391, 1)
            npcHandler:resetNpc(cid)
            npcHandler:releaseFocus(cid)
        elseif npcHandler.topic[cid] == 0 then
            npcHandler:say({
                "<chuckles> Good. We magic wielders prefer to fight from a distance, as our defense and armor is lot weaker that that of the knights. But we deal out more damage with a hit than they do. ...",
                "Stay a little distance from a monster. Too far away staff won't hit the monster, come too close and the monster will hit YOU. ...",
                "First, drag your staff to the weapon slot in your inventory to use it. Then, to attack, RIGHT-CLICK on a monster on the screen or battle list and choose 'ATTACK'. ...",
                "Remember that a staff also need {mana} to work, so make sure you always have some food, or you won't regenerate mana. There's food in the chest outside, and some blueberry bushes. Oh yeas, one other thing! mana will only regenerated inside tents. ...",
                "{Ready} for the staff?"
            }, cid)
            npcHandler.topic[cid] = 20
        end
    elseif msgcontains(msg, "no") then
        if npcHandler.topic[cid] == 50 then
            npcHandler.say("<chuckles> Oh well, if you're sure... come back if you change your mind. Have fun!", cid)
            npcHandler:resetNpc(cid)
            npcHandler:releaseFocus(cid)
        end
    elseif msgcontains(msg, "again") and npcHandler.topic[cid] == 50 then
        npcHandler:say({
            "Gladly. We magic wielders prefer to fight from a distance, as our defense and armor is a lot weaker than that of the knights. But we deal out more damage with a hit than they do. ...",
            "Stay at a little distance from a monster. Too far away: the staff won't hit the monster; come too close and the monster will hit YOU. ...",
            "Drag your staff to the weapon slot in your inventory to use it, right-click on a monster and choose 'attack'. ...",
            "Check your mana and always have some food with you. When you're done, visit Shanoya next door for more... mundane distance fighting. <chuckles> Enjoy!"
        },cid)
        npcHandler:resetNpc(cid)
        npcHandler:releaseFocus(cid)
    end
end

keywordHandler:addKeyword({'job'}, StdModule.say, {npcHandler = npcHandler, text = "I am a sorcerer apprentice to the master {sorcerer Estrella}."})
keywordHandler:addKeyword({'estrella'}, StdModule.say, {npcHandler = npcHandler, text = "She is the local vocation trainer for {sorcerers} and my esteemed master."})
keywordHandler:addKeyword({'sorcerer', 'estrella'}, StdModule.say, {npcHandler = npcHandler, text = "She is the local vocation trainer for {sorcerers} and my esteemed master."})
keywordHandler:addAliasKeyword({'sorcerer'})
keywordHandler:addKeyword({'ula'}, StdModule.say, {npcHandler = npcHandler, text = "<chuckles> Cute girl. Has some mean fireballs at the ready, too."})
keywordHandler:addKeyword({'shanoya'}, StdModule.say, {npcHandler = npcHandler, text = "<chuckles> Don't cross her in a dark wood! Or was it dark mood? Anyway. Tough lady. Teaches distance fighting for non-magic people, next door."})

npcHandler:setMessage(MESSAGE_WALKAWAY, '<chuckles> Always in a hurry, eh?')

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
The main problem here is you have "hi" and "bye" in their own segment blocks and this should not be the case. Did you copy and paste to put this npc script together? I would imagine you did.

This whole section of code is hi's segment block
LUA:
    if msgcontains(msg, "hi") or msgcontains(msg, "hello") and not npcHandler:isFocused(cid) then
        npcHandler:addFocus(cid)
        if player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) < 2 then
            npcHandler:say("Hello there! Interested in magic? Wish to become an acolyte of the arcane, a neophyte of nature? Then the staff is your weapon of choice! Care to try it out? Of course you do. Say {yes}.", cid)
        elseif player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) >= 2 then
            npcHandler:say("Hello again! Do you want me to explain how distance fighting for mages works {again}, or did you lose your {staff}?", cid)
            npcHandler.topic[cid] = 50
        end
    elseif(not npcHandler:isFocused(cid)) then
        return false
    end

While this is bye's segment block
LUA:
    if msgcontains(msg, "bye") or msgcontains(msg, "farewell") then
        npcHandler:say("<bows> Goodbye. May we meet again!")
        npcHandler:releaseFocus(cid)
    elseif msgcontains(msg, "yes") or msgcontains(msg, "ready") then
        if npcHandler.topic[cid] == 20 then
            npcHandler:say({
                "Alright, let's get startted! <snaps fingers> Here's your staff! Just drag it from your inventory to your weapon slot to use it. ...",
                "<chuckles> Have a blast and just visit Shanoya next door to try out mundane distance weapon, too, afterwards."
            }, cid)
            player:setStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine, 2)
            player:addItem(19391, 1)
            npcHandler:resetNpc(cid)
            npcHandler:releaseFocus(cid)
        elseif npcHandler.topic[cid] == 0 then
            npcHandler:say({
                "<chuckles> Good. We magic wielders prefer to fight from a distance, as our defense and armor is lot weaker that that of the knights. But we deal out    more damage with a hit than they do. ...",
                "Stay a little distance from a monster. Too far away staff won't hit the monster, come too close and the monster will hit YOU. ...",
                "First, drag your staff to the weapon slot in your inventory to use it. Then, to attack, RIGHT-CLICK on a monster on the screen or battle list and choose 'ATTACK'. ...",
                "Remember that a staff also need {mana} to work, so make sure you always have some food, or you won't regenerate mana. There's food in the chest outside, and some blueberry bushes. Oh yeas, one other thing! mana will only regenerated inside tents. ...",
                "{Ready} for the staff?"
            }, cid)
            npcHandler.topic[cid] = 20
        end
    elseif msgcontains(msg, "no") then
        if npcHandler.topic[cid] == 50 then
            npcHandler.say("<chuckles> Oh well, if you're sure... come back if you change your mind. Have fun!", cid)
            npcHandler:resetNpc(cid)
            npcHandler:releaseFocus(cid)
        end
    elseif msgcontains(msg, "again") and npcHandler.topic[cid] == 50 then
        npcHandler:say({
            "Gladly. We magic wielders prefer to fight from a distance, as our defense and armor is a lot weaker than that of the knights. But we deal out more damage with a hit than they do. ...",
            "Stay at a little distance from a monster. Too far away: the staff won't hit the monster; come too close and the monster will hit YOU. ...",
            "Drag your staff to the weapon slot in your inventory to use it, right-click on a monster and choose 'attack'. ...",
            "Check your mana and always have some food with you. When you're done, visit Shanoya next door for more... mundane distance fighting. <chuckles> Enjoy!"
        },cid)
        npcHandler:resetNpc(cid)
        npcHandler:releaseFocus(cid)
    end
 
Last edited by a moderator:
So what are we talking about here? You can't say good bye to an npc which you never said hi to right? the bye section should be nested inside of hi's segment block.
 
this elseif between the two segments should be removed
LUA:
elseif(not npcHandler:isFocused(cid)) then
        return false
    end

instead it should be
LUA:
local function creatureSayCallback(cid, type, msg)
    local player = Player(cid)

    if msgcontains(msg, "hi") or msgcontains(msg, "hello") and not npcHandler:isFocused(cid) then
        npcHandler:addFocus(cid)
        if player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) < 2 then
            npcHandler:say("Hello there! Interested in magic? Wish to become an acolyte of the arcane, a neophyte of nature? Then the staff is your weapon of choice! Care to try it out? Of course you do. Say {yes}.", cid)
        elseif player:getStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine) >= 2 then
            npcHandler:say("Hello again! Do you want me to explain how distance fighting for mages works {again}, or did you lose your {staff}?", cid)
            npcHandler.topic[cid] = 50
        end
        if msgcontains(msg, "bye") or msgcontains(msg, "farewell") then
            npcHandler:say("<bows> Goodbye. May we meet again!")
            npcHandler:releaseFocus(cid)
        elseif msgcontains(msg, "yes") or msgcontains(msg, "ready") then
            if npcHandler.topic[cid] == 20 then
                npcHandler:say({
                    "Alright, let's get startted! <snaps fingers> Here's your staff! Just drag it from your inventory to your weapon slot to use it. ...",
                    "<chuckles> Have a blast and just visit Shanoya next door to try out mundane distance weapon, too, afterwards."
                }, cid)
                player:setStorageValue(Storage.IslandOfDestiny.ChildOfDestiny.QuestLine, 2)
                player:addItem(19391, 1)
                npcHandler:resetNpc(cid)
                npcHandler:releaseFocus(cid)
            elseif npcHandler.topic[cid] == 0 then
                npcHandler:say({
                    "<chuckles> Good. We magic wielders prefer to fight from a distance, as our defense and armor is lot weaker that that of the knights. But we deal out    more damage with a hit than they do. ...",
                    "Stay a little distance from a monster. Too far away staff won't hit the monster, come too close and the monster will hit YOU. ...",
                    "First, drag your staff to the weapon slot in your inventory to use it. Then, to attack, RIGHT-CLICK on a monster on the screen or battle list and choose 'ATTACK'. ...",
                    "Remember that a staff also need {mana} to work, so make sure you always have some food, or you won't regenerate mana. There's food in the chest outside, and some blueberry bushes. Oh yeas, one other thing! mana will only regenerated inside tents. ...",
                    "{Ready} for the staff?"
                }, cid)
                npcHandler.topic[cid] = 20
            end
        elseif msgcontains(msg, "no") then
            if npcHandler.topic[cid] == 50 then
                npcHandler.say("<chuckles> Oh well, if you're sure... come back if you change your mind. Have fun!", cid)
                npcHandler:resetNpc(cid)
                npcHandler:releaseFocus(cid)
            end
        elseif msgcontains(msg, "again") and npcHandler.topic[cid] == 50 then
            npcHandler:say({
                "Gladly. We magic wielders prefer to fight from a distance, as our defense and armor is a lot weaker than that of the knights. But we deal out more damage with a hit than they do. ...",
                "Stay at a little distance from a monster. Too far away: the staff won't hit the monster; come too close and the monster will hit YOU. ...",
                "Drag your staff to the weapon slot in your inventory to use it, right-click on a monster and choose 'attack'. ...",
                "Check your mana and always have some food with you. When you're done, visit Shanoya next door for more... mundane distance fighting. <chuckles> Enjoy!"
            },cid)
            npcHandler:resetNpc(cid)
            npcHandler:releaseFocus(cid)
        end
    end
end
 
Not "bye" section is a problem. Look:
Code:
    elseif msgcontains(msg, "again") and npcHandler.topic[cid] == 50 then
        npcHandler:say({
            "Gladly. We magic wielders prefer to fight from a distance, as our defense and armor is a lot weaker than that of the knights. But we deal out more damage with a hit than they do. ...",
            "Stay at a little distance from a monster. Too far away: the staff won't hit the monster; come too close and the monster will hit YOU. ...",
            "Drag your staff to the weapon slot in your inventory to use it, right-click on a monster and choose 'attack'. ...",
            "Check your mana and always have some food with you. When you're done, visit Shanoya next door for more... mundane distance fighting. <chuckles> Enjoy!"
        },cid)
        npcHandler:resetNpc(cid)
        npcHandler:releaseFocus(cid)
    end

If i say "again" to NPC, he release focus on me before he say all sentences. To be more precise, he does not say anything, just release focus and walk away.
 
Back
Top