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

Elexonic

Well-Known Member
Joined
Jun 18, 2008
Messages
1,920
Reaction score
59
Last Page
anyone know which one I put script is better?


What script is beter?

Code:
function customCallbackOnBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks, shopWindow)
		local shopItem, npcHandler = nil, NpcHandler
		for _, item in ipairs(shopWindow) do
			if(item.id == itemid) then
				if item.subType then
					if(item.subType == subType) then
						shopItem = item
						break
					end
				else
					shopItem = item
					break
				end
			end
		end
 
		if(shopItem == nil) then
			error("[ShopModule.onBuy]", "Item not found on shopItems list")
			return false
		end
 
		if(shopItem.buy == -1) then
			error("[ShopModule.onSell]", "Attempt to purchase an item which is only sellable")
			return false
		end
 
		local backpack, totalCost = 1988, amount * shopItem.buy
		if(inBackpacks) then
			totalCost = isItemStackable(itemid) == TRUE and totalCost + 20 or totalCost + (math.max(1, math.floor(amount / getContainerCapById(backpack))) * 20)
		end
 
		if(getPlayerMoney(cid) < totalCost) then
			doPlayerSendCancel(cid, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_NEEDMONEY), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name}))
			return false
		end
 
		local subType = shopItem.subType or 1
		local a, b = doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
		if(a < amount) then
			local msgId = MESSAGE_NEEDMORESPACE
			if(a == 0) then
				msgId = MESSAGE_NEEDSPACE
			end
 
			doPlayerSendCancel(cid, npcHandler:parseMessage(npcHandler:getMessage(msgId), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name, [TAG_ITEMCOUNT] = a}))
 
			if(a > 0) then
				doPlayerRemoveMoney(cid, ((a * shopItem.buy) + (b * 20)))
				return true
			end
 
			return false
		end
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_BOUGHT), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name}))
		doPlayerRemoveMoney(cid, totalCost)
 
		return true
	end
 
	function customCallbackOnSell(cid, itemid, subType, amount, ignoreCap, inBackpacks)
		local shopItem, npcHandler = nil, NpcHandler
		for _, item in ipairs(self.npcHandler.shopItems) do
			if(item.id == itemid and item.subType == subType) then
				shopItem = item
				break
			end
		end
 
		if(shopItem == nil) then
			error("[ShopModule.onBuy]", "Item not found on shopItems list")
			return false
		end
 
		if(shopItem.sell == -1) then
			error("[ShopModule.onSell]", "Attempt to sell an item which is only buyable")
			return false
		end
 
		local parseInfo = {
			[TAG_PLAYERNAME] = getPlayerName(cid),
			[TAG_ITEMCOUNT] = amount,
			[TAG_TOTALCOST] = amount * shopItem.sell,
			[TAG_ITEMNAME] = shopItem.name
		}
 
		if(subType < 1) then
			subType = -1
		end
 
		if(doPlayerRemoveItem(cid, itemid, amount, subType) == TRUE) then
			doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, self.npcHandler:parseMessage(self.npcHandler:getMessage(MESSAGE_SOLD), parseInfo))
			doPlayerAddMoney(cid, amount * shopItem.sell)
			if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
				self.npcHandler.talkStart[cid] = os.time()
			else
				self.npcHandler.talkStart = os.time()
			end
 
			return true
		end
		doPlayerSendCancel(cid, self.npcHandler:parseMessage(self.npcHandler:getMessage(MESSAGE_NEEDITEM), parseInfo))
		if(NPCHANDLER_CONVBEHAVIOR ~= CONVERSATION_DEFAULT) then
			self.npcHandler.talkStart[cid] = os.time()
		else
			self.npcHandler.talkStart = os.time()
		end
 
		return false
	end


Or

Code:
function customCallbackOnBuy(cid, itemid, subType, amount, ignoreCap, inBackpacks, shopWindow)
	local shopItem, npcHandler = nil, NpcHandler
	for _, item in ipairs(shopWindow) do
		if not item.subType then
			item.subType = (isItemFluidContainer(item.id) == TRUE or isItemStackable(item.id) == TRUE) and 0 or 1
		end
		if(item.id == itemid and (item.subType == subType)) then
			shopItem = item
			break
		end
	end
 
	if(shopItem == nil) then
		error("[ShopModule.onBuy]", "Item not found on shopItems list")
		return false
	end
 
	if(shopItem.buy < 0) then
		error("[ShopModule.onSell]", "Attempt to purchase an item which is only sellable")
		return false
	end
 
	local backpack, totalCost = 1988, amount * shopItem.buy
	if(inBackpacks) then
		totalCost = isItemStackable(itemid) == TRUE and totalCost + 20 or totalCost + (math.max(1, math.floor(amount / getContainerCapById(backpack))) * 20)
	end
 
	if(getPlayerMoney(cid) < totalCost) then
		doPlayerSendCancel(cid, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_NEEDMONEY), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name}))
		return false
	end
 
	local subType = shopItem.subType or isItemFluidContainer(itemid) == TRUE and 0 or 1
	local a, b = doNpcSellItem(cid, itemid, amount, subType, ignoreCap, inBackpacks, backpack)
	if(a < amount) then
		local msgId = MESSAGE_NEEDMORESPACE
		if(a == 0) then
			msgId = MESSAGE_NEEDSPACE
		end
 
		doPlayerSendCancel(cid, npcHandler:parseMessage(npcHandler:getMessage(msgId), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name, [TAG_ITEMCOUNT] = a}))
 
		if(a > 0) then
			doPlayerRemoveMoney(cid, ((a * shopItem.buy) + (b * 20)))
			return true
		end
 
		return false
	end
	doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_BOUGHT), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = totalCost, [TAG_ITEMNAME] = shopItem.name}))
	doPlayerRemoveMoney(cid, totalCost)
 
	return true
end
 
function customCallbackOnSell(cid, itemid, subType, amount, ignoreCap, inBackpacks, shopWindow)
	local shopItem, npcHandler, subType = nil, NpcHandler, subType or 0
	for _, item in ipairs(shopWindow) do
		item.subType = not item.subType and 0 or item.subType
		if(item.id == itemid and (isItemFluidContainer(itemid) == FALSE or isItemFluidContainer(itemid) == TRUE and item.subType == subType)) then
			shopItem = item
			break
		end
	end
 
	if(shopItem == nil) then
		error("[ShopModule.onBuy]", "Item not found on shopItems list")
		return false
	end
 
	if(shopItem.sell < 0) then
		error("[ShopModule.onSell]", "Attempt to sell an item which is only buyable")
		return false
	end
 
	if(doPlayerRemoveItem(cid, itemid, amount, isItemFluidContainer(itemid) == TRUE and subType or isItemStackable(itemid) == TRUE and amount or 1) == TRUE) then
		doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_SOLD), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = amount * shopItem.sell, [TAG_ITEMNAME] = shopItem.name}))
		doPlayerAddMoney(cid, amount * shopItem.sell)
 
		return true
	end
	doPlayerSendCancel(cid, npcHandler:parseMessage(npcHandler:getMessage(MESSAGE_NEEDITEM), {[TAG_PLAYERNAME] = getPlayerName(cid), [TAG_ITEMCOUNT] = amount, [TAG_TOTALCOST] = amount * shopItem.sell, [TAG_ITEMNAME] = shopItem.name}))
 
	return false
end
 
Last edited:
Looks that you have an NPC that sells x items if you dont made a quest and y items if you made, right?
I will give you my npc script! works fine to me, have fun and rep++!

Lua:
local keywordHandler = KeywordHandler:new() 
local npcHandler = NpcHandler:new(keywordHandler) 
NpcSystem.parseParameters(npcHandler) 
local talkState = {} 
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
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 greetCallback(cid)
talkState[talkUser] = 0
return true
end

function creatureSayCallback(cid, type, msg) 
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

	        if(not npcHandler:isFocused(cid)) then  
	                return false  
	        end 
local trade = 	{ 
                  	{id=2386, buy=20, sell=8, name="axe"}, 
                  	{id=2382, buy=15, sell=4, name="club"},
                  	{id=2450, buy=18, sell=6, name="bone sword"}, 
                  	{id=2388, buy=160, sell=32, name="hatchet"}, 
                  	{id=2448, buy=145, sell=26, name="studded club"}, 
                  	{id=2395, buy=155, sell=30, name="carlin sword"}, 
                  	{id=2428, buy=525, sell=110, name="orcish axe"}, 
                  	{id=2449, buy=490, sell=95, name="bone club"}, 
                  	{id=2419, buy=510, sell=105, name="scimitar"}, 
                  	{id=8601, buy=1280, sell=360, name="steel axe"}, 
                  	{id=2417, buy=1190, sell=290, name="battle hammer"}, 
                  	{id=8602, buy=1240, sell=310, name="jagged sword"}, 
                  	{id=2378, buy=2300, sell=580, name="battle axe"}, 
                  	{id=7452, buy=2120, sell=520, name="spiked squelcher"}, 
                  	{id=2413, buy=2240, sell=550, name="broadsword"}, 
                  }

local trade2 = 	{ 
                  	{id=2386, buy=20, sell=8, name="axe"}, 
                  	{id=2382, buy=15, sell=4, name="club"},
                  	{id=2450, buy=18, sell=6, name="bone sword"}, 
                  	{id=2388, buy=160, sell=32, name="hatchet"}, 
                  	{id=2448, buy=145, sell=26, name="studded club"}, 
                  	{id=2395, buy=155, sell=30, name="carlin sword"}, 
                  	{id=2428, buy=525, sell=110, name="orcish axe"}, 
                  	{id=2449, buy=490, sell=95, name="bone club"}, 
                  	{id=2419, buy=510, sell=105, name="scimitar"}, 
                  	{id=8601, buy=1280, sell=360, name="steel axe"}, 
                  	{id=2417, buy=1190, sell=290, name="battle hammer"}, 
                  	{id=8602, buy=1240, sell=310, name="jagged sword"}, 
                  	{id=2378, buy=2300, sell=580, name="battle axe"}, 
                  	{id=7452, buy=2120, sell=520, name="spiked squelcher"}, 
                  	{id=2413, buy=2240, sell=550, name="broadsword"}, 
                  	{id=5880, sell=80, name="iron ore"}, 
                  }

local items = {}  
    for _, item in ipairs(trade) do 
    items[item.id] = {storage = item.storage, item_id = item.id, buyPrice = item.buy, sellPrice = item.sell, subType = 0, realName = item.name} 
end 

local items2 = {}  
    for _, item in ipairs(trade2) do 
    items2[item.id] = {storage = item.storage, item_id = item.id, buyPrice = item.buy, sellPrice = item.sell, subType = 0, realName = item.name} 
end 

	local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)  
	    if items[item].buyPrice ~= 0 then  
	        doPlayerRemoveMoney(cid, amount * items[item].buyPrice)  
	        for i = 1, amount do 
	            doPlayerAddItem(cid, items[item].item_id, amount)  
	        end 
	    end 
	end 
	    
	local onBuy2 = function(cid, item, subType, amount, ignoreCap, inBackpacks)  
	    if items2[item].buyPrice ~= 0 then  
	        doPlayerRemoveMoney(cid, amount * items2[item].buyPrice)  
	        for i = 1, amount do 
	            doPlayerAddItem(cid, items2[item].item_id, amount)  
	        end 
	    end 
	end 

	local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)  
		if items2[item].sellPrice ~= 0 then  
			doPlayerAddMoney(cid, items[item].sellPrice * amount)  
			doPlayerRemoveItem(cid, items[item].item_id, amount)  
		end 
	end 

	local onSell2 = function(cid, item, subType, amount, ignoreCap, inBackpacks)  
		if items2[item].sellPrice ~= 0 then  
			doPlayerAddMoney(cid, items2[item].sellPrice * amount)  
			doPlayerRemoveItem(cid, items2[item].item_id, amount)  
		end 
	end


    if msgcontains(msg, 'trade') and (getPlayerStorageValue(cid, 21000) < 3) then  
            openShopWindow(cid, trade, onBuy, onSell) 
	 	selfSay("O que deseja?", cid)  
		return true
    end

    if msgcontains(msg, 'trade') and (getPlayerStorageValue(cid, 21000) == 3) then  
        	openShopWindow(cid, trade2, onBuy2, onSell2) 
	 	selfSay("O que deseja?", cid) 
		return true
    end
return 1
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback) 
npcHandler:addModule(FocusModule:new())

I dont use the greetings on LUA, so do it if you want. I just modify in xml.

If you want to post this skill in another forum, put my name on it! and remember to REP++

Tested on tfs 0.3.6pl2

Also note that the only item I modified from trade1 to trade2 is the Iron ore.
 
I'll prove it tonight.
Anyway if anyone has the same npc as me but it works well if you can post.
regards
 
Bump!!

Have problem in this line

customCallbackOnSell(cid, itemid, subType, amount, ignoreCap, inBackpacks, items)
 
Lol, I tried adding it to my server, BAM, crashed the server.
Didn't see any errors in the console, so I can't tell what the problem was.
 
Back
Top