• 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 SOLVED on How to: Make a NPC script that send random messages on hi / hello?

Vikarious

New Member
Joined
Dec 16, 2008
Messages
93
Reaction score
2
I have a lot o sailors on docks and them all say always the same.

I want to make a script with random speechs when someone say hi, I may put a lot of speechs on this script so I could have a small database of speechs to all sailors in question, it doesn't matter if they repeat speechs sometimes.

Does it need to be a script or can it be done using the paramenter on .xml of the NPC in question?

I'm using "9.31 - The Forgotten Server - Version 0.2.11.2 (Mystic Spirit) - R4"

Here is how I set up the NPC.xml file:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Sailor" script="data/npc/scripts/sailor.lua" walkinterval="200" floorchange="0">
	<health now="100" max="100"/>
	<look type="93" head="114" body="0" legs="0" feet="0" addons="0"/>
	<parameters>
	<parameter key="message_greet" value="Ya!! Hello |PLAYERNAME|!!" />
	<parameter key="message_farewell" value="Nice travels |PLAYERNAME|!!" />
	</parameters>
</npc>

Atcual script file (it has some keywords but theyre all aritifical, but even funny, if it could be done in a way that I could keep keywords, plus add the random speechs to hi/bye it would be way better!)

Code:
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

-- Don't forget npcHandler = npcHandler in the parameters. It is required for all StdModule functions!
keywordHandler:addKeyword({'sea'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'I love the sea...'})
keywordHandler:addKeyword({'what?'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Ehh?!'})
keywordHandler:addKeyword({'trade'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Yo ho ho one more bottle of rom!!'})
keywordHandler:addKeyword({'jack'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Are you asking me about the Captain Jack Sparrow?'})
keywordHandler:addKeyword({'quest'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Oh yes! The treasure chest... Captain only, knows the right place...'})
keywordHandler:addKeyword({'travel'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Ya!! Nice day to travel!!'})
keywordHandler:addKeyword({'boat'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you have seen the Black Pearl? She is the best ship ever!!'})
keywordHandler:addKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'HA! Jack is the best pirate from all seas!!'})

npcHandler:addModule(FocusModule:new())

I would really appreciate any help =)
 
Last edited:
that's not all
without
npcHandler:addModule(FocusModule:new())

for example like this:
LUA:
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)
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid


	if (doMessageCheck(msg, "hail") or doMessageCheck(msg, "hello") or doMessageCheck(msg, "salutations")) and doMessageCheck(msg, "king") and (not npcHandler:isFocused(cid)) then
		selfSay("I greet thee, my loyal subject "..getCreatureName(cid)..".", cid)
		npcHandler:addFocus(cid)
		talkState[talkUser] = 0
		return true

	elseif(not npcHandler:isFocused(cid)) then return false

-- your stuff

	elseif doMessageCheck(msg, "bye") or doMessageCheck(msg, "farewell") then
		selfSay("Good bye, "..getCreatureName(cid).."!", cid)
		talkState[talkUser] = 0
		npcHandler:releaseFocus(cid)

	end
	return true
end

npcHandler:setMessage(MESSAGE_WALKAWAY, "How rude!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

and klick here :)
 
hmmm alright, but my main problem is that i cant made the function to give a random message if message 'hi' 'hello' etc is

I still need to prove but i was thinking of:

LUA:
randomMessageGreetings = "Drop the cable!", "Nice day to travel huh?", "Ya! Lets get that treasure!", "Yo ho! Who is Jack the pirate?", etc+++

randomMessageFarewell = "See you soon!!", "Care out there!", "May nice winds conduct you!", etc+++


if (doMessageCheck(msg, "hail") or doMessageCheck(msg, "hello") or doMessageCheck(msg, "salutations")) and doMessageCheck(msg, "king") and (not npcHandler:isFocused(cid)) then
		selfSay("randomMessageGreetings", cid)

That's the main idea, so I would be able to add random speechs in the same script to a lot of sailors, but I have no clue of what function to use to make it work =(

And of course I may rep you guys, youre always so cool helping!
 
LUA:
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)
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

	local greet = "Drop the cable!", "Nice day to travel huh?", "Ya! Lets get that treasure!", "Yo ho! Who is Jack the pirate?"}
	local fare = "See you soon!!", "Care out there!", "May nice winds conduct you!"}

	if (doMessageCheck(msg, "hail") or doMessageCheck(msg, "hello") or doMessageCheck(msg, "salutations")) and doMessageCheck(msg, "king") and (not npcHandler:isFocused(cid)) then
		selfSay(greet[math.random(#greet)], cid)
		npcHandler:addFocus(cid)
		talkState[talkUser] = 0
		return true

	elseif(not npcHandler:isFocused(cid)) then return false

-- your stuff

	elseif doMessageCheck(msg, "bye") or doMessageCheck(msg, "farewell") then
		selfSay(fare[math.random(#fare)], cid)
		talkState[talkUser] = 0
		npcHandler:releaseFocus(cid)

	end
	return true
end

npcHandler:setMessage(MESSAGE_WALKAWAY, "How rude!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)

and here klick :)
 
Alright, it look awesome but won't work, first got error on line 14 due to lack of " { ".

Then I add both on line 14 and 15, but console returns this error on any message you send near the npc (even : kajkajkaj):

Code:
[12/12/2011 23:41:55] Lua Script Error: [Npc interface] 
[12/12/2011 23:41:55] data/npc/scripts/sailor.lua:onCreatureSay
[12/12/2011 23:41:55] data/npc/scripts/sailor.lua:17: attempt to call global 'doMessageCheck' (a nil value)
[12/12/2011 23:41:55] stack traceback:
[12/12/2011 23:41:55] 	[C]: in function 'doMessageCheck'
[12/12/2011 23:41:55] 	data/npc/scripts/sailor.lua:17: in function 'callback'
[12/12/2011 23:41:55] 	data/npc/lib/npcsystem/npchandler.lua:390: in function 'onCreatureSay'
[12/12/2011 23:41:55] 	data/npc/scripts/sailor.lua:8: in function <data/npc/scripts/sailor.lua:8>

I search out in the foruns but couldn't be sucessful.

I've seen you re one of the most patient and helpful guys around here actually, I don't want to stole all your time but, I must to say it is very hard for me to script all by myself.

Thanks again!

LUA:
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)
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
 
	local greet = {"Drop the cable!", "Nice day to travel huh?", "Ya! Lets get that treasure!", "Yo ho! Who is Jack the pirate?"}
	local fare = {"See you soon!!", "Care out there!", "May nice winds conduct you!"}
 
	if (doMessageCheck(msg, "hi") or doMessageCheck(msg, "hello") or doMessageCheck(msg, "salutations")) and (not npcHandler:isFocused(cid)) then
		selfSay(greet[math.random(#greet)], cid)
		npcHandler:addFocus(cid)
		talkState[talkUser] = 0
		return true
 
	elseif(not npcHandler:isFocused(cid)) then return false
 
-- your stuff
 
	elseif doMessageCheck(msg, "bye") or doMessageCheck(msg, "farewell") then
		selfSay(fare[math.random(#fare)], cid)
		talkState[talkUser] = 0
		npcHandler:releaseFocus(cid)
 
	end
	return true
end
 
npcHandler:setMessage(MESSAGE_WALKAWAY, "How rude!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
That's because andypsylon is retarded.

Here:

LUA:
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)
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
 
	local greet = {"Drop the cable!", "Nice day to travel huh?", "Ya! Lets get that treasure!", "Yo ho! Who is Jack the pirate?"}
	local fare = {"See you soon!!", "Care out there!", "May nice winds conduct you!"}
 
	if (msgcontains(msg, "hi") or msgcontains(msg, "hello") or msgcontains(msg, "salutations")) and (not npcHandler:isFocused(cid)) then
		selfSay(greet[math.random(#greet)], cid)
		npcHandler:addFocus(cid)
		talkState[talkUser] = 0
		return true
 
	elseif(not npcHandler:isFocused(cid)) then return false
 
-- your stuff
 
	elseif msgcontains(msg, "bye") or msgcontains(msg, "farewell") then
		selfSay(fare[math.random(#fare)], cid)
		talkState[talkUser] = 0
		npcHandler:releaseFocus(cid)
 
	end
	return true
end
 
npcHandler:setMessage(MESSAGE_WALKAWAY, "How rude!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Back
Top