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

Requesting check if monster is in area

VJ2354

New Member
Joined
Jan 17, 2013
Messages
39
Reaction score
0
Hello,

I'm requesting a script for the following:-

If a tiger is in a certain area then a lever can be pulled and the player will be teleported. This is basically the concept of the Nomads Land Quest in Rl tibia. I've been searching all around and been experimenting on my own to see if i can find anything but alas the closest I've gotten is the following script :-

Code:
local monster = "Tiger" -- name of the monster
local storage = 2021 -- global storage value used to know that lever is available
local area = { -- frompos: upper left corner -- topos: right bottom corner
	[1] = {frompos={x = 33194, y = 32854, z = 9}, topos={x = 33197, y = 32855, z = 9}}--,
	-- can add more positions where this applies to our condition
	-- [2] = {frompos={x=1000,y=1000,z=7}, topos={x=1001,y=1001,z=7}},
	-- [3] = {frompos={x=1000,y=1000,z=7}, topos={x=1001,y=1001,z=7}}
}
 
function onKill(cid, target, lastHit)
	if isMonster(target) and string.lower(getCreatureName(target)) == string.lower(monster) then
		for i, pos in pairs(area) do
			if isInRange(getThingPos(target), pos.frompos, pos.topos) then
				setGlobalStorageValue(storage, 1) -- set global to 1
				addEvent(setGlobalStorageValue, 10000, storage, 0) -- reset in 60 secs
			end
		end
	end
 
	return true
end

this is a creature script I tie in to my lever by usee of the global storage. If globalstrage == 2021 then teleport player and what not..

The issue of course with this script is that it only works if the tiger is killed IN the room. I on the other hand want the tiger to be alive. I was also hoping I could have this all in one action script for the lever instead of a creature script because I imagine it's quite resource heavy to continuously reset. So if it's possible for there to be a check when the lever is being pulled that would be great. Any help is appreciated! Thank you.
 
Hrmm that script was a little too advanced for me to break down ahah

Is there no way a lever can be pulled only if a monster is in a certain area?
 
I will finish that later
Lua:
local storage = 2021 -- global storage value used to know that lever is available
local area = {x=111,y=222,z=333} -- tiger center position
 
function onUse(cid, item, fromPosition, itemEx, toPosition)

	if item.aid == storage then
		if item.id == 1945 then
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do
				if isMonster(pid) and getCreatureName(pid):lower == "tiger" then
					
					-- create wall
					
					doTransformItem(item.uid, 1946)
					addEvent(doTransformItem, 10*60*1000, item.uid, 1945) -- reset in 10 min
				end
			end
		end
		return true
	end

	return false
end
 
Code:
local storage = 2021 -- global storage value used to know that lever is available
local area = {x=33195,y=32855,z=9} -- tiger center position
 
function onUse(cid, item, fromPosition, itemEx, toPosition)

	if item.aid == storage then
		if item.id == 1945 then
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do
				if isMonster(pid) and getCreatureName(pid):lower == "tiger" then
				doCreateItem(1547,1,gatepos1)
                doCreateItem(1547,1,gatepos2)
                doCreateItem(1547,1,gatepos3)
				doTransformItem(item.uid, 1946)
				addEvent(doTransformItem, 10*60*1000, item.uid, 1945) -- reset in 10 min
				end
			end
		end
		return true
	end
 
	return false
end

So I tried to load the sccript but an error came up on the console stating that the script couldn't be loaded. "Function arguments expected near ==" on line 10 which is :-

Code:
if isMonster(pid) and getCreatureName(pid):lower == "tiger" then


Any suggestions?
 
Thank you! That fixed that error however now a new error has come up. "Attempt to index a nil value. Stack Traceback: [C]: in function 'getSpectators' "

This is the lines where ''getSpectators'' is mentioned:-

Code:
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do

Here is the whole script if it makes things easier :-
Code:
local storage = 2021 -- global storage value used to know that lever is available
local area = {x=33195,y=32855,z=9} -- tiger center position
 
function onUse(cid, item, fromPosition, itemEx, toPosition)

	if item.aid == storage then
		if item.id == 1945 then
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do
				if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
				doCreateItem(1547,1,gatepos1)
                doCreateItem(1547,1,gatepos2)
                doCreateItem(1547,1,gatepos3)
				doTransformItem(item.uid, 1946)
				addEvent(doTransformItem, 10*60*1000, item.uid, 1945) -- reset in 10 min
				end
			end
		end
		return true
	end
 
	return false
end
 
You have no defined var "pos", "gatepos1", "gatepos2" and "gatepos3".
Another problem is that your script(creating and transforming items) will be executed once for every tiger. Example: 4 tigers in the area = you will have 4 gates in the same place.
Try this:

Lua:
local area = {x=33195,y=32855,z=9} -- tiger center position
local gatepos1 = {x=33195,y=32855,z=9} --CHANGE IT!!
local gatepos2 = {x=33195,y=32855,z=9} --CHANGE IT!!
local gatepos3 = {x=33195,y=32855,z=9} --CHANGE IT!!
 
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if item.itemid == 1945 then
		local spectators = getSpectators(area, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
		local isTigerNear = false
		for _, pid in ipairs(spectators) do
			if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
				isTigerNear = true
			end
		end
		if isTigerNear then
			doCreateItem(1547,1,gatepos1)
			doCreateItem(1547,1,gatepos2)
			doCreateItem(1547,1,gatepos3)
			doTransformItem(item.uid, 1946)
			addEvent(doTransformItem, 10*60*1000, item.uid, 1945) -- reset in 10 min
			return true
		end
	end
	return false
end

In actions.xml
Code:
<action actionid="2021" event="script" value="script_name.lua"/>
 
almost ready :)
Lua:
local storage = 2021 -- global storage value used to know that lever is available
local pos = {x=111,y=222,z=333} -- tiger center position
local posWall1 = {x=111,y=222,z=333}
local posWall2 = {x=111,y=222,z=333}

function onUse(cid, item, fromPosition, itemEx, toPosition)

	if item.aid == storage then
		if item.id == 1945 then
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do
				if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
					doCreateItem(1547, 1, posWall1)
					doCreateItem(1547, 1, posWall2)
					doTransformItem(item.uid, 1946)
					addEvent(function()
						doRemoveItem(getTileItemById(posWall1, 1547).uid, 1)
						doRemoveItem(getTileItemById(posWall2, 1547).uid, 1)
						doTransformItem(item.uid, 1945)
					end, 10*60*1000) -- reset in 10 min
				end
			end
		end
		return true
	end

	return false
end
 
I just wanted to thank you all for your help. I'd been stuck for ages trying to figure it out and gave up for quite a while to be honest. I've repp'd everyone that helped. The script is working perfectly now, I managed to get everything I needed out of it.

- - - Updated - - -

Hello there again!! I've realized I need a little more help. The script is working perfectly however I can still use the lever when the Tiger is not in the room. The lever does nothing however it comes up with errors because its looking for a tiger when it isn't there, same thing when the lever is pulled again (back to 1945) its trying to remove walls that isn't there.

Here is my script so far, hopefully somebody can help make it so that the lever can only be pressed if the tiger is actually in the room, if it's not it will say "sorry not possible" or something like that :-

Code:
local area = {x=33196,y=32854,z=9} -- tiger center position
function onUse(cid, item, fromPosition, itemEx, toPosition)
gatepos1 = {x = 33196, y = 32856, z = 9, stackpos=1} --CHANGE IT!!
gatepos2 = {x = 33197, y = 32856, z = 9, stackpos=1} --CHANGE IT!!
gatepos3 = {x = 33198, y = 32856, z = 9, stackpos=2} --CHANGE IT!!
getgate1 = getThingfromPos(gatepos1)
getgate2 = getThingfromPos(gatepos2)
getgate3 = getThingfromPos(gatepos3)
	if item.itemid == 1945 and item.aid == 2021 then
		local spectators = getSpectators(area, 1, 1) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
		local isTigerNear = false
		for _, pid in ipairs(spectators) do
			if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
				isTigerNear = true
			end
		end
		if isTigerNear then
			doCreateItem(1547,1,gatepos1)
			doCreateItem(1547,1,gatepos2)
			doCreateItem(1547,1,gatepos3)
			doTransformItem(item.uid, 1946)
			doTeleportThing(cid,{x = 33196, y = 32862, z = 9})
		    doSendMagicEffect({x = 33196, y = 32862, z = 9}, 10)
			return true
		end
	end
	if item.itemid == 1946 and item.aid == 2021 then
	        doRemoveItem(getgate1.uid,1)
            doRemoveItem(getgate2.uid,1)
			doRemoveItem(getgate3.uid,1)
			doTransformItem(item.uid,item.itemid-1)
		    return true
	end	
end
 
Last edited:
so I chose this and no other solution:
Lua:
local storage = 2021 -- global storage value used to know that lever is available
local pos = {x=111,y=222,z=333} -- tiger center position
local posWall1 = {x=111,y=222,z=333}
local posWall2 = {x=111,y=222,z=333}

function onUse(cid, item, fromPosition, itemEx, toPosition)

	if item.aid == storage then
		if item.id == 1945 then
			local spectators = getSpectators(pos, 2, 2) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
			for _, pid in ipairs(spectators) do
				if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
					doCreateItem(1547, 1, posWall1)
					doCreateItem(1547, 1, posWall2)
					doTransformItem(item.uid, 1946)
					addEvent(function()
						doRemoveItem(getTileItemById(posWall1, 1547).uid, 1)
						doRemoveItem(getTileItemById(posWall2, 1547).uid, 1)
						doTransformItem(item.uid, 1945)
					end, 10*60*1000) -- reset in 10 min
				end
			end
		end
		return true
	end

	return false
end
 
Lua:
local area = {x=33196,y=32854,z=9} -- tiger center position
function onUse(cid, item, fromPosition, itemEx, toPosition)
    gatepos1 = {x = 33196, y = 32856, z = 9, stackpos=1} --CHANGE IT!!
    gatepos2 = {x = 33197, y = 32856, z = 9, stackpos=1} --CHANGE IT!!
    gatepos3 = {x = 33198, y = 32856, z = 9, stackpos=2} --CHANGE IT!!
    getgate1 = getThingfromPos(gatepos1)
    getgate2 = getThingfromPos(gatepos2)
    getgate3 = getThingfromPos(gatepos3)
    if item.itemid == 1945 and item.aid == 2021 then
        local spectators = getSpectators(area, 1, 1) -- 2 tile left, 2 right, 2 up, 2 down :) capiszi?
        local isTigerNear = false
        if spectators then
            for _, pid in ipairs(spectators) do
                if isMonster(pid) and getCreatureName(pid):lower() == "tiger" then
                    isTigerNear = true
                end
            end
            if isTigerNear then
                doCreateItem(1547,1,gatepos1)
                doCreateItem(1547,1,gatepos2)
                doCreateItem(1547,1,gatepos3)
                doTransformItem(item.uid, 1946)
                doTeleportThing(cid,{x = 33196, y = 32862, z = 9})
                doSendMagicEffect({x = 33196, y = 32862, z = 9}, 10)
                return true
            end
        end
    end
    if item.itemid == 1946 and item.aid == 2021 then
        if getgate1 and getgate1.uid > 0 and getgate1.itemid == 1547 then
            doRemoveItem(getgate1.uid,1)
        end
        if getgate2 and getgate2.uid > 0 and getgate1.itemid == 1547 then
            doRemoveItem(getgate2.uid,1)
        end
        if getgate3 and getgate3.uid > 0 and getgate1.itemid == 1547 then
            doRemoveItem(getgate3.uid,1)
        end
        doTransformItem(item.uid,item.itemid-1)
        return true
    end
    return false
end
Try this
 
Back
Top