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

[TFS 1.0] NPC will say one of x random lines when you greet them.

Jaed Le Raep

★Gaeming★
Joined
Sep 3, 2007
Messages
1,298
Reaction score
446
So, for this request I was hoping someone could make what I think is a pretty simple template. When a player says hi to a NPC, they'll say one of a set of 10 lines. This will only happen when they greet the npc, and the npc will have no other dialogue with the player until they say bye and re-greet them.

Also, while I'm requesting this, can someone please refresh my memory and tell me how have NPCs say a random line as they walk around? This would be without them being spoken to, every x seconds they have x chance of saying x line(s).
 
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
random_word_arena = 0
local random_texts = {'Welcome to the Arena.', 'Fighting on three levels, only here.', 'Come in, don\'t be shy', 'Good challenges, epic rewards, only here.'}
local random_texts_chance = 10 -- percent
local random_texts_interval = 5 -- seconds

Code:
function onThink()
   if(random_word_arena < os.time()) then
     random_word_arena = (os.time() + random_texts_interval)
     if(math.random(1, 100) < random_texts_chance) then
       selfSay(random_texts[math.random(1, #random_texts)])
     end
   end
   npcHandler:onThink()
end
function onPlayerEndTrade(cid)         npcHandler:onPlayerEndTrade(cid)       end
function onPlayerCloseChannel(cid)       npcHandler:onPlayerCloseChannel(cid)     end
function greetCallback(cid)
   local talkUser = cid
   local msg = {
     "Welcome to the {Arena}, " .. getPlayerName(cid) .. ".",
     "No need to extend that discussion. If you want to {fight} just ask.",
     "Welcome, " .. getPlayerName(cid) .. ". Welcome to the {Arena}.",
     "Be greeted.",
     "Hello, " .. getPlayerName(cid) .. "."
   }
   npcHandler:setMessage(MESSAGE_GREET, msg[math.random(1, #msg)])
   talkState[talkUser] = 0
   return true
end

Code:
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
 
Lol, well that was quick. Much appreciated, will test them right now and let you know how they work out :)

Edit:
Got this error, over and over again.

7j7Qs.png
 
Lol, well that was quick. Much appreciated, will test them right now and let you know how they work out :)
I had existing script so I just pasted it there.
Use different random_word variable for every npc
if you use same, they won't speak or speak very rarely
 
I had existing script so I just pasted it there.
Use different random_word variable for every npc
if you use same, they won't speak or speak very rarely

Thanks for the help :) Any thoughts on the error I received?

Edit: Just noticed the last parsed code. Hmm.. where would it go?

It's been about two years since I've worked on OTs and I'm super rusty, so a lot of this is about memory reflex for me (which isn't proving to be very reliable x.x)
 
It isn't working:

Code:
function onThink(interval)
  if(random_word_arena < os.time()) then
    random_word_arena = (os.time() + random_texts_interval)
    if(math.random(1, 100) < random_texts_chance) then
      selfSay(random_texts[math.random(1, #random_texts)])
    end
  end
  npcHandler:onThink()
end

function onPlayerEndTrade(cid)        npcHandler:onPlayerEndTrade(cid)      end
function onPlayerCloseChannel(cid)      npcHandler:onPlayerCloseChannel(cid)    end
function greetCallback(cid)
  local talkUser = cid
  local msg = {
    "Welcome to the {Arena}, " .. getPlayerName(cid) .. ".",
    "No need to extend that discussion. If you want to {fight} just ask.",
    "Welcome, " .. getPlayerName(cid) .. ". Welcome to the {Arena}.",
    "Be greeted.",
    "Hello, " .. getPlayerName(cid) .. "."
  }
  npcHandler:setMessage(MESSAGE_GREET, msg[math.random(1, #msg)])
  npcHandler:setCallback(CALLBACK_GREET, greetCallback)
  talkState[talkUser] = 0
  return true
end
 
Thanks @zbizu and @Ninja for all the help. This is the final product.

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
random_word_arena = 0
local random_texts = {'Welcome to the Arena.', 'Fighting on three levels, only here.', 'Come in, don\'t be shy', 'Good challenges, epic rewards, only here.'}
local random_texts_chance = 10 -- percent
local random_texts_interval = 5 -- seconds

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(interval)
  if(random_word_arena < os.time()) then
    random_word_arena = (os.time() + random_texts_interval)
    if(math.random(1, 100) < random_texts_chance) then
      selfSay(random_texts[math.random(1, #random_texts)])
    end
  end
  npcHandler:onThink()
end

function onPlayerEndTrade(cid)        npcHandler:onPlayerEndTrade(cid)      end
function onPlayerCloseChannel(cid)      npcHandler:onPlayerCloseChannel(cid)    end
function greetCallback(cid)
  local talkUser = cid
  local msg = {
    "Welcome to the {Arena}, " .. getPlayerName(cid) .. ".",
    "No need to extend that discussion. If you want to {fight} just ask.",
    "Welcome, " .. getPlayerName(cid) .. ". Welcome to the {Arena}.",
    "Be greeted.",
    "Hello, " .. getPlayerName(cid) .. "."
  }
  npcHandler:setMessage(MESSAGE_GREET, msg[math.random(1, #msg)])
  npcHandler:setCallback(CALLBACK_GREET, greetCallback)
  talkState[talkUser] = 0
  return true
end

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