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

Zolax

New Member
Joined
Feb 16, 2011
Messages
97
Reaction score
1
Hello I have a problem. When i sell items in npc he gives me money but they arent together. How i can solve it? Here is example of my npc:
Code:
_state = 0
_count = 0
_index = 0
_delay = 500

items = {}
--runes
items[0] = {name = 'white pearl', id = 2143, subtype = -1, sell = 0, buy = 160}
items[1] = {name = 'black pearl', id = 2144, subtype = -1, sell = 0, buy = 280}
items[2] = {name = 'small diamond', id = 2145, subtype = -1, sell = 0, buy = 300}
items[3] = {name = 'small sapphire', id = 2146, subtype = -1, sell = 0, buy = 250}
items[4] = {name = 'small emerald', id = 2149, subtype = -1, sell = 0, buy = 250}
items[5] = {name = 'small ruby', id = 2147, subtype = -1, sell = 0, buy = 250}
items[6] = {name = 'small amethyst', id = 2150, subtype = -1, sell = 0, buy = 200}
items[7] = {name = 'wedding ring', id = 2121, subtype = -1, sell = 0, buy = 100}
items[8] = {name = 'scarab coin', id = 2159, subtype = -1, sell = 0, buy = 100}






local function onActionItem(action)
	if ((action == 'buy' and items[_index].sell == -1) or
		(action == 'sell' and items[_index].buy == -1)) then
		return 
	end
	
	amount = ''
	suffix = ''
	plural = 'a'
	
	if (_count > 1) then
		amount = ' ' .. tostring(_count)
		suffix = 's'
		plural = ''
	end
	
	cost = items[_index].buy
	if (action == 'buy') then
		cost = items[_index].sell*_count
	else if(action =='sell') then
		cost = items[_index].buy*_count
	end
	end
	
	selfSay('Do you want to ' .. action .. ' ' .. plural .. amount .. ' ' .. items[_index].name .. suffix .. ' for ' .. cost .. ' gold?')
end

function getNext()
	nextPlayer = getQueuedPlayer()
	if (nextPlayer ~= nil) then
		if (getDistanceToCreature(nextPlayer) <= 4) then
			updateNpcIdle()
			setNpcFocus(nextPlayer)
			selfSay('What do you want, ' .. getCreatureName(nextPlayer) .. ' I\'m a busy man.', _delay * 3)

			return
		else
			getNext()
		end
	end
	
	setNpcFocus(0)
	resetNpcIdle()
end

function _selfSay(message)
	selfSay(message, _delay)
	updateNpcIdle()
end

--
function onCreatureAppear(creature)
return 1
end

function onCreatureDisappear(cid)
	if (getNpcFocus() == cid) then
		selfSay('Good bye ' ..getCreatureName(cid).. '.', _delay)
		getNext()
	else
		unqueuePlayer(cid)
	end
	
end

function onCreatureMove(cid, oldPos, newPos)
	if (getNpcFocus() == cid) then
		faceCreature(cid)
		
		if (oldPos.z ~= newPos.z or getDistanceToCreature(cid) > 4) then
				selfSay('Good bye ' ..getCreatureName(cid).. '.', _delay)
			getNext()
		end
	else
		if (oldPos.z ~= newPos.z or getDistanceToCreature(cid) > 4) then
			unqueuePlayer(cid)
		end
	end
	return 1
end

function onCreatureSay(cid, type, msg)
	if (getNpcFocus() == 0) then
		if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
			updateNpcIdle()
			setNpcFocus(cid)
			selfSay('What do you want, ' .. getCreatureName(cid) .. ' I\'m a busy man.', _delay)
		end
	elseif (getNpcFocus() ~= cid) then
		if ((msgcontains(msg, 'hi') or msgcontains(msg, 'hello')) and getDistanceToCreature(cid) <= 4) then
			selfSay('Just wait, ' .. getCreatureName(cid) .. '.', _delay)
			queuePlayer(cid)
		end
		
		elseif (msgcontains(msg, 'offer')) then
			_selfSay('Give me all pearls, and I\'ll pay you for it')
			_state = 1
	else
		if (msgcontains(msg, 'bye')) then
			selfSay('Good bye ' ..getCreatureName(cid).. '.', _delay)
			getNext()		
		elseif (_state == 1) then
			if (msgcontains(msg, 'yes')) then
				if(doPlayerRemoveMoney(cid, items[_index].sell*_count)) then
					for i = 1, _count do
						if items[_index].subtype < 1 then
					costam = 1
					else
					costam = items[_index].subtype
					end
					doPlayerAddItem(cid, items[_index].id, costam)
					end
				
					selfSay('Here you are.', _delay)
				else
					selfSay('Come back, when you have enough money.', _delay)
					
				end
				
				updateNpcIdle()
			else
				selfSay('Hmm, but next time.', _delay)
			end
			
			_state = 0
			
		elseif (_state == 2) then
            if (msgcontains(msg, 'yes')) then
                if (doPlayerRemoveItem(cid, items[_index].id, _count)) then
                    local mon = items[_index].buy *_count
                     doPlayerAddMoney(cid, mon)
                    selfSay('Ok. Here is your money.')
                else
                    if (_count > 1) then
                        selfSay('Sorry, you do not have so many.', _delay)
                    else
                        selfSay('Sorry, you do not have one.', _delay)
                    end
                end
                
                updateNpcIdle()
            else
                selfSay('Maybe next time.', _delay)
            end
		
			_state = 0
			
		else
			for n = 0, table.getn(items) do
				if (msgcontains(msg, items[n].name) or msgcontains(msg, items[n].name .. "s")) then
					_count = getCount(msg)
					_index = n
					
					if (msgcontains(msg, 'sell')) then
						onActionItem('sell')
						_state = 2
					else
						onActionItem('sell')
						_state = 2
					end
					
					updateNpcIdle()
					break
				end
			end
			
		end
		
	end
	return 1
end

function onThink()
	if (getNpcFocus() ~= 0) then
		if (isNpcIdle()) then
			selfSay('Good bye.', _delay)
			getNext()
		end
	end
	return 1
end
 
Back
Top