• 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 Need help with a NPC script

Crunch

Member
Joined
Jun 21, 2011
Messages
353
Reaction score
19
Location
England
Hello,

I was wondering if some people could help me. There is a lot of addon NPC's released on the forums, and I want to make seperate NPC's for each set of addons. I have looked at some released NPC's and have altered the script so it only does 1 outfit. For my first one I have justed used Varkhal.

I have put this in the Varkhal XML document

Code:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Varkhal" script="data/npc/scripts/addon.lua" access="5" lookdir="2" autowalk="25">
  <mana now="800" max="800" />
  <health now="200" max="200" />
  <look type="134" head="78" body="88" legs="0" feet="88" addons="3" />
  <parameters>
    <parameter key="message_greet" value="Greetings |PLAYERNAME|. Will you help me? If you do, I'll reward you with nice addons! Just say 'addons' or 'help' if you don't know what to do." />
    <parameter key="module_keywords" value="18" />
    <parameter key="keyword_reply3" value="The addons I'm currently offering for the Citizen outfit are 'feather hat' and 'backpack' for males and females." />
</npc>

I have then created a new LUA document in NPC > Scripts, and have put this

Code:
-- OTServ event handling functions end

function creatureSayCallback(cid, type, msg)
	-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
	if(npcHandler.focus ~= cid) then
		return false
	end

		addon_need_premium = 'Sorry, you need a premium account to get addons.'
		addon_have_already = 'Sorry, you already have this addon.'
		addon_have_not_items = 'Sorry, you don\'t have these items.'
		addon_give = 'Here you are.'
		player_gold = getPlayerItemCount(cid,2148)
		player_plat = getPlayerItemCount(cid,2152)*100
		player_crys = getPlayerItemCount(cid,2160)*10000
		player_money = player_gold + player_plat + player_crys
		
		if msgcontains(msg, 'addons') then
			selfSay('I can give you Citizen addons.')
		elseif msgcontains(msg, 'help') then
			selfSay('To buy the first addon say \'first NAME addon\', for the second addon say \'second NAME addon\'.')
------------------------------------------------ addon ------------------------------------------------
		elseif msgcontains(msg, 'first citizen addon') then
			if isPremium(cid) then
				if getPlayerItemCount(cid,5878) >= 100 then
					selfSay('Did you bring me 100 minotaur leathers?')
					talk_state = 1
				else
					selfSay('I need 100 minotaur leather, to give you the first citizen addon. Come back when you have them.')
					talk_state = 0
				end
			else
				selfSay(addon_need_premium)
				talk_state = 0
			end
------------------------------------------------ confirm yes ------------------------------------------------
		elseif msgcontains(msg, 'yes') and talk_state == 1 then
			talk_state = 0
			if getPlayerItemCount(cid,5878) >= 100 then
				addon = getPlayerStorageValue(cid,10001)
				if addon == -1 then
					if doPlayerTakeItem(cid,5878,100) == 0 then
						selfSay(addon_give)
						doPlayerAddOutfit(cid, 128, 1)
						doPlayerAddOutfit(cid, 136, 1)
						setPlayerStorageValue(cid,10001,1)
					end
				else
					selfSay(addon_have_already)
				end
			else
				selfSay(addon_have_not_items)
			end
------------------------------------------------ addon ------------------------------------------------
		elseif msgcontains(msg, 'second citizen addon') then
			if isPremium(cid) then
				if getPlayerItemCount(cid,5890) >= 100 and getPlayerItemCount(cid,5902) >= 50 and getPlayerItemCount(cid,2480) >= 1 then
					selfSay('Did you bring me 100 chicken feathers, 50 honeycombs and the legion helmet?')
					talk_state = 2
				else
					selfSay('I need 100 chicken feathers, 50 honeycombs and a legion helmet, to give you the second citizen addon. Come back when you have them.')
					talk_state = 0
				end
			else
				selfSay(addon_need_premium)
				talk_state = 0
			end
------------------------------------------------ confirm yes ------------------------------------------------
		elseif msgcontains(msg, 'yes') and talk_state == 2 then
			talk_state = 0
			if getPlayerItemCount(cid,5890) >= 100 and getPlayerItemCount(cid,5902) >= 50 and getPlayerItemCount(cid,2480) >= 1 then
				addon = getPlayerStorageValue(cid,10002)
				if addon == -1 then
					if doPlayerTakeItem(cid,5890,100) == 0 and doPlayerTakeItem(cid,5902,50) == 0 and doPlayerTakeItem(cid,2480,1) == 0 then
						selfSay(addon_give)
						doPlayerAddOutfit(cid, 128, 2)
						doPlayerAddOutfit(cid, 136, 2)
						setPlayerStorageValue(cid,10002,1)
					end
				else
					selfSay(addon_have_already)
				end
			else
				selfSay(addon_have_not_items)
			end
------------------------------------------------ confirm no ------------------------------------------------
		elseif msgcontains(msg, 'no') and (talk_state >= 1 and talk_state <= 34) then
			selfSay('Ok than.')
			talk_state = 0
		end
	-- Place all your code in here. Remember that hi, bye and all that stuff is already handled by the npcsystem, so you do not have to take care of that yourself.
	return true
end

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

I got it so the NPC would say "I can offer you feather hat or backpack", but when I tried to say first addon to lead onto me giving the items for it, he said nothing. But now when I try to import it to map editor it just says "Invalid format?"

If anyone could help I'd very much appreciate it, and also any explainations helping me to understand would be wonderful, once I understand one, I will be able to in theory do some more NPC's without so many problems!

Crunch~
 
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Varkhal" script="addon.lua">
	<look type="134" head="78" body="88" legs="0" feet="88" addons="3"/>
	<parameters>
		<parameter key="message_greet" value="Greetings |PLAYERNAME|. Will you help me? If you do, I'll reward you with nice addons! Just say 'addons' or 'help' if you don't know what to do."/>
	</parameters>
</npc>
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

local t = {}

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 greetCallback(cid)
	t[cid] = nil
	return true
end

function creatureSayCallback(cid, type, msg)
	if not npcHandler:isFocused(cid) then
		return false
	end

	local n
	if msgcontains(msg, 'addons') then
		npcHandler:say('I can give you Citizen addons.', cid)
	elseif msgcontains(msg, 'first') then
		npcHandler:say('Did you bring me 100 minotaur leathers?', cid)
		n = 1
	elseif msgcontains(msg, 'second') then
		npcHandler:say('Did you bring me 100 chicken feathers, 50 honeycombs and a legion helmet?', cid)
		n = 2
	elseif t[cid] == 1 and msgcontains(msg, 'yes') then
		if not canPlayerWearOutfitId(cid, 1, 1) then
			if doPlayerRemoveItem(cid, 5878, 100) then
				npcHandler:say('Here you are.', cid)
				doPlayerAddOutfitId(cid, 1, 1)
			else
				npcHandler:say('Sorry, you don\'t have these items.', cid)
			end
		else
			npcHandler:say('Sorry, you already have this addon.', cid)
		end
	elseif t[cid] == 2 and msgcontains(msg, 'yes') then
		if not canPlayerWearOutfitId(cid, 1, 2) then
			if getPlayerItemCount(cid, 5890) >= 100 and getPlayerItemCount(cid, 5902) >= 50 and doPlayerRemoveItem(cid, 2480, 1) then
				doPlayerRemoveItem(cid, 5890, 100)
				doPlayerRemoveItem(cid, 5902, 50)
				doPlayerAddOutfitId(cid, 1, 2)
				npcHandler:say('Here you are.', cid)
			else
				npcHandler:say('Sorry, you don\'t have these items.', cid)
			end
		else
			npcHandler:say('Sorry, you already have this addon.', cid)
		end
	end
	t[cid] = n
	return true
end

npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Hey Cyko, it got as far as "do you have blah blah items", for both the first and second addon but he wouldnt take them and give me the addons. One step closer anyway =] ty
 
using an older server?
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
 
local t = {}
 
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 greetCallback(cid)
	t[cid] = nil
	return true
end
 
function creatureSayCallback(cid, type, msg)
	if not npcHandler:isFocused(cid) then
		return false
	end
 
	local n
	if msgcontains(msg, 'addons') then
		npcHandler:say('I can give you Citizen addons.', cid)
	elseif msgcontains(msg, 'first') then
		npcHandler:say('Did you bring me 100 minotaur leathers?', cid)
		n = 1
	elseif msgcontains(msg, 'second') then
		npcHandler:say('Did you bring me 100 chicken feathers, 50 honeycombs and a legion helmet?', cid)
		n = 2
	elseif t[cid] == 1 and msgcontains(msg, 'yes') then
		if canPlayerWearOutfit(cid, 128, 1) == FALSE then
			if doPlayerRemoveItem(cid, 5878, 100) == TRUE then
				npcHandler:say('Here you are.', cid)
				doPlayerAddOutfit(cid, 128, 1)
				doPlayerAddOutfit(cid, 136, 1)
			else
				npcHandler:say('Sorry, you don\'t have these items.', cid)
			end
		else
			npcHandler:say('Sorry, you already have this addon.', cid)
		end
	elseif t[cid] == 2 and msgcontains(msg, 'yes') then
		if canPlayerWearOutfit(cid, 128, 2) == FALSE then
			if getPlayerItemCount(cid, 5890) >= 100 and getPlayerItemCount(cid, 5902) >= 50 and doPlayerRemoveItem(cid, 2480, 1) == TRUE then
				doPlayerRemoveItem(cid, 5890, 100)
				doPlayerRemoveItem(cid, 5902, 50)
				doPlayerAddOutfit(cid, 128, 2)
				doPlayerAddOutfit(cid, 136, 2)
				npcHandler:say('Here you are.', cid)
			else
				npcHandler:say('Sorry, you don\'t have these items.', cid)
			end
		else
			npcHandler:say('Sorry, you already have this addon.', cid)
		end
	end
	t[cid] = n
	return true
end
 
npcHandler:setCallback(CALLBACK_GREET, greetCallback)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Back
Top