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

Solved NPC random Bug

Aleada

Unknown Member
Joined
Mar 14, 2013
Messages
231
Reaction score
7
When I say (even when I'm off the NPC's screen) it'll say Hi back... Lol.. I only want the NPC to respond if you're within about 4-5 sqm of the NPC

Here's the script of the NPC:
LUA:
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)) then
			doNpcSetCreatureFocus(v)
			return
		end
	end
	doNpcSetCreatureFocus(0)
end

local itemWindow = {
	{id=2389, subType=0, sell=7, buy=30, name="Spear"},
	{id=2384, subType=0, sell=23, buy=95, name="Rapier"},
	{id=2385, subType=0, sell=27, buy=110, name="Sabre"},
	{id=2379, subType=0, sell=5, buy=20, name="Dagger"},
	{id=2405, subType=0, sell=2, buy=10, name="Sickle"},
	{id=2380, subType=0, sell=5, buy=20, name="Hand Axe"},
	{id=2386, subType=0, sell=33, buy=135, name="Axe"},
	{id=2406, subType=0, sell=30, buy=120, name="Short Sword"},
	{id=2650, subType=0, sell=5, buy=20, name="Jacket"},
	{id=2651, subType=0, sell=5, buy=20, name="Coat"},
	{id=2485, subType=0, sell=8, buy=35, name="Doublet"},
	{id=2467, subType=0, sell=2, buy=80, name="Leather Armor"},
	{id=2649, subType=0, sell=8, buy=35, name="Leather Legs"},
	{id=2461, subType=0, sell=3, buy=15, name="Leather Helmet"},
	{id=2482, subType=0, sell=10, buy=40, name="Studded Helmet"},
	{id=2458, subType=0, sell=20, buy=85, name="Chain Helmet"},	
	{id=2512, subType=0, sell=6, buy=25, name="Wooden Shield"},
	{id=2526, subType=0, sell=40, buy=0, name="Studded Shield"},
	{id=2398, subType=0, sell=40, buy=0, name="Mace"},	
	{id=2464, subType=0, sell=50, buy=0, name="Chain Armor"},
	{id=2511, subType=0, sell=46, buy=0, name="Brass Shield"},
	{id=2510, subType=0, sell=50, buy=0, name="Plate Shield"},
	{id=2530, subType=0, sell=60, buy=0, name="Copper Shield"},
	{id=2643, subType=0, sell=5, buy=20, name="Leather Boots"}
}

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 = doNpcSellItem(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)) 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)) 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(not isCreature(focus)) 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

If anyone could help, I'd greatly appreciate it! :D

- - - Updated - - -

Nvm, I solved it just took a little time to test and fail :p

Here it is if anyone wants it btw..

LUA:
local focuses = {}

local function getNpcDistanceToCreature(id)
	if(not id or id == 0) then
		selfIdle()
		return nil
	end

	local c = getCreaturePosition(id)
	if(not c.x or c.x == 0) then
		return nil
	end

	local s = getCreaturePosition(getNpcId())
	if(not s.x or s.x == 0 or s.z ~= c.z) then
		return nil
	end

	return math.max(math.abs(s.x - c.x), math.abs(s.y - c.y))
end


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
		if getNpcDistanceToCreature(cid) < 4 then
			table.insert(focuses, cid)
		end
	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)) then
			doNpcSetCreatureFocus(v)
			return
		end
	end
	doNpcSetCreatureFocus(0)
end
 
local itemWindow = {
	{id=2389, subType=0, sell=7, buy=30, name="Spear"},
	{id=2384, subType=0, sell=23, buy=95, name="Rapier"},
	{id=2385, subType=0, sell=27, buy=110, name="Sabre"},
	{id=2379, subType=0, sell=5, buy=20, name="Dagger"},
	{id=2405, subType=0, sell=2, buy=10, name="Sickle"},
	{id=2380, subType=0, sell=5, buy=20, name="Hand Axe"},
	{id=2386, subType=0, sell=33, buy=135, name="Axe"},
	{id=2406, subType=0, sell=30, buy=120, name="Short Sword"},
	{id=2650, subType=0, sell=5, buy=20, name="Jacket"},
	{id=2651, subType=0, sell=5, buy=20, name="Coat"},
	{id=2485, subType=0, sell=8, buy=35, name="Doublet"},
	{id=2467, subType=0, sell=2, buy=80, name="Leather Armor"},
	{id=2649, subType=0, sell=8, buy=35, name="Leather Legs"},
	{id=2461, subType=0, sell=3, buy=15, name="Leather Helmet"},
	{id=2482, subType=0, sell=10, buy=40, name="Studded Helmet"},
	{id=2458, subType=0, sell=20, buy=85, name="Chain Helmet"},	
	{id=2512, subType=0, sell=6, buy=25, name="Wooden Shield"},
	{id=2526, subType=0, sell=40, buy=0, name="Studded Shield"},
	{id=2398, subType=0, sell=40, buy=0, name="Mace"},	
	{id=2464, subType=0, sell=50, buy=0, name="Chain Armor"},
	{id=2511, subType=0, sell=46, buy=0, name="Brass Shield"},
	{id=2510, subType=0, sell=50, buy=0, name="Plate Shield"},
	{id=2530, subType=0, sell=60, buy=0, name="Copper Shield"},
	{id=2643, subType=0, sell=5, buy=20, name="Leather Boots"}
}
 
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 = doNpcSellItem(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)) 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)) then --Be sure he's online
			closeShopWindow(cid)
		end
	end
end
 
function onCreatureSay(cid, type, msg)
	if((msg == "hi") and not (isFocused(cid))) then
		addFocus(cid)
		if isFocused(cid) then
			selfSay("Welcome, ".. getCreatureName(cid) ..".", cid, true)
			selfSay("Do you want to see my {wares}?", cid)
		end
	end
	if((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(not isCreature(focus)) 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