Tfs 1.2 LoopCount Error

lazarus321

Member
Joined
May 8, 2017
Messages
164
Best answers
0
Reaction score
15
Hi guys,

I make one spell script with LoopCount but if select the target and deselect, the loop get error because no have target more. How can I fix this part?

Spell.

C++:
local function sendHealingEffect(cid, position, loopCount)
    local player = Player(cid)
    local alvo = player:getTarget()
    local alvopos = alvo:getPosition()
    local nivel = player:getLevel()
    local ml = player:getMagicLevel()
    
    local efeito = math.random(2,8)
    
      position:sendDistanceEffect(alvopos, efeito)

    if efeito == 2 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_PHYSICALDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 3 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_FIREDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 4 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_ICEDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 5 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_EARTHDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 6 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_ENERGYDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 7 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_HOLYDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 8 then
    addEvent(function()  doTargetCombatHealth(0, alvo, COMBAT_DEATHDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
        
end

function onCastSpell(creature, var)
    local playerPos = creature:getPosition()
    local qtd = math.floor((creature:getLevel() / 100) + (creature:getMagicLevel() / 10))
    local alvo = creature:getTarget()
    local alvopos = alvo:getPosition()
    
    local loopCount = qtd

    for i = 1, loopCount do

        local position = Position(playerPos.x + math.random(-4, 4), playerPos.y + math.random(-4, 3), playerPos.z)
        addEvent(doAreaCombatHealth, i * 125, creature:getId(), COMBAT_PHYSICALDAMAGE, position, 0, 0, 0, CONST_ME_NONE)
        addEvent(sendHealingEffect, i * 125, creature:getId(), position, loopCount - i)

    end
    return true
end
 

zetibia

Intermediate OT User
Joined
Jun 23, 2013
Messages
106
Best answers
0
Reaction score
15
You need to verify that the creature exists. Something like if not Creature(target) then return false end in the scripts that will be executed in the future (addEvent).

Code:
local function sendHealingEffect(cid, position, loopCount)
    local player = Player(cid)
    if not player then
        return true
    end
    local alvo = player:getTarget()
    local alvopos = alvo:getPosition()
    local nivel = player:getLevel()
    local ml = player:getMagicLevel()
    
    local efeito = math.random(2,8)
    
      position:sendDistanceEffect(alvopos, efeito)

    if efeito == 2 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_PHYSICALDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 3 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_FIREDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 4 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_ICEDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 5 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_EARTHDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 6 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_ENERGYDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 7 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_HOLYDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
    if efeito == 8 then
    addEvent(function()  local creature = Creature(alvo) if not creature then return false end doTargetCombatHealth(0, alvo, COMBAT_DEATHDAMAGE, -10, -30, CONST_ME_NONE) end, 1)
    end
        
end

function onCastSpell(creature, var)
    local playerPos = creature:getPosition()
    local qtd = math.floor((creature:getLevel() / 100) + (creature:getMagicLevel() / 10))
    local alvo = creature:getTarget()
    local alvopos = alvo:getPosition()
    
    local loopCount = qtd

    for i = 1, loopCount do

        local position = Position(playerPos.x + math.random(-4, 4), playerPos.y + math.random(-4, 3), playerPos.z)
        addEvent(doAreaCombatHealth, i * 125, creature:getId(), COMBAT_PHYSICALDAMAGE, position, 0, 0, 0, CONST_ME_NONE)
        addEvent(sendHealingEffect, i * 125, creature:getId(), position, loopCount - i)

    end
    return true
end
 
OP
L

lazarus321

Member
Joined
May 8, 2017
Messages
164
Best answers
0
Reaction score
15
Hi Zetibia,

I try add this above "position:sendDistanceEffect(alvopos, efeito" and not work.

I think after this function starts "for i = 1, loopCount do" no stop.

C++:
if not Creature(alvo) then
return false
end
 
Top