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

peha

New Member
Joined
Apr 21, 2020
Messages
12
Reaction score
0
Hi, I want to made spell which boost next auto attack. Spell gives certain storage to player, but my onHealthChange scripts doesn't work.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
  if getStorageValue(attacker, 1005) == 1 then
    primaryDamage, secondaryDamage = primaryDamage*100, secondaryDamage*100
    setStorageValue(attacker, 1005, -1)
  end
  return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
attacker:getStorageValue(1005) == 1 attacker:setStorageValue(1005, -1)

also you need to check if attacker:isPlayer() before checking storages
 
attacker:getStorageValue(1005) == 1 attacker:setStorageValue(1005, -1)

also you need to check if attacker:isPlayer() before checking storages
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker == nil then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if attacker:getStorageValue(1005) == 1 then
          attacker:say("Boom!", TALKTYPE_MONSTER_SAY)
          attacker:getPosition():sendMagicEffect(20)
          attacker::setStorageValue(1005, -1)
      return 500, primaryType, 500, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
XML:
<event type="healthchange" name="EnhancedAttack" script="boost.lua"/>
I did it like this and still doesn't work. Function not even starting, when i put Print("X") under function line.
 
Last edited:
I did it like this and still doesn't work. Function not even starting, when i put Print("X") under function line.

You forgot to register event on login and onSpawn

Keep in mind that it's onHealthChange so it has to be registered on your target, not the attacker.

Also, you have double ":" right above "return 500..."

Also it's onHealthChange so if you don't do damage, it won't execute.
 
Last edited:
You forgot to register event on login and onSpawn

Keep in mind that it's onHealthChange so it has to be registered on your target, not the attacker.

Also, you have double ":" right above "return 500..."

Also it's onHealthChange so if you don't do damage, it won't execute.
boost.lua:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker == nil then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if attacker:getStorageValue(1005) == 1 then
          attacker:say("Boom!", TALKTYPE_MONSTER_SAY)
      attacker:getPosition():sendMagicEffect(20)
          attacker:setStorageValue(1005, -1)
      return 500, primaryType, 500, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
creaturescripts.xml:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<creaturescripts>
    <event type="login" name="PlayerLogin" script="login.lua" />
    <event type="logout" name="PlayerLogout" script="logout.lua" />
    <event type="login" name="FirstItems" script="firstitems.lua" />
    <event type="login" name="RegenerateStamina" script="regeneratestamina.lua" />
    <event type="death" name="PlayerDeath" script="playerdeath.lua" />
    <event type="death" name="DropLoot" script="droploot.lua" />
    <event type="extendedopcode" name="ExtendedOpcode" script="extendedopcode.lua" />

    <event type="healthchange" name="EnhancedAttack" script="boost.lua"/>
</creaturescripts>
login.lua:
Lua:
function onLogin(player)
    local loginStr = "Welcome to " .. configManager.getString(configKeys.SERVER_NAME) .. "!"
    if player:getLastLoginSaved() <= 0 then
        loginStr = loginStr .. " Please choose your outfit."
        player:sendOutfitWindow()
    else
        if loginStr ~= "" then
            player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)
        end

        loginStr = string.format("Your last visit was on %s.", os.date("%a %b %d %X %Y", player:getLastLoginSaved()))
    end
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)

    -- Stamina
    nextUseStaminaTime[player.uid] = 0

    -- Promotion
    local vocation = player:getVocation()
    local promotion = vocation:getPromotion()
    if player:isPremium() then
        local value = player:getStorageValue(STORAGEVALUE_PROMOTION)
        if not promotion and value ~= 1 then
            player:setStorageValue(STORAGEVALUE_PROMOTION, 1)
        elseif value == 1 then
            player:setVocation(promotion)
        end
    elseif not promotion then
        player:setVocation(vocation:getDemotion())
    end

    -- Events
    player:registerEvent("PlayerDeath")
    player:registerEvent("DropLoot")
    player:registerEvent("EnhancedAttack")
    player:setStorageValue(1005, -1)
    return true
end

troll.xml (mob on which i am testing):
XML:
<?xml version="1.0" encoding="UTF-8"?>
<monster name="Troll" nameDescription="a troll" race="blood" experience="20" speed="0" manacost="290">
    <health now="50000" max="50000"/>
    <look type="15" corpse="5960"/>
    <targetchange interval="2000" chance="0"/>
    <strategy attack="100" defense="0"/>
    <flags>
        <flag summonable="1"/>
        <flag attackable="1"/>
        <flag hostile="1"/>
        <flag illusionable="1"/>
        <flag convinceable="1"/>
        <flag pushable="1"/>
        <flag canpushitems="0"/>
        <flag canpushcreatures="0"/>
        <flag targetdistance="1"/>
        <flag staticattack="90"/>
        <flag runonhealth="15"/>
    </flags>
    <attacks>
    </attacks>
    <defenses armor="6" defense="8"/>
    <elements>
    </elements>
    <voices interval="5000" chance="10">
        <voice sentence="Grrrr"/>
    </voices>
    <loot>
    </loot>
    <scripts>
        <event name="EnhancedAttack"/>
    </scripts>
</monster>

and spell which i am using to boost my next damage:
Lua:
function notify(creature)
    local creature = Creature(creature)
    if not creature then stopEvent(notify) return end

    if creature:getStorageValue(1005) == 1 then
        print("Your next attack is enhanced")
      addEvent(notify, 30000, creature:getId())
    end
end

function onCastSpell(creature, variant)
    if getPlayerStorageValue(creature, 1005) <= 0 then
        setPlayerStorageValue(creature, 1005, 1)
        addEvent(notify, 100, creature:getId())
    end
    return true
end


Spell is giving certain storage, I checked this. Seems like functions onHealthChange doesn't work. Even if I add print("test") right under function line it doesn't appear.
 
Last edited:
Hi, I want to made spell which boost next auto attack. Spell gives certain storage to player, but my onHealthChange scripts doesn't work.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
  if getStorageValue(attacker, 1005) == 1 then
    primaryDamage, secondaryDamage = primaryDamage*100, secondaryDamage*100
    setStorageValue(attacker, 1005, -1)
  end
  return primaryDamage, primaryType, secondaryDamage, secondaryType
end
onHealthChange is pretty trash in use when you have multiple "onHealthChange" events and one of it change damage.
Take working code
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if primaryDamage >= 0 or secondaryDamage >= 0 then
    if creature:isMonster() or creature:isPlayer() then
    if attacker:isPlayer() and attacker:getStorageValue(1005) == 1 then
          attacker:say("Show!", TALKTYPE_MONSTER_SAY)
          attacker:getPosition():sendMagicEffect(20)
          attacker::setStorageValue(1005, 0)
          primaryDamage = 500
end
                return primaryDamage, primaryType, secondaryDamage, secondaryType
      
        else

                return primaryDamage, primaryType, secondaryDamage, secondaryType
end
        if attacker:isPlayer() and creature:isMonster() then
            end
        return primaryDamage, primaryType, secondaryDamage, secondaryType

end
return true
end

Code:
      return 500, primaryType, 500, secondaryType
This dont work like that, check code how to define it and use :)
Use 0 instead of -1, and delete this storage from login.lua
 
Last edited:
Doesn't work still. It's 8.6 downgraded TFS..

Its you spell... Change it for this one, and check if work:
Lua:
function onCastSpell(cid, var)
    if cid:getPlayerStorageValue(1005) == 0 then
        cid:setPlayerStorageValue(1005, 1)
          cid:say("Your next attack is enhanced", TALKTYPE_MONSTER_SAY)
    end
    return true
end
 
Back
Top