• 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 NPC System problem

Rudixx

New Member
Joined
Aug 3, 2007
Messages
246
Reaction score
0
Location
England
Code:
local focuses = {}
local function isFocused(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			return true
		end
	end
	return false
end

local function addFocus(cid)
	if(not isFocused(cid)) then
		table.insert(focuses, cid)
	end
end
local function removeFocus(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			table.remove(focuses, i)
			break
		end
	end
end
local function lookAtFocus()
	for i, v in pairs(focuses) do
		if(isPlayer(v) == TRUE) then
			doNpcSetCreatureFocus(v)
			return
		end
	end
	doNpcSetCreatureFocus(0)
end

local itemWindow = {
	{id=2465, subType=0, buy=0, sell=150, name="brass armor"},
	{id=2460, subType=0, buy=0, sell=22, name="brass helmet"},
	{id=2511, subType=0, buy=0, sell=25, name="brass shield"},
	{id=2464, subType=0, buy=0, sell=40, name="chain armor"},
	{id=2458, subType=0, buy=52, sell=12, name="chain helmet"},
	{id=2651, subType=0, buy=8, sell=0, name="coat"},
	{id=2530, subType=0, buy=0, sell=50, name="copper shield"},
	{id=2485, subType=0, buy=16, sell=3, name="doublet"},
	{id=2650, subType=0, buy=10, sell=0, name="jacket"},
	{id=2467, subType=0, buy=25, sell=5, name="leather armor"},
	{id=2643, subType=0, buy=0, sell=2, name="leather boots"},
	{id=2461, subType=0, buy=12, sell=3, name="leather helmet"},
	{id=2649, subType=0, buy=10, sell=2, name="leather legs"},
	{id=2480, subType=0, buy=0, sell=22, name="legion helmet"},
	{id=2510, subType=0, buy=0, sell=40, name="plate shield"},
	{id=2484, subType=0, buy=0, sell=10, name="studded armor"},
	{id=2482, subType=0, buy=63, sell=20, name="studded helmet"},
	{id=2468, subType=0, buy=0, sell=15, name="studded legs"},
	{id=2526, subType=0, buy=50, sell=16, name="studded shield"},
	{id=2473, subType=0, buy=0, sell=25, name="viking helmet"},
	{id=2512, subType=0, buy=15, sell=3, name="wooden shield"}
}

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

local function getPlayerMoney(cid)
	return ((getPlayerItemCount(cid, 2160) * 10000) +
	(getPlayerItemCount(cid, 2152) * 100) +
	getPlayerItemCount(cid, 2148))
end

local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
	if(items[item] == nil) then
		selfSay("Ehm.. sorry... this shouldn\'t be there, I\'m not selling it.", cid)
		return
	end

	if(getPlayerMoney(cid) >= amount * items[item].buyPrice) then
		local itemz, i = doPlayerAddItem(cid, item, amount, subType, ignoreCap, inBackpacks)
		if(i < amount) then
			if(i == 0) then
				doPlayerSendCancel(cid, "There is not enough room.")
			else
				selfSay("I\'ve sold some for you, but it seems you can\'t carry more than this. I won\'t take more money than necessary.", cid)
				doPlayerRemoveMoney(cid, i * items[item].buyPrice)
			end
		else

And I've got error:
Code:
Lua Script Error: [Npc interface]
(Unknown scriptfile)

luaDoPlayerAddItem(). Player not found

Lua Script Error: [Npc interface]
(Unkown scriptfile)

data\npc\scripts\Lol.lua:77: attempt to compare nil with number stack traceback:
data\npc\scripts\Lol.lua:77: in function (data\npc\scripts\Lol.lua:69>

Server: TFS3 beta 1
 
Last edited:
Isn't it simple enough? I posted script, posted error message, and I want solution how to fix the problem. There is trade window when I am clicking to buy item nothing happens and error on console appear.
 
Line 77:
Code:
		if(i < amount) then
Btw. the script you have posted is not full. Last line is just "else" without an end and something which will be if "if" can't be done (in human language).
 
Here you are ;] I've jsut wrote it, should work well ;]

PHP:
local focuses = {}
local function isFocused(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			return true
		end
	end
	return false
end

local function addFocus(cid)
	if(not isFocused(cid)) then
		table.insert(focuses, cid)
	end
end
local function removeFocus(cid)
	for i, v in pairs(focuses) do
		if(v == cid) then
			table.remove(focuses, i)
			break
		end
	end
end
local function lookAtFocus()
	for i, v in pairs(focuses) do
		if(isPlayer(v) == TRUE) then
			doNpcSetCreatureFocus(v)
			return
		end
	end
	doNpcSetCreatureFocus(0)
end

local itemWindow = {
	{id=2465, subType=0, buy=0, sell=150, name="brass armor"},
	{id=2460, subType=0, buy=0, sell=22, name="brass helmet"},
	{id=2511, subType=0, buy=0, sell=25, name="brass shield"},
	{id=2464, subType=0, buy=0, sell=40, name="chain armor"},
	{id=2458, subType=0, buy=52, sell=12, name="chain helmet"},
	{id=2651, subType=0, buy=8, sell=0, name="coat"},
	{id=2530, subType=0, buy=0, sell=50, name="copper shield"},
	{id=2485, subType=0, buy=16, sell=3, name="doublet"},
	{id=2650, subType=0, buy=10, sell=0, name="jacket"},
	{id=2467, subType=0, buy=25, sell=5, name="leather armor"},
	{id=2643, subType=0, buy=0, sell=2, name="leather boots"},
	{id=2461, subType=0, buy=12, sell=3, name="leather helmet"},
	{id=2649, subType=0, buy=10, sell=2, name="leather legs"},
	{id=2480, subType=0, buy=0, sell=22, name="legion helmet"},
	{id=2510, subType=0, buy=0, sell=40, name="plate shield"},
	{id=2484, subType=0, buy=0, sell=10, name="studded armor"},
	{id=2482, subType=0, buy=63, sell=20, name="studded helmet"},
	{id=2468, subType=0, buy=0, sell=15, name="studded legs"},
	{id=2526, subType=0, buy=50, sell=16, name="studded shield"},
	{id=2473, subType=0, buy=0, sell=25, name="viking helmet"},
	{id=2512, subType=0, buy=15, sell=3, name="wooden shield"}

}

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

local function getPlayerMoney(cid)
	return ((getPlayerItemCount(cid, 2160) * 10000) +
	(getPlayerItemCount(cid, 2152) * 100) +
	getPlayerItemCount(cid, 2148))
end

local onBuy = function(cid, item, subType, amount, ignoreCap, inBackpacks)
	if(items[item] == nil) then
		selfSay("Ehm.. sorry... this shouldn't be there, I'm not selling it.", cid)
		return
	end

	if(getPlayerMoney(cid) >= amount * items[item].buyPrice) then
		local itemz, i = doPlayerAddItem(cid, item, amount, subType, ignoreCap, inBackpacks)
		if(i < amount) then
			if(i == 0) then
				selfSay("Sorry, but you don't have space to take it.", cid)
			else
				selfSay("I've sold some for you, but it seems you can't carry more than this. I won't take more money than necessary.", cid)
				doPlayerRemoveMoney(cid, i * items[item].buyPrice)
			end
		else
			selfSay("Thanks for the money!", cid)
			doPlayerRemoveMoney(cid, amount * items[item].buyPrice)
		end
	else
		selfSay("Stfu noob, you don't have money.", cid)
	end
end

local onSell = function(cid, item, subType, amount, ignoreCap, inBackpacks)
	if(items[item] == nil) then
		selfSay("Ehm.. sorry... this shouldn't be there, I'm not buying it.", cid)
	end

	if(subType < 1) then
		subType = -1
	end
	if(doPlayerRemoveItem(cid, item, amount, subType) == TRUE) then
		doPlayerAddMoney(cid, items[item].sellPrice * amount)
		selfSay("Here you are.", cid)
	else
		selfSay("No item, no deal.", cid)
	end
end

function onCreatureAppear(cid)
end

function onCreatureDisappear(cid)
	if(isFocused(cid)) then
		selfSay("Hmph!")
		removeFocus(cid)
		if(isPlayer(cid) == TRUE) then --Be sure he's online
			closeShopWindow(cid)
		end
	end
end

function onCreatureSay(cid, type, msg)
	if((msg == "hi") and not (isFocused(cid))) then
		selfSay("Welcome, ".. getCreatureName(cid) ..".", cid, TRUE)
		selfSay("Do you want to see my {wares}?", cid)
		addFocus(cid)
	elseif((isFocused(cid)) and (msg == "wares" or msg == "trade")) then
		selfSay("Pretty nice, right?", cid)
		openShopWindow(cid, itemWindow, onBuy, onSell)
	elseif((isFocused(cid)) and (msg == "bye" or msg == "goodbye" or msg == "cya")) then
		selfSay("Goodbye!", cid, TRUE)
		closeShopWindow(cid)
		removeFocus(cid)
	end
end

function onPlayerCloseChannel(cid)
	if(isFocused(cid)) then
		selfSay("Hmph!")
		closeShopWindow(cid)
		removeFocus(cid)
	end
end

function onPlayerEndTrade(cid)
	selfSay("It was a pleasure doing business with you.", cid)
end

function onThink()
	for i, focus in pairs(focuses) do
		if(isCreature(focus) == FALSE) then
			removeFocus(focus)
		else
			local distance = getDistanceTo(focus) or -1
			if((distance > 4) or (distance == -1)) then
				selfSay("Hmph!")
				closeShopWindow(focus)
				removeFocus(focus)
			end
		end
	end
	lookAtFocus()
end
 
Back
Top