• 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 1.X+ How to make NPC say something in greetCallback(cid)

guiismiti

Well-Known Member
Joined
May 19, 2014
Messages
315
Solutions
3
Reaction score
68
Hello!
I am using a modal window on greet for ship travelling.

If the player is pz locked or doesn't have enough gold, the NPC needs to say it.
Problem - if I use selfSay or npcHandler:say inside the greet function, a random NPC will say those messages, and not the NPC the player is talking to.
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 shipDestinations = {
    ["Ab'dendriel (130gp)"] = {
        price = 130,
        destinyPos = Position(32734, 31668, 6)
    },
    ["Carlin (110gp)"] = {
        price = 110,
        destinyPos = Position(32387, 31820, 6)
    },
    ["Thais (40gp)"] = {
        price = 40,
        destinyPos = Position(32310, 32210, 6)
    },
    ["Venore (170gp)"] = {
        price = 170,
        destinyPos = Position(32954, 32022, 6)
    }
}

local function greetCallback(cid)
    local player = Player(cid)
   
    if player:isPzLocked() then
        npcHandler:say("You cannot travel in this condition.", cid)
       
        return true
    end
   
    local window = ModalWindow{title = "Destinations", message = "Select a destination"}
   
    for k, v in pairs(shipDestinations) do
        window:addChoice(k)
    end
   
    window:addButton("Select", function(button, choice)
        local chosenDestiny = shipDestinations[choice.text]
       
        if not player:removeMoneyNpc(chosenDestiny.price) then
            npcHandler:say("You don't have enough gold.", cid)
           
            return true
        else
            player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            player:teleportTo(chosenDestiny.destinyPos)
            chosenDestiny.destinyPos:sendMagicEffect(CONST_ME_TELEPORT)
        end
    end)
   
    window:setDefaultEnterButton("Select")
    window:addButton("Cancel")
    window:setDefaultEscapeButton("Cancel")
    window:sendToPlayer(player)
   
    return true
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setMessage(MESSAGE_GREET, "Welcome on board, Sir |PLAYERNAME|.")
npcHandler:setMessage(MESSAGE_FAREWELL, "Good bye. Recommend us if you were satisfied with our service.")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye then.")

npcHandler:addModule(FocusModule:new())

Can anybody help, please?
 
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 shipDestinations = {
    ["Ab'dendriel (130gp)"] = {
        price = 130,
        destinyPos = Position(32734, 31668, 6)
    },
    ["Carlin (110gp)"] = {
        price = 110,
        destinyPos = Position(32387, 31820, 6)
    },
    ["Thais (40gp)"] = {
        price = 40,
        destinyPos = Position(32310, 32210, 6)
    },
    ["Venore (170gp)"] = {
        price = 170,
        destinyPos = Position(32954, 32022, 6)
    }
}

local function greetCallback(cid)
     local player = Player(cid)
  
    if player:isPzLocked()
        npcHandler:setMessage(MESSAGE_GREET, "You cannot travel in this condition.")
        return true
    end
  
    local window = ModalWindow{title = "Destinations", message = "Select a destination"}
  
    for k, v in pairs(shipDestinations) do
        window:addChoice(k)
    end
  
    window:addButton("Select", function(button, choice)
        local chosenDestiny = shipDestinations[choice.text]
      
        if not player:removeMoneyNpc(chosenDestiny.price) then
            npcHandler:setMessage(MESSAGE_GREET, "You don't have enough gold.")   
            return true
        else
            player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            player:teleportTo(chosenDestiny.destinyPos)
            chosenDestiny.destinyPos:sendMagicEffect(CONST_ME_TELEPORT)
        end
    end)
  
    window:setDefaultEnterButton("Select")
    window:addButton("Cancel")
    window:setDefaultEscapeButton("Cancel")
    window:sendToPlayer(player)
  
    return true
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setMessage(MESSAGE_GREET, "Welcome on board, Sir |PLAYERNAME|.")
npcHandler:setMessage(MESSAGE_FAREWELL, "Good bye. Recommend us if you were satisfied with our service.")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye then.")

npcHandler:addModule(FocusModule:new())

try this
 
I just tried it, still got "Welcome on board, Sir |PLAYERNAME|." as greet message, when I said 'hi'.

I also tried removing, from the last lines of the code:
Code:
npcHandler:setMessage(MESSAGE_GREET, "Welcome on board, Sir |PLAYERNAME|.")
But then it just used the default greet message when I said 'hi', which is defined in npc/lib/npcsystem/npchandler.lua:
Code:
        messages = {
            -- These are the default replies of all npcs. They can/should be changed individually for each npc.
            [MESSAGE_GREET] = "Greetings, |PLAYERNAME|.",
 
I just tried it, still got "Welcome on board, Sir |PLAYERNAME|." as greet message, when I said 'hi'.

I also tried removing, from the last lines of the code:
Code:
npcHandler:setMessage(MESSAGE_GREET, "Welcome on board, Sir |PLAYERNAME|.")
But then it just used the default greet message when I said 'hi', which is defined in npc/lib/npcsystem/npchandler.lua:
Code:
        messages = {
            -- These are the default replies of all npcs. They can/should be changed individually for each npc.
            [MESSAGE_GREET] = "Greetings, |PLAYERNAME|.",
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 shipDestinations = {
    ["Ab'dendriel (130gp)"] = {
        price = 130,
        destinyPos = Position(32734, 31668, 6)
    },
    ["Carlin (110gp)"] = {
        price = 110,
        destinyPos = Position(32387, 31820, 6)
    },
    ["Thais (40gp)"] = {
        price = 40,
        destinyPos = Position(32310, 32210, 6)
    },
    ["Venore (170gp)"] = {
        price = 170,
        destinyPos = Position(32954, 32022, 6)
    }
}

local function greetCallback(cid)
     local player = Player(cid)

    if player:isPzLocked() then
        npcHandler:setMessage(MESSAGE_GREET, "You cannot travel in this condition.")
        return true
    else
        npcHandler:setMessage(MESSAGE_GREET, "Welcome on board, Sir |PLAYERNAME|.")
    end
    local window = ModalWindow{title = "Destinations", message = "Select a destination"}

    for k, v in pairs(shipDestinations) do
        window:addChoice(k)
    end

    window:addButton("Select", function(button, choice)
        local chosenDestiny = shipDestinations[choice.text]
   
        if not player:removeMoneyNpc(chosenDestiny.price) then
            npcHandler:setMessage(MESSAGE_GREET, "You don't have enough gold.")
            return true
        else
            player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
            player:teleportTo(chosenDestiny.destinyPos)
            chosenDestiny.destinyPos:sendMagicEffect(CONST_ME_TELEPORT)
        end
    end)

    window:setDefaultEnterButton("Select")
    window:addButton("Cancel")
    window:setDefaultEscapeButton("Cancel")
    window:sendToPlayer(player)

    return true
end

npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
npcHandler:setMessage(MESSAGE_FAREWELL, "Good bye. Recommend us if you were satisfied with our service.")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye then.")

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

Im not sure about the You don't have enough gold line though.

I feel like you should use creaturesaycallback for this, and not greetcallback for the script.
Maybe you have to use npcHandler:say("", cid) there instead of setMessage, you're gonna have to try it ig.
ex. I know this would work. Because I use a similar type in my npc's
Lua:
local function greetCallback(cid)
    local player = Player(cid)
    local pz = player:isPzLocked()
    if pz then
        npcHandler:setMessage(MESSAGE_GREET, "You have PZ")
    else
        npcHandler:setMessage(MESSAGE_GREET, "Shit is working, |PLAYERNAME|.")
    end
    return true
end
 
Maybe you have to use npcHandler:say("", cid)
That is the first thing I tried - and a random NPC from anywhere will tell me I have no enough gold.

The problem is the modal window - the pz lock test is working, setting the greet message.
NPCs can't wait for a modal window response to set the greet message.

If I use creaturesaycallback, can I open this window just by saying 'hi'?
Or will this function run only when I say something after 'hi'?
 
That is the first thing I tried - and a random NPC from anywhere will tell me I have no enough gold.

The problem is the modal window - the pz lock test is working, setting the greet message.
NPCs can't wait for a modal window response to set the greet message.

If I use creaturesaycallback, can I open this window just by saying 'hi'?
Or will this function run only when I say something after 'hi'?
creaturesaycallback will react only if the player is in focus, so yeah, after saying hi.

You could do it like, hi and after they say sail. And in the greet you can say like Greetings |PLAYERNAME|, where do you want to {sail}?, and they say sail to get the modalwindow.
 
You could do it like, hi and after they say sail.
Yea, I know... still I think I prefer to use the greet function for the modal window and send a cancel message instead of NPC talk.
It is more practical for players, and the cancel message is not a big loss for the server.
 
Back
Top