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

Keru

Entrepreneur
Joined
Jul 24, 2008
Messages
1,777
Solutions
2
Reaction score
167
Location
USA
First NPC I've ever wrote, I'm not even good at LUA I'm just trying to learn stuff, and I would love to see how this will actually look by some one with more skill also i want to know how to make something like " storage X < y then" cuz all my attempt results on a failed experiment

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 Galadriel Port."
}
npcHandler:setMessage(MESSAGE_GREET, selectText(greet, length(greet)))
 
function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
 
	news = {
        "Aye Aye |PLAYERNAME|. You look quite soft, you have to gear up. I can give you a {list} of locations where you can get some experience from, and become an stronger player.",
        "I'll put some marks on your map. These are places where friends of mine, can help you further. just tell them you are {new here}, and they will know what to do.",
		"|PLAYERNAME|, you again?, do you need the {list} again?",
		"What? you need the marks again? here you go..."
	}
 
		----KEYWORDS----
	if msgcontains(msg, "help") and getPlayerStorageValue(cid, 10000) == -1 then
		selfSay(news[1], cid)
		elseif msgcontains(msg, "help") and getPlayerStorageValue(cid, 10000) == 1 then
		selfSay(news[3], cid)
	end

	
	if msgcontains(msg, "List") and getPlayerStorageValue(cid, 10000) == -1 then
		selfSay(news[2], cid)
		doAddMapMark(cid, {x = 809, y = 1413, z = 7}, MAPMARK_GREENNORTH, 'Castle')
		doAddMapMark(cid, {x = 829, y = 1447, z = 7}, MAPMARK_TEMPLE, 'Temple')
		doAddMapMark(cid, {x = 798, y = 1447, z = 7}, MAPMARK_GREENNORTH, 'Depot')
		setPlayerStorageValue(cid, 10000, 1)
			elseif msgcontains(msg, "list") and getPlayerStorageValue(cid, 10000) == 1 then
				selfSay(news[4], cid)
		doAddMapMark(cid, {x = 809, y = 1413, z = 7}, MAPMARK_GREENNORTH, 'Castle')
		doAddMapMark(cid, {x = 829, y = 1447, z = 7}, MAPMARK_TEMPLE, 'Temple')
		doAddMapMark(cid, {x = 798, y = 1447, z = 7}, MAPMARK_GREENNORTH, 'Depot')
	end
	 
	---QUEST---
 
 qst = {
        "Aye Aye |PLAYERNAME|. Quest you say? I need someone stronger, whenever you gain so might get here.",
        "Test.",
		"I can't believe what my eyes are seeing, it's |PLAYERNAME|. Quite long time now, you seem stronger now, perhaps you can help me. Are you interested in {dangerous adventures}? "
	}
 
	if msgcontains(msg, 'quest') and getPlayerStorageValue(cid, 10001) == -1 then
		selfSay(qst[1], cid)
		elseif msgcontains(msg, 'quest') and getPlayerStorageValue(cid, 10001) == 1 then
		elfSay(qst[3], cid)
	end
 
	if msgcontains(msg, 'test') and getPlayerStorageValue(cid, 100001) == 2 then
		selfSay(qst[2], cid)
	end
 
	return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
getPlayerStorageValue(cid, 100001) == 1
getPlayerStorageValue(cid, 100001) >= 1
getPlayerStorageValue(cid, 100001) > 1
getPlayerStorageValue(cid, 100001) <= 1
getPlayerStorageValue(cid, 100001) < 1

or what do you mean?
 
getPlayerStorageValue(cid, 100001) <= 1 i think is this one, but when ever i use it. it does not work and the npc sends an error.
I want to make it, If storage bigger than 1 send MSG X if lower than 1 then msg Y
 
if getPlayerStorageValue(cid, 100001) > 1 then
selfSay("msg", cid)
else
selfSay("othermsg", cid)
end
 
Back
Top