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

Addevent error when logout

Zazeros

Member
Joined
Feb 13, 2012
Messages
64
Reaction score
17
0.4

Morning, I know there is a lot of threads talking about the same error, but I haven't found one that helped me.

I made this spell, where after 2 seconds, deals damage on the target. Simple.

The problem is: if the player (or creature) that used the spell cease to exist (logging out or dying), i get the error: (luaGetCreatureTarget) Creature not found
I can't find the real "check" for this, because I'm using a target check, but if they die, it can't check if it has a target.

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)


function onCastSpell(cid, var)
doCreatureSetNoMove(cid, true)
doCombat(cid, combat, var)
doSendAnimatedText(getCreaturePos(cid), "2", 150)

 addEvent(function()
if isCreature(cid) then
doSendAnimatedText(getCreaturePos(cid), "1", 150)
doCreatureSetNoMove(cid, false)
end
 end, 2000)
 
addEvent(function()
if getCreatureTarget(cid) ~= 0 and isCreature(cid) then
local target = getCreatureTarget(cid)
local enemypos = getCreaturePosition(target)
local pos = getThingPos(cid)
               doSendDistanceShoot(pos, enemypos, 15)   
 return doTargetCombatHealth(cid, target, COMBAT_PHYSICALDAMAGE, -75, -95, 9)
           else
          return false
                end
          end, 2000)

end
 
Solution
You'd also want to move the check around, to confirm cid exists, before checking if cid has a target

so change this
Lua:
if getCreatureTarget(cid) ~= 0 and isCreature(cid) then
to this
Lua:
if isCreature(cid) and getCreatureTarget(cid) ~= 0 then
Probably because you're not passing cid as a parameter to addEvent. Therefore, cid doesn't exist anymore when player dies/logs out. By passing it to addEvent, cid will exist even if the player/creatures logs out or dies, and getCreaturePosition will return nil.

Lua:
addEvent(function, time, cid)

This may not be the right fix, and I don't have any means to test it. If this doesn't work, someone with more TFS experience will surely help you.
 
You'd also want to move the check around, to confirm cid exists, before checking if cid has a target

so change this
Lua:
if getCreatureTarget(cid) ~= 0 and isCreature(cid) then
to this
Lua:
if isCreature(cid) and getCreatureTarget(cid) ~= 0 then
 
Solution
No problem.

Even if what I said wasn't needed, I would still begin to pass variables to addEvent as this will prevent errors in the future.

In general coding, passing the variable at that time prevents the variable being null and throwing an error in the future, such as in setTimeout in Javascript (which is the equivalent of OT's Lua addEvent)
 
Back
Top