Fermantor
Active Member
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
data/npc/scripts/trade.lua
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.
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>
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: