• 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 :)
 
Is the player dieing or standing without health? Do you get the textmessage if you add it under isInRange?

So, what happens is that it runs through the code, teleports the player out of the arena (doesn't add health) and then runs again (because his health is 0) and the player dies.
The message is displayed, only once.

that last Orange frase is to test if my other death script runs.
30tjabn.png
 
Maybe switch to the OO methods (possibly bypassing a badly behaved script):

creature:getMaxHealth()
creature:getHealth()
creature:addHealth(healthChange)
creature:setMaxHealth(maxHealth)

And some changes to your "tracing":

Add the script name and function name to the messages (set in a variable at the start of each function you're working with)
Make a boolean "debug" at the start of each script, default false, set to true ina function when you want to log thingss, false on exiting the function, just before return)
change the logging messages so they're controlled by an if statement:
if debug then <code to send a nice message here> end

Then add messages to display on entry Maxhealth, Health, to you function(s) and after you change them.
(NB: any message you use a lot can be put into a function so you only have to write the message code once)

And ofc: do this for everything you're experimenting with: debugging goes a lot faster when you know what's going on :)

The principle is to identify points in your code where you think you know the values of your key control variables.
Make comments stating what you think is true (these are called "assertions" - some languages have built-in support for this :)
If you're not sure what's happening, send yourself a message with the values of your key variables, and enough info so you can match them to your assertions. If they don't match, you know where to look to understand why (because you chose your assertions for that specific purpose :)
 
Maybe switch to the OO methods (possibly bypassing a badly behaved script):

creature:getMaxHealth()
creature:getHealth()
creature:addHealth(healthChange)
creature:setMaxHealth(maxHealth)

And some changes to your "tracing":

Add the script name and function name to the messages (set in a variable at the start of each function you're working with)
Make a boolean "debug" at the start of each script, default false, set to true ina function when you want to log thingss, false on exiting the function, just before return)
change the logging messages so they're controlled by an if statement:
if debug then <code to send a nice message here> end

Then add messages to display on entry Maxhealth, Health, to you function(s) and after you change them.
(NB: any message you use a lot can be put into a function so you only have to write the message code once)

And ofc: do this for everything you're experimenting with: debugging goes a lot faster when you know what's going on :)

The principle is to identify points in your code where you think you know the values of your key control variables.
Make comments stating what you think is true (these are called "assertions" - some languages have built-in support for this :)
If you're not sure what's happening, send yourself a message with the values of your key variables, and enough info so you can match them to your assertions. If they don't match, you know where to look to understand why (because you chose your assertions for that specific purpose :)

I understood half of it but thanks haha.
I tried the creature: thing but didn't work


BUT I HAVE IT WORKING NOW!
It was basically missing parameters I think, I could't find where the function was defined.
This is what I used
Code:
doCreatureAddHealth(cid, getCreatureMaxHealth(cid)-getCreatureHealth(cid), 0, 0, 1)

Thanks for helping guys!
 
Where did you find out about the extra parameters? Do you have a custom version of doCreatureAddHealth()?

The one in my TFS 1.0 compat.lua looks like this:

function doCreatureAddHealth(cid, health)
local c = Creature(cid)
return c ~= nil and c:addHealth(health) or false
end


i.e. it has no room for your last three parameters.

(BTW "c = Creature(cid)" and "c:addHealth()" are examples of the OO-style Lua coding I suggested above)
 
Where did you find out about the extra parameters? Do you have a custom version of doCreatureAddHealth()?

The one in my TFS 1.0 compat.lua looks like this:

function doCreatureAddHealth(cid, health)
local c = Creature(cid)
return c ~= nil and c:addHealth(health) or false
end


i.e. it has no room for your last three parameters.

(BTW "c = Creature(cid)" and "c:addHealth()" are examples of the OO-style Lua coding I suggested above)

I have no idea man, I didn't found the function anywhere to check the parameters. I just searched for 1 hour in the forums for people using CreatureAddHealth and found someone doing it that way haha.
Its a bad solution, but its a solution.
 
It's a good enough solution :)

I'm just interested in the API, because it implies extra capabilities that might also be available in the newer (OO) APIs.

OTOH if you don't already know what the extra parameters mean here's no reason for you to check it out for me - I'll do it myself if I ever need to.
 
Back
Top