• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

NPC NPC only trades after quest/level/item, etc.

Fermantor

Active Member
Joined
Dec 16, 2009
Messages
209
Solutions
4
Reaction score
33
Location
Germany
I was looking for an Rashid like npc, that only trades after a special quest.
So as I found nothing, I looked around and made this script.

Tested with TFS 0.2.14 (9.6)

npc.xml
XML:
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Trader" script="data/npc/scripts/trade.lua" walkinterval="2000" floorchange="0" >
	<health now="150" max="150"/>
	<look type="144" head="38" body="38" legs="94" feet="38" addons="1" corpse="2212"/>
	<parameters>
		<parameter key="message_greet" value="Welcome, little adventurer. This is just an example text." />
		<parameter key="message_farewell" value="Good bye."/>
	</parameters>
</npc>
data/npc/scripts/trade.lua
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
local shopWindow = {}
local t = {
          [2195] = {price = 15, price2 = 10}, -- [ITEMID TO SELL] = {Buy cost (0 = not buyable), sell cost (0 = not sellable)}
          [2493] = {price = 25, price2 = 0},
          [2361] = {price = 30, price2 = 0},
          [8851] = {price = 20, price2 = 0},
          [8925] = {price = 30, price2 = 0},
          [2640] = {price = 50, price2 = 0},
          [2494] = {price = 100, price2 = 0},
          [9932] = {price = 50, price2 = 0},
          [2472] = {price = 70, price2 = 0},
          [8931] = {price = 100, price2 = 0}
          }
local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
	if  t[item] and not doPlayerRemoveMoney(cid, t[item].price*amount) then
		selfSay("You don't have enough money.", cid)
	else
		doPlayerAddItem(cid, item, amount)
		doPlayerSendTextMessage(cid, 20, "You have bought " .. amount .. "x " .. getItemName(item) .. " for " .. t[item].price*amount .. " gold.")
		--selfSay("Here your are!", cid)
	end
	return true
end
local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
	doPlayerRemoveItem(cid, item, amount)
	doPlayerAddMoney(cid, t[item].price2*amount)
	doPlayerSendTextMessage(cid, 20, "You have sold " .. amount .. "x " .. getItemName(item) .. " for " .. t[item].price*amount .. " gold.")
	--selfSay("Here your are!", cid)
	return true
end
if (msgcontains(msg, 'trade') or msgcontains(msg, 'TRADE'))then
	if getPlayerItemCount(cid, 2472) == 0 then -- You can add level limit, quest storage or anything you want here, to make player trade able
		selfSay("You need a magic plate armor to trade with me.",cid)
	else
		for var, ret in pairs(t) do
			table.insert(shopWindow, {id = var, subType = 0, buy = ret.price, sell = ret.price2, name = getItemName(var)})
		end
		openShopWindow(cid, shopWindow, onBuy, onSell) end
		return true
	end
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

I tested it, and found no errors. If you have problems, message me here. This is my first published script, so don't be too hard with me. ;)
 
Last edited:
how edit
Lua:
if getPlayerItemCount(cid, 2472) == 0 then -- You can add level limit, quest storage or anything you want here, to make player trade able
		selfSay("You need a magic plate armor to trade with me.",cid)
i need removable item, meybe function removeitem(cid, xxx) ?
Lua:
 0
is required level?


very very nice script ;*
 
Lua:
if (msgcontains(msg, 'trade') or msgcontains(msg, 'TRADE'))then
	if getPlayerItemCount(cid, 2472) == 0 then -- You can add level limit, quest storage or anything you want here, to make player trade able
		selfSay("You need a magic plate armor to trade with me.",cid)
	else
		doPlayerRemoveItem(cid, item, amount) --NEW
		for var, ret in pairs(t) do
			table.insert(shopWindow, {id = var, subType = 0, buy = ret.price, sell = ret.price2, name = getItemName(var)})
		end
		openShopWindow(cid, shopWindow, onBuy, onSell) end
		return true
	end
end
 
thanks in "item" get write id of item ?

Yes, that's right ;)
But if you wanna remove a bigger amount than 1 (eg. 50 gold coins), you need to change the
Lua:
if getPlayerItemCount(cid, 2472) == 0 then
into
Lua:
if getPlayerItemCount(cid, 2472) >= X then
where X = amount of the item. :)
 
Back
Top