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

Keru

Entrepreneur
Joined
Jul 24, 2008
Messages
1,777
Solutions
2
Reaction score
167
Location
USA
Is there any post related to this? I'm so looking into it but the Search tool takes me to something totally different.
If you don't know of any post, can you help me understand the basics of NPC scripting :)

Thanks.
 
I just ran across this issue myself a few days ago, I have a pretty good system going and it's working great for me. I could make a post and show you how I'm working as long as you understand lua. As far as I could find there aren't any npc resources out there.
 
Ok so here's the shell of the NPC essentially.
The functions under the comment I wrote to iterate through arrays and select a random key. More on this in a second.

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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

	-- FUNCTIONS ---
	local function length(t)
		local count = 0
		for key in pairs(t) do 
			count = count + 1 
		end
		return count
	end
		
	local function selectText(list, size)
		local selection = math.ceil(math.random(1, size))
		return list[selection]
	end
--end---

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

I'm using a lot of multiple responses amongst a lot of topics so I have a lot of these:

Lua:
	local wep = {
	"You want to test out my steel.",
	"The best blade is made by one who'se wielded them.",
	"A bloody trolloc can't crack my armor."
	}

So to make the NPC talk:

Lua:
selfSay("text", cid)

This will make the NPC talk in default, just remove the cid parameter:
Lua:
selfSay("text")

Checking if whether to trigger a response:

Lua:
if msgcontains(msg, "sometext") then
selfSay("text", cid)
end

Using those functions to select random text:

Lua:
if msgcontains(msg, "sometext") then
selfSay(selectText(wep, length(wep)), cid)
end

if you just use an if-else block then the npc will instantly say what's in the else section after meeting the condition instead of waiting for msg to get a new value, so we use storages.

Lua:
if msgcontains(msg, 'quest') then
		selfSay('Huh? Quest? Just a simple post office here. You looking for work?', cid)
		setPlayerStorageValue(cid, 8000, 2)
end

if msgcontains(msg, 'blah') and getPlayerStorageValue(cid, 8000) == 2 then
end

Here's a full script example:
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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

	-- FUNCTIONS ---
	local function length(t)
		local count = 0
		for key in pairs(t) do 
			count = count + 1 
		end
		return count
	end
		
	local function selectText(list, size)
		local selection = math.ceil(math.random(1, size))
		return list[selection]
	end

local greet = {
"Welcome to the Tar Valon Post."
}
npcHandler:setMessage(MESSAGE_GREET, selectText(greet, length(greet)))

function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
	
	news = {
        "test a",
        "test b",
        "test c"
	}
	
		----KEYWORDS----
	if msgcontains(msg, "news") then
		selfSay(news[1], cid)
	end
	
	---QUEST---
	
	
	
	if msgcontains(msg, 'quest') then
			selfSay('Huh? Quest? Just a simple post office here. You looking for work?', cid)
			setPlayerStorageValue(cid, 8000, 2)
	end
	
	if msgcontains(msg, 'test') and getPlayerStorageValue(cid, 8000) == 2 then
		selfSay("This was a storagevalue response example!", cid)	
	end

	return true
end

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