• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Guard NPC

Himii

Premium User
Premium User
Joined
Jan 19, 2011
Messages
1,268
Solutions
5
Reaction score
188
Location
Sweden
Can anyone make an npc which kill monsters and the corpse is empty if its killed by the guard.
And alsow kill players with blackskull.

Rep+ if you help!
 
I'm looking for something like this too :P I'll try to make one myself and post back here if I succeed xD

- - - Updated - - -

LUA:
local target = 0
local prevTarget = 0
local maxChaseDistance = 20
local origPos = 0
local lastAttack = 0
local followTimeout = 10
 
local function goToOrigPos()
	target = 0
	lastAttack  = 0
	selfFollow(0)
	doTeleportThing(getNpcCid(), origPos)	
end
 
local function updateTarget()
	if(target == 0) then
		local list = getSpectators(getNpcPos(), 9, 9, false)
		for i = 1, table.getn(list) do
			local _target = list[i]
			if(_target ~= 0) then
				local monster = {"Wolf", "Snake", "Troll", "Bug"}
				if(isInArray(monster, getCreatureName(_target))) then
					if(not getTilePzInfo(getCreaturePosition(_target))) then
						if(selfFollow(_target)) then
							target = _target
							if(target ~= prevTarget) then
								selfSay("Everyone get to safety! I'll take care of this "..getCreatureName(_target).."!")
							end
 
							prevTarget = target
							break
						end
					end
				end
			end
		end
	end
end
 
function onCreatureAppear(cid)
	if(cid == getNpcCid()) then
		origPos = getNpcPos()
	end
end
 
function onCreatureDisappear(cid)
	if(cid == target) then
		goToOrigPos()
	end
end
 
function onCreatureMove(creature, oldPos, newPos)
	--
end
 
function onThink()
	updateTarget()
 
	if(target == 0) then
		return
	end
 
	local playerPos = getCreaturePosition(target)
	local myPos = getNpcPos()
 
	if(myPos.z ~= playerPos.z) then
		goToOrigPos()
		return
	end
 
	if(math.abs(myPos.x - origPos.x) > maxChaseDistance or math.abs(myPos.y - origPos.y) > maxChaseDistance) then
		selfSay("I'll get it next time...")
		goToOrigPos()
		return
	end
 
	if(lastAttack == 0) then
		lastAttack = os.clock()
	end
 
	if(os.clock() - lastAttack > followTimeout) then
		selfSay("I'll get you next time.")
		goToOrigPos()
		return
	end
 
	if((math.abs(playerPos.x - myPos.x) <= 1) and (math.abs(playerPos.y - myPos.y) <= 1)) then
		doTargetCombatHealth(getNpcCid(), target, COMBAT_LIFEDRAIN, -5, -25, CONST_ME_BLOCKHIT)
		lastAttack = os.clock()
	end
end
This works but I don't know how to make it walk back to the original position instead of teleporting :/
 
Last edited:
I tried it but it didn't work :/

- - - Updated - - -

I tried using "doSteerCreature(getNpcCid(), origPos)" instead of "doTeleportThing(getNpcCid(), origPos)" but it didn't do anything :/
 
Thank you :D It works but it's kinda bugged haha if a monster is near and it's like 9 sqm away it'll sit there walking back and fourth saying "23:59 Guard: I'll get it next time..." haha

- - - Updated - - -

I tried adding "if(math.abs(myPos.x - origPos.x) < maxChaseDistance or math.abs(myPos.y - origPos.y) < maxChaseDistance) then" like:
LUA:
local target = 0
local prevTarget = 0
local maxChaseDistance = 7
local origPos = 0
local lastAttack = 0
local followTimeout = 10
 
local function goToOrigPos()
	target = 0
	lastAttack  = 0
	selfFollow(0)
	doSteerCreature(getNpcCid(), origPos)	
end
 
local function updateTarget()
	if(target == 0) then
		local myPos = getNpcPos()
		if(math.abs(myPos.x - origPos.x) < maxChaseDistance or math.abs(myPos.y - origPos.y) < maxChaseDistance) then
			local list = getSpectators(getNpcPos(), 5, 5, false)
			for i = 1, table.getn(list) do
				local _target = list[i]
				if(_target ~= 0) then
					local monster = {"Wolf", "Snake", "Troll", "Bug"}
					if(isInArray(monster, getCreatureName(_target))) then
						if(not getTilePzInfo(getCreaturePosition(_target))) then
							if(selfFollow(_target)) then
								target = _target
								if(target ~= prevTarget) then
									selfSay("Everyone get to safety! I'll take care of this "..getCreatureName(_target).."!")
								end
 
								prevTarget = target
								break
							end
						end
					end
				end
			end
		end
	end
end
 
function onCreatureAppear(cid)
	if(cid == getNpcCid()) then
		origPos = getNpcPos()
	end
end
 
function onCreatureDisappear(cid)
	if(cid == target) then
		goToOrigPos()
	end
end
 
function onCreatureMove(creature, oldPos, newPos)
	--
end
 
function onThink()
	updateTarget()
 
	if(target == 0) then
		return
	end
 
	local playerPos = getCreaturePosition(target)
	local myPos = getNpcPos()
 
	if(myPos.z ~= playerPos.z) then
		goToOrigPos()
		return
	end
 
	if(math.abs(myPos.x - origPos.x) > maxChaseDistance or math.abs(myPos.y - origPos.y) > maxChaseDistance) then
		selfSay("I'll get it next time...")
		goToOrigPos()
		return
	end
 
	if(lastAttack == 0) then
		lastAttack = os.clock()
	end
 
	if(os.clock() - lastAttack > followTimeout) then
		selfSay("I'll get you next time.")
		goToOrigPos()
		return
	end
 
	if((math.abs(playerPos.x - myPos.x) <= 1) and (math.abs(playerPos.y - myPos.y) <= 1)) then
		doTargetCombatHealth(getNpcCid(), target, COMBAT_LIFEDRAIN, -5, -25, CONST_ME_BLOCKHIT)
		lastAttack = os.clock()
	end
end

But.. it didn't work ahah
 
test with getNpcPos() -> origPos in getSpectators funtion in updateTarget
local list = getSpectators(origPos, 5, 5, false)

Do not forget to do a new condition "if" for an empty "list"
 
Thank you very much for helping andypsylon! Here is the finished product:

LUA:
local target = 0
local prevTarget = 0
local maxChaseDistance = 7
local origPos = 0
local lastAttack = 0
local followTimeout = 10
 
local function goToOrigPos()
	target = 0
	lastAttack  = 0
	selfFollow(0)
	doSteerCreature(getNpcCid(), origPos)	
end
 
local function updateTarget()
	if(target == 0) then
		local list = getSpectators(origPos, 7, 7, false)
		for i = 1, table.getn(list) do
			local _target = list[i]
			if(_target ~= 0) then
				local monster = {"Wolf", "Snake", "Troll", "Bug"}
				if(isInArray(monster, getCreatureName(_target))) then
					if(not getTilePzInfo(getCreaturePosition(_target))) then
						if(selfFollow(_target)) then
							target = _target
							if(target ~= prevTarget) then
								selfSay("Everyone get to safety! I'll take care of this "..getCreatureName(_target).."!")
							end
 
							prevTarget = target
							break
						end
					end
				end
			end
		end
	end
end
 
function onCreatureAppear(cid)
	if(cid == getNpcCid()) then
		origPos = getNpcPos()
	end
end
 
function onCreatureDisappear(cid)
	if(cid == target) then
		goToOrigPos()
	end
end
 
function onCreatureMove(creature, oldPos, newPos)
	--
end
 
function onThink()
	updateTarget()
 
	if(target == 0) then
		return
	end
 
	local playerPos = getCreaturePosition(target)
	local myPos = getNpcPos()
 
	if(myPos.z ~= playerPos.z) then
		goToOrigPos()
		return
	end
 
	if(math.abs(myPos.x - origPos.x) > maxChaseDistance or math.abs(myPos.y - origPos.y) > maxChaseDistance) then
		selfSay("I'll get it next time...")
		goToOrigPos()
		return
	end
 
	if(lastAttack == 0) then
		lastAttack = os.clock()
	end
 
	if(os.clock() - lastAttack > followTimeout) then
		selfSay("I'll get you next time.")
		goToOrigPos()
		return
	end
 
	if((math.abs(playerPos.x - myPos.x) <= 1) and (math.abs(playerPos.y - myPos.y) <= 1)) then
		doTargetCombatHealth(getNpcCid(), target, COMBAT_LIFEDRAIN, -5, -25, CONST_ME_BLOCKHIT)
		lastAttack = os.clock()
	end
end
 
Back
Top