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

Solved Problem with NPC

Phemus

Member
Joined
Jun 16, 2012
Messages
150
Solutions
2
Reaction score
13
Hello!
This was my first attempt at scripting a NPC script, so i have no idea on how it works :p
My problem is this:


The problem is that the NPC does not appears in the server. I hope that one can understand it :p
Thank You
 
Last edited:
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)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
 
	if (msgcontains(msg, 'new mana rune')) then
		selfSay('It is called the Great Mana Rune. It would heal your mana a 15% more than the Sterong Mana Rune. I could make one for you if only I had the {items} to make one.', cid)
		talkState[talkUser] = 1
	elseif(msgcontains(msg, 'items')) then
		if (getPlayerStorageValue(cid,19732) >= 1) then
			selfSay('Wait... I already gave you one', cid)
		else
			selfSay('There is an Orc Tower to the east of Forula City. Inside that tower, there is a teleport that teleports one to a place where is a chest that have an ancient rune. I need it to create the Great Mana Rune. Would you go and bring it to me?', cid)
		end
	elseif(msgcontains(msg, 'yes')) then
		selfSay('Great! Now go to the teleport of the room to the right. It would take you to the Tower that I told you. Then come here and bring me that rune. Good luck!', cid)
		setPlayerStorageValue(cid, 19730, 1)
	elseif (msgcontains(msg, 'ancient rune')) and (getPlayerStorageValue(cid, 19732) >= 1) then
		selfSay('Do you have it?', cid)
		talkState[talkUser] = 2
	elseif (msgcontains(msg, 'yes')) and (getPlayerItemCount(cid,2348) >= 1) then
		selfSay('Thank You! Here it is, the Grat Mana Rune with 50 free charges.', cid)
		if doPlayerRemoveItem(cid,2348,1) == TRUE then
			doPlayerAddItem(cid,2307,1)
			setPlayerStorageValue(cid, 19733, 51) 
		else
			selfSay('Where is it?', cid)
		end
	elseif msgcontains(msg, 'buy great mana rune') and (getPlayerStorageValue(cid, 19732) >= 1) then
		selfSay('Did you lose your Great Mana Rune? It would cost you 2500000 gold coins. Do you want to buy it?', cid)
	elseif msgcontains(msg, 'yes') and (getPlayerMoney(cid) >= 250000) then
		if doPlayerRemoveMoney(cid, 250000) then
			doPlayerAddItem(cid,2307,1)
		else
			selfSay('Sorry, you don\'t have the money.', cid)
		end
	end
        return true
end
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
This should solve the error, you were ending the creatureSayCallback function to early so a part of the script was outside the function.
There are more things wrong, but to correct them, I need to know how you want it to work exactly.
For example you should check for talkstate in the msgcontains lines after the first one, because else someone can say it before first saying the other things and also with the yes it will do the first one it finds.
Example:
LUA:
if(msgcontains(msg, 'yes') and talkState[talkUser] == 1) then
Now it will only respond if the talkstate is 1, and this is how to set the talkstate to 1, what you already have.
LUA:
talkState[talkUser] = 1
 
Last edited:
Thank You! :) It works, but when i say yes when i try to buy the item it says something else.
Well... It should work like this....
If the player goes to the npc to do the quest for the mana rune the player should say this.
player: hi
npc: hi player i'm working on a new mana rune bla bla bla
player: new mana rune
npc: It is called the Great Mana Rune. It would heal your mana a 15% more than the Sterong Mana Rune.... items....
player: items
npc: There is an Orc Tower to the east of.... do you want to do the quest?
player: yes
--If the player already gave him the item, the npc would say, "Wait... I already gave you one". It would give him an storage value after the player hands him the item.--
npc: Great! Now go to the teleport of the room to the right... (it gives him an storage to enter to a door after the player says "yes".)

Then, the player would go to the quest and would find a chests that would give him another storage and the item that he needs. Later, the player would come back to the npc and say...

player: hi
npc: did you brought the item?
Player: yes
--the npc would remove the item and give to him his price.--

That's it. Later if the player loses his price he would be able to buy one from the npc but only if he has finished with the quest.

Player: hi
npc: Hi player, how can i help you now?
player: buy item
npc: it would cost you x are you sure you want to buy it?
player: yes

And that's all.
I hope it's understandable and sorry for my bad English :)
 
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
 
local storage = 26533
function creatureSayCallback(cid, type, msg)
	local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
	if(not npcHandler:isFocused(cid)) then
		if msg == 'hello' or msg == 'hi' then
			if(getPlayerStorageValue(cid, storage) == -1) then
				selfSay('I\'m working on a new mana rune. It is called the Great Mana Rune. It would heal your mana a 15% more than the Sterong Mana Rune. I could make one for you if only I had the {items} to make one.', cid)
				talkState[talkUser] = 1
			elseif(getPlayerStorageValue(cid, storage) == 1) then
				selfSay('Did you brought the item?', cid)
				talkState[talkUser] = 2
			else
				selfSay('How can I help you?', cid)
			end
			npcHandler:addFocus(cid)
		else
			return false
		end
	end
	if(msgcontains(msg, 'items') and talkState[talkUser] == 1) then
		selfSay('There is an Orc Tower to the east of Forula City. Inside that tower, there is a teleport that teleports one to a place where is a chest that have an ancient rune. I need it to create the Great Mana Rune. Would you go and bring it to me?', cid)
		talkState[talkUser] = 2
	elseif(msgcontains(msg, 'yes') and talkState[talkUser] == 2) then
		if(getPlayerStorageValue(cid, storage) == -1) then
			selfSay('Great! Now go to the teleport of the room to the right. It would take you to the Tower that I told you. Then come here and bring me that rune. Good luck!', cid)
			setPlayerStorageValue(cid, 19730, 1)
			setPlayerStorageValue(cid, storage, 1)
		elseif(getPlayerStorageValue(cid, storage) == 1) then
			if doPlayerRemoveItem(cid,2348,1) then
				selfSay('Thank You! Here it is, the Great Mana Rune with 50 free charges.', cid)
				doPlayerAddItem(cid,2307,1)
				setPlayerStorageValue(cid, 19733, 51) 
				setPlayerStorageValue(cid, storage, 2)
			else
				selfSay('Where is it?', cid)
			end
		else
			if doPlayerRemoveMoney(cid, 250000) then
				doPlayerAddItem(cid,2307,1)
				selfSay('Here you are.', cid)
			else
				selfSay('Sorry, you don\'t have the money.', cid)
			end
		end
		talkState[talkUser] = 0
	elseif msgcontains(msg, 'buy great mana rune') and (getPlayerStorageValue(cid, 19732) >= 1) then
		selfSay('Did you lose your Great Mana Rune? It would cost you 2500000 gold coins. Do you want to buy it?', cid)
		talkState[talkUser] = 2
	elseif (msgcontains(msg, 'bye')) then
		selfSay("Bye.", cid)
		npcHandler:releaseFocus(cid)
	end
	return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Good bye.")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Back
Top