• 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 Problem with loops

Core_

Well-Known Member
Joined
Jul 9, 2010
Messages
1,557
Solutions
1
Reaction score
50
Well, I'm making this Shop NPC, but I'm having some problems.

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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid)			end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid)		end

local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end

local items = {
["item1"] = {price = 5,  cash = 500},
["item2"] = {price = 10, cash = 1000}
}

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, 'hi') or msgcontains(msg, 'hello')) then
			selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
		selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
		talkState = 2
		elseif(msgcontains(msg,getCount(msg))) and talkState == 2 then
		talkState = 2
		selfSay('Do you really wan\'t to buy '..getCount(msg)..' '..i..'', cid)
		end
	end
	
	
	return true
end


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


When I say "Item1"
The NPC says "So do you want to buy item1's... how many?"
Then I say 50, and here is the bug the NPC answers:
"Do you really wan't to buy 50 item1
Do you really wan't to buy 50 item2"


Twice cause he gets "i" twice so I wan't he to just answer with the "i" I said first.

I hope I explained it well.
 
Maybe 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
[1234] = {item = "item1", price = 5,  cash = 500},
[1235] = {item = "item2", price = 10, cash = 1000}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
			selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
		selfSay('So do you want to buy '..v[1]..'\'s... how many? ', cid)
		talkState = 2
		elseif(msgcontains(msg,getCount(msg))) and talkState == 2 then
		talkState = 2
		selfSay('Do you really wan\'t to buy '..getCount(msg)..' '..v[1]..'', cid)
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new()
 
D: not working.

No console errors, nothing, I just say Hi, item1 or item2 and he doesn't even answer.
 
try 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["item1"] = {price = 5,  cash = 500},
["item2"] = {price = 10, cash = 1000}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
			selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
		selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
		talkState[talkUser] = 1
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
		talkState[talkUser] = (talkState[talkUser] + 1)
		selfSay('Do you really wan\'t to buy '..getCount(msg)..' '..i..'', cid)
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Maybe 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["item1"] = {price = 5,  cash = 500},
["item2"] = {price = 10, cash = 1000}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
			selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
		selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
		talkState[talkUser] = 1
		item_type = i
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
		talkState[talkUser] = (talkState[talkUser] + 1)
		selfSay('Do you really wan\'t to buy '..getCount(msg)..' '..item_type..'', cid)
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Maybe 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["item1"] = {price = 5,  cash = 500},
["item2"] = {price = 10, cash = 1000}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
			selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
		selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
		talkState[talkUser] = 1
		item_type = i
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
		talkState[talkUser] = (talkState[talkUser] + 1)
		selfSay('Do you really wan\'t to buy '..getCount(msg)..' '..item_type..'', cid)
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

:D it works thanks
 
D: Another little problem, now in the "selling" part, the npc takes the same price for every item :3

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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["poke ball"] = {Id = 2147, price = 5},
["great ball"] = {Id = 11437, price = 20}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
		selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
			selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
			talkState[talkUser] = 1
			item_type = i
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
			talkState[talkUser] = (talkState[talkUser] + 1)
			tPrice = v.price*100*getCount(msg)
			if getPlayerMoney(cid) >= v.price*100*getCount(msg) then
				doPlayerAddItem(cid,getItemIdByName(item_type),getCount(msg))
				doPlayerRemoveMoney(cid, v.price*100*getCount(msg))
				selfSay('Here are your '..getCount(msg)..' '..item_type..'s.', cid)
			elseif getPlayerMoney(cid) < v.price*100*getCount(msg) then
				selfSay('You don\'t have enought money.', cid)
			end
		
			
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
oki check 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["poke ball"] = {Id = 2147, price = 5},
["great ball"] = {Id = 11437, price = 20}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
		selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
			selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
			talkState[talkUser] = 1
			item_type = i
			item_price = v
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
			talkState[talkUser] = (talkState[talkUser] + 1)
			tPrice = item_price.price*100*getCount(msg)
			if getPlayerMoney(cid) >= item_price.price*100*getCount(msg) then
				doPlayerAddItem(cid,getItemIdByName(item_type),getCount(msg))
				doPlayerRemoveMoney(cid, item_price.price*100*getCount(msg))
				selfSay('Here are your '..getCount(msg)..' '..item_type..'s.', cid)
			elseif getPlayerMoney(cid) < item_price.price*100*getCount(msg) then
				selfSay('You don\'t have enought money.', cid)
			end
 
 
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
:3! OMGosh thanks! xD I'm learning too I guess :3

You must spread some Reputation around before giving it to SpiderOt again.
 
oki check 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 onPlayerEndTrade(cid)				npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid)			npcHandler:onPlayerCloseChannel(cid) end
 
local function getCount(s)
	local b, e = s:find('%d+')
	return b and e and math.min(4294967295, tonumber(s:sub(b, e))) or -1
end
 
local items = {
["poke ball"] = {Id = 2147, price = 5},
["great ball"] = {Id = 11437, price = 20}
}
 
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, 'hi') or msgcontains(msg, 'hello')) then
		selfSay('Hi, May I help you?', cid)
	elseif(msgcontains(msg, 'offer')) or msgcontains(msg, 'trade') then
		selfSay('I sell a lot of items.', cid)
	end
	for i, v in pairs(items) do
		if(msgcontains(msg, i)) then
			selfSay('So do you want to buy '..i..'\'s... how many? ', cid)
			talkState[talkUser] = 1
			item_type = i
			item_price = v
		elseif(msgcontains(msg,getCount(msg))) and talkState[talkUser] < 2 then
			talkState[talkUser] = (talkState[talkUser] + 1)
			tPrice = item_price.price*100*getCount(msg)
			if getPlayerMoney(cid) >= item_price.price*100*getCount(msg) then
				doPlayerAddItem(cid,getItemIdByName(item_type),getCount(msg))
				doPlayerRemoveMoney(cid, item_price.price*100*getCount(msg))
				selfSay('Here are your '..getCount(msg)..' '..item_type..'s.', cid)
			elseif getPlayerMoney(cid) < item_price.price*100*getCount(msg) then
				selfSay('You don\'t have enought money.', cid)
			end
 
 
		end
	end
 
 
	return true
end
 
 
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

nvm.
 
Back
Top