• 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 isInArea not working (0.3.6)

filipus

Member
Joined
Dec 31, 2010
Messages
229
Reaction score
12
So I'm using the isInArea function. I don't know why but it doesn't seem to be working.

Here is my code:
Code:
local arena = {
frompos = {x=32389, y=32188, z=7},
topos = {x=32403, y=32201, z=7},
exit = {x=32404, y=32204, z=7}
}

function onPrepareDeath(cid, lastHitKiller, mostDamageKiller)
if isPlayer(cid) == TRUE then
if isInArea(getPlayerPosition(cid), arena.frompos, arena.topos) then
if doCreatureAddHealth(cid, getCreatureMaxHealth(cid) - getCreatureHealth(cid), TRUE) == TRUE then
if doTeleportThing(cid, arena.exit) == TRUE then
doSendMagicEffect(arena.exit, 10)
doPlayerSendTextMessage(mostDamageKiller,MESSAGE_STATUS_CONSOLE_BLUE,'[ARENA] You have defeated '..getPlayerName(cid)..'!')
doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE,'[ARENA] You are defeated by '..getPlayerName(mostDamageKiller)..'!')
return FALSE
end
end
end
end
return TRUE
end

For some reason the getPlayerPosition(cid) doesn't seem to be returning any value.
This is the error:

Code:
[05/04/2014 13:25:14] [Error - CreatureScript Interface]
[05/04/2014 13:25:14] data/creaturescripts/scripts/arena.lua:onPrepareDeath
[05/04/2014 13:25:14] Description:
[05/04/2014 13:25:14] data/lib/032-position.lua:2: attempt to index global 'position' (a nil value)
[05/04/2014 13:25:14] stack traceback:
[05/04/2014 13:25:14]  data/lib/032-position.lua:2: in function 'isInArea'
[05/04/2014 13:25:14]  data/creaturescripts/scripts/arena.lua:11: in function <data/creaturescripts/scripts/arena.lua:9>

Can anyone help?
Still trying to learn :)
 
Do you have getPlayerPosition = getCreaturePosition in 100-compat.lua?
You can also just try to replace getPlayerPosition with getCreaturePosition in the script.
 
if isPlayer(cid) == TRUE then
won't test as you expect.

It's probably blowing up on a creature kill.

1. In Lua the boolean values are true and false (small letters only) - NB I tested this outside OT, on Lua 5.1 running in "Lua for Windows"
2. The "if" looks for a boolean after all the conditions are resolved, so you should use:
if isPlayer(cid) then
3. The convention is that the method name template "isXxx()" will always and only return a boolean
(this is true for every coding language I know)
 
Do you have getPlayerPosition = getCreaturePosition in 100-compat.lua?
You can also just try to replace getPlayerPosition with getCreaturePosition in the script.

getCreaturePosition = getThingPosition
getPlayerPosition = getCreaturePosition

Yes I do. And I tried that, still didn't work.

if isPlayer(cid) == TRUE then
won't test as you expect.

It's probably blowing up on a creature kill.

1. In Lua the boolean values are true and false (small letters only)
2. The "if" looks for a boolean after all the conditions are resolved, so you should use:
if isPlayer(cid) then
3. The convention is that the method name template "isXxx()" will always and only return a boolean
(this is true for every coding language I know)


ooo, interesting. So... how do I get the player position if that function only returns true and false?
 
Can you post the function in 032-position.lua?

Btw the isPlayer(cid) check is not needed, if it's registered in login.lua, it will only work for players.
 
maybe i'm wrong .. but i found it in lib/position.lua
Code:
function isInRange(pos, fromPosition, toPosition)
    return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)
end

did u tried inRange ? or u have it names as inArea?
 
Actually according to my logic that code will never execute :s There must be something else happening.

Suggest you print a message to the screen or console to find out what's happening.

Try this right after the function is called:

doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "player Id: " .. cid )

That will only work if cid is a player, but I don't know a better way.
Maybe a broadcast if there is one? That would also give you an answer if it was a creature.



PS:
Where does this get "position" value from?
Code:
function isInRange(pos, fromPosition, toPosition)
        return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)
end

As written, unless there's a global called position somewhere, that will blow up at "position.x"

maybe this?
Code:
function isInRange(position, fromPosition, toPosition)
 
isInArea is a compat in TFS 0.3.6, it's set to isInRange in 100-compat.lua
It should be this, if the parameters are not the same (pos, position) it will be a nil value.
Code:
function isInRange(position, fromPosition, toPosition)
   return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)
end
 
Yes I tried the isInRange function, same error.

20tqa7o.png




If I take off the "if isPlayer(cid) then" it doesn't give any error but sets the position of the char to 0,0,0.
I have another script that uses onDeath, does that interfere?
 
How does the isInRange function look like in your 032-position.lua?
Make sure it's like this.

function isInRange(position, fromPosition, toPosition)
return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)​
end
 
Add
"doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "player position: " ..
getPlayerPosition(cid)
)"


after
"if isPlayer(cid) then"

This is probably just going to prove the error is in "isInRange()", so please post your code for that too.
(which is kinda what Limos is asking :)
 
WOW, WOW WOW.
This is what happens when you download servers like a noob.

Code:
function isInRange(pos, fromPosition, toPosition)
    return (position.x >= fromPosition.x and position.y >= fromPosition.y and position.z >= fromPosition.z and position.x <= toPosition.x and position.y <= toPosition.y and position.z <= toPosition.z)
end


CHanged it, it is working now.

Now I only have one problems, my arena script is not working haha.

[05/04/2014 14:35:29] [Error - LuaScriptInterface::loadFile] data/creaturescripts/scripts/arena.lua:12: unexpected symbol near '=='
[05/04/2014 14:35:29] [Warning - Event::loadScript] Cannot load script (data/creaturescripts/scripts/arena.lua)
[05/04/2014 14:35:29] data/creaturescripts/scripts/arena.lua:12: unexpected symbol near '=='


Code:
local arena = {
frompos = {x=32389, y=32188, z=7},
topos = {x=32403, y=32201, z=7},
exit = {x=32404, y=32204, z=7}
}

function onPrepareDeath(cid, lastHitKiller, mostDamageKiller)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "player Id: " .. cid )
if isInRange(getPlayerPosition(cid), arena.frompos, arena.topos) then
doCreatureAddHealth(cid, getCreatureMaxHealth(cid) - getCreatureHealth(cid), true) == true then
if doTeleportThing(cid, arena.exit) == true then
doSendMagicEffect(arena.exit, 10)
doPlayerSendTextMessage(mostDamageKiller,MESSAGE_STATUS_CONSOLE_BLUE,'[ARENA] You have defeated '..getPlayerName(cid)..'!')
doPlayerSendTextMessage(cid,MESSAGE_STATUS_CONSOLE_BLUE,'[ARENA] You are defeated by '..getPlayerName(mostDamageKiller)..'!')
return false
end
end
end
return true
end


So, I dont understand very well how the onPrepareDeath function works but most of those ifs seem kind of useless to me. Why does it return false and return true? Does that change anything?
 
doCreatureAddHealth(cid, getCreatureMaxHealth(cid) - getCreatureHealth(cid), true) == true

Missing "if" ?

The parser doesn't like "==" outside a comparison

PS.

Those tests after a normal function call will (should :) return "false" if the function executes ok but isn't able to do what it was asked to do (e.g. can't teleport because the target position is not a valid teleport target).

It provides some error reporting back to the script in the absence of something anything better (like exception handlers).

i.e. - where a condition isn't selecting for anything related to function, re-read assuming it's just error checking and it will sometimes make sense.
 
Last edited:
doCreatureAddHealth(cid, getCreatureMaxHealth(cid) - getCreatureHealth(cid), true) == true

Missing "if" ?

The parser doesn't like "==" outside a comparison

Im felling stupid...
Is that I deleted the ifs because it seemed weird to me and forgot to put them back haha.


Ok, this is my current script

Code:
local arena = {
frompos = {x=32389, y=32188, z=7},
topos = {x=32403, y=32201, z=7},
exit = {x=32404, y=32204, z=7}
}

function onPrepareDeath(cid, lastHitKiller, mostDamageKiller)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "player Id: " .. cid )
if isInRange(getPlayerPosition(cid), arena.frompos, arena.topos) then
doCreatureAddHealth(cid, getCreatureMaxHealth(cid) - getCreatureHealth(cid), true)
doTeleportThing(cid, arena.exit)
doSendMagicEffect(arena.exit, 10)
return false
end
return true
end

the doCreatureAddHealth doesn't seem to be working.
It also doesn't exist on compat.lua, but Im new to all that so maybe Im not looking right.

Also, if I die outside the temple it doesn't run my other ondeath() script.
 
There's a list of TFS 1.0 functions here:
http://otland.net/threads/tfs-1-0-lua-functions.197202/

It has the signature for that call as:
doCreatureAddHealth(cid, health)
Im using 0.3.6, how can I had that function?

Is the onDeath() correctly defined in XML and registered?

Yea, and even if I take off the arena thing it is not working now. I have no idea how I could have possibly broken it o_O

<event type="death" name="deathcheck" event="script" value="death.lua"/>
registerCreatureEvent(cid, "death")
 
Last edited by a moderator:
I think you register the name (deathcheck), so you can have different callbacks for the same event type.
Hm... I don't know, I went to check other creturescripts of other otservers and they follow the same system.
But I changed the registerCreatureEvent() from death to deathcheck and now it works :D

My only problem now is adding health to the player, so that he doesn't die.
 
Last edited:
Back
Top