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

Lua Simple Lua If Statement - onHealthChange

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
I have the following code:

Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
   
    if attacker:getLevel() - creature:getLevel() >= 100 and math.random(100) < 15 then -- cannot dodge players if level higher by 100
        creature:say("Cant dodge!", TALKTYPE_MONSTER_SAY)
       
    elseif math.random(100) < 15 then
    creature:say("Dodge!", TALKTYPE_MONSTER_SAY)
    primaryDamage = 0
   
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

The first if statement checks for the following:

- The player is unable to dodge players with level higher than 100, and there is also a chance of 15% of that happening

It works perfectly but here is the problem with the elseif:

it works as well despite the first one being executed, but I want only one of these to occur


if higher than 100, there is 15% chance that the text will appear saying you cannot dodge him and that SHOULD END THE IF STATEMENT after that, but it is not.

I would appreciate help

TFS 1.0
 
Solution
I'm not 100% sure what you mean, but I took some guesses and beautified your code little:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  
    if math.random(100) < 15 then
        if attacker:getLevel() - creature:getLevel() >= 100 then -- cannot dodge players if level higher by 100
            creature:say("Cant dodge!", TALKTYPE_MONSTER_SAY)
        else
            creature:say("Dodge!", TALKTYPE_MONSTER_SAY)
            primaryDamage = 0
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
I'm not 100% sure what you mean, but I took some guesses and beautified your code little:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  
    if math.random(100) < 15 then
        if attacker:getLevel() - creature:getLevel() >= 100 then -- cannot dodge players if level higher by 100
            creature:say("Cant dodge!", TALKTYPE_MONSTER_SAY)
        else
            creature:say("Dodge!", TALKTYPE_MONSTER_SAY)
            primaryDamage = 0
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Solution
Back
Top