• 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 setMessage TFS 1.3

charlyhustle

Member
Joined
Dec 9, 2011
Messages
47
Reaction score
6
Hi guys,

is it possible to use the setMessage function to set a different message each time the player says something?

Because right now I'm using this:

Lua:
npcHandler:setMessage(MESSAGE_FAREWELL, MESSAGES_GOODBYE[math.random(1,3)])

And it kinda works but the random function is just called once, when I start the server, I assume. So it's one of the three messages for the whole serversave period.

Thanks in advance!
 
Solution
Lua:
function creatureFarewellCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GOODBYE[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:releaseFocus(cid)
    end
    return false
end

function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GREET[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:addFocus(cid)
    end
    return false
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewellCallback)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetingCallback)

If the goodbye messages don't...
The normal thing to achieve the behavior you mention is to declare directly in a script
next I show you an example of the aspect of the npc script:
Lua:
local MESSAGES_GOODBYE = {
    "message1",
    "message2",
    "message3"
}

function creatureFarewallCallback(cid)
    local player = Player(talkPlayer)
    if player then
        selfSay(MESSAGES_GOODBYE[math.random(1, 3)], cid)
    end
    return false
end
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewallCallback)


What you do is set an farewall message, when the script is loaded or reloaded
Lua:
npcHandler:setMessage(MESSAGE_FAREWELL, MESSAGES_GOODBYE[math.random(1,3)])
 
The normal thing to achieve the behavior you mention is to declare directly in a script
next I show you an example of the aspect of the npc script:
Lua:
local MESSAGES_GOODBYE = {
    "message1",
    "message2",
    "message3"
}

function creatureFarewallCallback(cid)
    local player = Player(talkPlayer)
    if player then
        selfSay(MESSAGES_GOODBYE[math.random(1, 3)], cid)
    end
    return false
end
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewallCallback)


What you do is set an farewall message, when the script is loaded or reloaded
Lua:
npcHandler:setMessage(MESSAGE_FAREWELL, MESSAGES_GOODBYE[math.random(1,3)])
Alrighty I'll try that. Thank you very much!
 
@Sarah Wesker
I tried it like this:

Lua:
function creatureFarewellCallback(cid)
    local player = Player(cid)
    if player then
        selfSay(MESSAGES_GOODBYE[math.random(1, 3)], cid)
    end
    return false
end

function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        selfSay(MESSAGES_GREET[math.random(1, 3)], cid)
    end
    return false
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewellCallback)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetingCallback)

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

Now the NPC doesn't respond at all. So I can't start a conversation.
Any suggestions maybe?

EDIT:
I edited the code a little bit and now he's responding but not really starting a conversation. he just responds to the greeting and farewell messages.
 
Last edited:
I don't see anything strange in what I post, if you don't mind showing me the complete script, of course it would be much easier to help you ;)
 
Yeah of course. Here you go:

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 npcMissions = {

    [1] = {name = "Rotworms",
        storageMission = 43000,
        windowMsg = "",
    },
    [2] = {name = "Cyclopes",
        storageMission = 43001,
        windowMsg = "",
    },
    [3] = {name = "Mutateds",
        storageMission = 43002,
        windowMsg = "",
    },
    [4] = {name = "Terramites",
        storageMission = 43003,
        windowMsg = "",
    },
    [5] = {name = "Dragons and Dragon Lords",
        storageMission = 43004,
        windowMsg = "",
    },
    [6] = {name = "Hydras",
        storageMission = 43005,
        windowMsg = "",
    },
    [7] = {name = "Giant Spiders",
        storageMission = 43006,
        windowMsg = "",
    },
    [8] = {name = "Nightmares",
        storageMission = 43007,
        windowMsg = "",
    },
    [9] = {name = "Wyrms and Elder Wyrms",
        storageMission = 43008,
        windowMsg = "",
    },
    [10] = {name = "Frost Dragons",
        storageMission = 43009,
        windowMsg = "",
    },

}

local MESSAGES_GREET = {
    [1] = "Hello |PLAYERNAME|. Do you want to do some {tasks} for me?",
    [2] = "Hey buddy! I've got some {tasks} for you. Interested?",
    [3] = "Another day another {task}, am I right?.",
}

local MESSAGES_GOODBYE = { -- This works the same as MESSAGES_GREET. Depending on how many tasks the player has done for the npc. The npc will say different things when he says goodbye to the player.
    [1] = "Farewell |PLAYERNAME|.",
    [2] = "See ya later.",
    [3] = "Goodbye |PLAYERNAME|."
}

local arrayGreetings = {"hi", "Hi", "hello", "Hello", "hey", "Hey", "oi", "Oi", "hola", "Hola"}
local arrayFarewell = {"bye", "Bye", "goodbye", "Goodbye", "good-bye", "Good-Bye", "Good-bye", "cya", "adios", "Adios"}

local messageCompletedAllTasks = "I don't have any more tasks for you."
local taskWindowTitle = "Select an available task"
local taskWindowText = "Tasks"

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    player:setStorageValue(taskSystem)
    
    if msgcontains(msg, "task") then
        local window = ModalWindow(0x10, taskWindowTitle, taskWindowText)
        local hasMissions = false
        for i = 1, #npcMissions do
            if player:getStorageValue(npcMissions[i].storageMission) < 2 then
                    window:addChoice(i, npcMissions[i].name)
                    hasMissions = true
            end
        end
    
        if hasMissions then
            window:addButton(0x00, "Exit")
            window:addButton(0x01, "Select")
            window:setDefaultEnterButton(0x01)
            window:sendToPlayer(player)
            return true
        else
            selfSay(messageCompletedAllTasks, cid)
            return true
        end
    end
return true
end

function creatureFarewellCallback(cid)
    local player = Player(cid)
    if player then
        selfSay(MESSAGES_GOODBYE[math.random(1, 3)], cid)
    end
    return false
end

function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        selfSay(MESSAGES_GREET[math.random(1, 3)], cid)
    end
    return false
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewellCallback)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetingCallback)

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

I assume the problem is that it's just a callback and the greeting doesn't really start the conversation? But I just can't figure it out :p
 
Lua:
function creatureFarewellCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GOODBYE[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:releaseFocus(cid)
    end
    return false
end

function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GREET[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:addFocus(cid)
    end
    return false
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewellCallback)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetingCallback)

If the goodbye messages don't work, add this change:
 
Last edited:
Solution
Lua:
function creatureFarewellCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GOODBYE[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:releaseFocus(cid)
    end
    return false
end

function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GREET[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:addFocus(cid)
    end
    return false
end

npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewellCallback)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetingCallback)

If the goodbye messages don't work, add this change:

I will test it tommorow, thanks a lot. I just didn't find the addFocus method 🤦‍♂️
 
Works! :) The only thing left now is that if I don't say bye he still replies with the standard callback but unfortunately this didn't work:

Lua:
function creatureGreetingCallback(cid)
    local player = Player(cid)
    if player then
        local msg = NpcHandler:parseMessage(MESSAGES_GREET[math.random(1, 3)], {[TAG_PLAYERNAME] = player:getName()})
        selfSay(msg, cid)
        npcHandler:addFocus(cid)
        npcHandler:setMessage(MESSAGE_FAREWELL, MESSAGES_GOODBYE[math.random(1,3)])
    end
    return false
end

probably because it doesnt have a reference inside of the function?
 
help
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/Noodles.lua
data/npc/scripts/lib/npcsystem/npchandler.lua:302: table index is nil
stack traceback:
        [C]: in function '__newindex'
        data/npc/scripts/lib/npcsystem/npchandler.lua:302: in function 'setMessage'
        data/npc/scripts/Noodles.lua:13: in main chunk
[Warning - NpcScript::NpcScript] Can not load script: jack.lua

jack.lua
Code:
dofile('data/npc/scripts/lib/greeting.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

npcHandler:setMessage(MESSAGE_GREET, '<sniff> Woof! <sniff>')
npcHandler:setMessage(MESSAGE_PLACEDINQUEUE, 'Grrr! Woof!')
npcHandler:setMessage(MESSAGE_WALKAWAY, 'Woof?? <sniff> <sniff>')
npcHandler:setMessage(MESSAGE_FAREWELL, 'Woof! <wiggle>')

keywordHandler:addKeyword({'cat','queen','eloise'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'GRRRRRRR! WOOOOOOF! WOOOOOF! WOOOOOF!'})   
keywordHandler:addKeyword({'ferumbras'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Meeep! Meeep!'})
--keywordHandler:addKeyword({'th'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = '<sniff>'})
keywordHandler:addKeyword({'ar'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Woof!'})
keywordHandler:addKeyword({'bo'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = '<wiggle>'})
--keywordHandler:addKeyword({'an'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Grrrr!'})
keywordHandler:addKeyword({'go'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Woof! Woof!'})
keywordHandler:addKeyword({'how are you','king','tibianus'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Wooooof! <wiggle> <wiggle> <wiggle>'})

function creatureSayCallback(cid, type, msg)
    if(npcHandler.focus ~= cid) then
        return false
    end
    
    -- The Postman Missions Quest
    if(msgcontains(msg, "banana skin") or msgcontains(msg, "sniff banana skin")) then
        if(getPlayerStorageValue(cid, 233) == 7) then
            if(getPlayerItemCount(cid, 2219) >= 1) then
                npcHandler:say("<sniff><sniff> ", cid)
                npcHandlerfocus = 1
            end
        end
    elseif(msgcontains(msg, "dirty fur") or msgcontains(msg, "sniff dirty fur")) then
        if(getPlayerStorageValue(cid, 250) == 20) then
            if(getPlayerItemCount(cid, 2220) >= 1) then
                npcHandler:say("<sniff><sniff> ", cid)
                npcHandlerfocus = 2
            end
        end
    elseif(msgcontains(msg, "mouldy cheese") or msgcontains(msg, "sniff mouldy cheese")) then
        if(getPlayerStorageValue(cid, 250) == 21) then
            if(getPlayerItemCount(cid, 2235) >= 1) then
                npcHandler:say("<sniff><sniff> ", cid)
                npcHandlerfocus = 3
            end
        end
    elseif(msgcontains(msg, "yes") or msgcontains(msg, "do you like that")) then
        if(npcHandlerfocus == 1) then
            npcHandler:say("Woof!", cid)
            setPlayerStorageValue(cid, 250, 20)
            npcHandlerfocus = 0
        elseif(npcHandlerfocus == 2) then
            npcHandler:say("Woof!", cid)
            setPlayerStorageValue(cid, 250, 21)
            npcHandlerfocus = 0
        elseif(npcHandlerfocus == 3) then
            npcHandler:say("Meeep! Grrrrr! <spits> ", cid)
            setPlayerStorageValue(cid,233,8)
            npcHandlerfocus = 0
        end
    end

    return true
end   
    
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

data/npc/scripts/lib/npcsystem/npchandler.lua:302: in function
Code:
    function NpcHandler:setMessage(id, newMessage)
        if self.messages then
            self.messages[id] = newMessage
        end
    end
 
Back
Top