• 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 Multiple doRemovePlayerItem

Gall

Member
Joined
Nov 8, 2007
Messages
78
Reaction score
10
Hey, I am still playing a bit with my OT and now I am creating Smith npc. He basically does same thing as Uzgod in Rashid quest - takes some ore and money and gives a weapon.
So here's a script, pretty obviously, based on A Sweaty Cyclops from some rl map distro.
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 done = 'Here you are.'
local miss = 'I\'m afraid, but you don\'t have required items.'

function creatureSayCallback(cid, type, msg)
	if(not npcHandler:isFocused(cid)) then
		return false
	end
	if msgcontains(msg, 'offer') then
		npcHandler:say('I can smelt metals for you and craft fine weapons', cid)
	elseif msgcontains(msg, 'weapons') then
		npcHandler:say('I can craft nearly every weapon', cid)
	elseif msgcontains(msg, 'Blacksteel Sword') then
		if getPlayerItemCount(cid, 5880) >= 55 and getPlayerItemCount(cid, 5944) >=2 and getPlayerItemCount(cid, 2144) >= 12 and getPlayerMoney(cid) >= 1500 then
		npcHandler:say('Did you bring me 55 oz. of iron ore, 2 soul orbs, 12 black pearls and 1500 gp?', cid)
		talk_state = 1
		else
			npcHandler:say('I need 55 oz. of iron, 2 soul orbs, 12 black pearls. Fee is 1,500 gp', cid)
		talk_state = 0
		end
	elseif msgcontains(msg, 'yes') and talk_state == 1 then
		talk_state = 0
			if doPlayerRemoveItem(cid, 5880, 55) and doPlayerRemoveItem(cid, 5944, 2) and doPlayerRemoveItem(cid, 2144, 12) and getPlayerMoney(cid) >= 1500 then
				npcHandler:say(done, cid)
				doPlayerBuyItem(cid, 7406, 1, 1500)
			
			else
			npcHandler:say(miss, cid)
			end
	elseif msgcontains(msg, 'no') and talk_state >= 1 then
	npcHandler:say('Okay then.')
	talk_state = 0
	end
	
	return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Thing is that part:
Lua:
if doPlayerRemoveItem(cid, 5880, 55) and doPlayerRemoveItem(cid, 5944, 2) and doPlayerRemoveItem(cid, 2144, 12) and getPlayerMoney(cid) >= 1500 then
can eat items if player does not have second item. I was thinking about using something like this code...
[lua
if doPlayerRemoveItem(cid, xx, x) then
if doPlayerRemove... then
if do...[/code]
...but its looks quite unoptimized and messy. So could anyone of you guide me on similar script for npc or hint how to do it in less messy way?
 
Last edited:
Gall,

try
PHP:
	-- Items Remove {ItemID/Count}
	local itemsRemove = { {5880,55}, {5944,2}, {2144,12} }
	for i=1, #itemsRemove do
		doPlayerRemoveItem(cid, itemsRemove[i][1], itemsRemove[i][2])
	end
 
Thanks for hinting me to the loops.
However problem is still unsolved. If player drops item beetwen saying blacksteel sword and yes, npc will destroy part of items and then stop. Also For loop does not return anything, so I tried to use incremantation of "a" however after finishing loop it does set as 0, same thing does happen when I use a = i. Thanks to that I cannot check when it does stop and give back destroyed items.

I could use check after saying yes, but then I would have to insert it twice(which is resources waste), or resign from rl-like npc reactions to bringing certain items. I find second option better but still I would like to have those different reactions.

@edit
That was stupid. I had code like
Lua:
local a = 0
		local itemsRemove = { {5880,55}, {5944,2}, {2144,12} } 
		for i=1, #itemsRemove do 
			doPlayerRemoveItem(cid, itemsRemove[i][1], itemsRemove[i][2]) 
			local a = a + 1
			end
And I had not notice that inside for was "local a", which redefined it each time. After removing this stupidity i can get value.
 
Last edited:
Back
Top Bottom