• 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

luanmaximus123

New Member
Joined
Oct 28, 2010
Messages
15
Reaction score
2
I'm creating a spell that increases the player's damage, I'm using this script:
LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not creature or not attacker or not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
  
    if attacker:getStorageValue(3482101) == 1 then
        primaryDamage = primaryDamage + (primaryDamage * 50 / 100)
        secondaryType = secondaryType + (secondaryType * 50 / 100)
        print("Test")
    end
    return primaryDamage, primaryType, primaryDamage, secondaryType
end

Code:
<event type="healthchange" name="buffdamage" script="buffDamage.lua"/>

login.lua
Code:
player:registerEvent("buffdamage")

The problem is that it doesn't work, I only get the print 'Test' when I heal myself or take damage.

(The spell for storage is working perfectly)
TFS 1.4.2
 
not tested

LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not attacker or not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if attacker:getStorageValue(3482101) == 1 then
        if primaryDamage < 0 then
            primaryDamage = math.floor(primaryDamage * 1.5) -- Increase primary damage by 50%
        end

        if secondaryDamage < 0 then
            secondaryDamage = math.floor(secondaryDamage * 1.5) -- Increase secondary damage by 50%
        end

        print(string.format("Increased damage: Primary = %d, Secondary = %d", primaryDamage, secondaryDamage))
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
The problem is that it only works when I change the player's health, for example, when I use exura it works.
LUA:
Increased damage: Primary = 462, Secondary = 0
But when I attack a creature, it doesn't work.
 
The problem is that it only works when I change the player's health, for example, when I use exura it works.
LUA:
Increased damage: Primary = 462, Secondary = 0
But when I attack a creature, it doesn't work.
onHealthChange is for the creature that is having it's health changed.

For it to work on monsters as well, they would need to have it registered, just like the player.

For a single creature, put it into their xml file (just above the loot is a good spot)
XML:
<script>
    <event name="buffdamage"/>
</script>

If you want it for all monsters, then an easy way to register for every mob is via onSpawn.
(data/events/events.xml, ensure that onSpawn is set to 1, so it's enabled.)
Then put this code into data/scripts as a .lua file
LUA:
local eventCallback = EventCallback

function eventCallback.onSpawn(creature, position, startup, artificial)
    creature:registerEvent("buffdamage")
    return true
end

eventCallback:register(-666)
 
It worked adding it in Monster onSpawn, but now it seems like it's doing a double hit.

LUA:
function Monster:onSpawn(position, startup, artificial)
    self:registerEvent("buffdamage")
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
end

Captura de tela 2024-12-13 162353.webp
 
It worked adding it in Monster onSpawn, but now it seems like it's doing a double hit.

LUA:
function Monster:onSpawn(position, startup, artificial)
    self:registerEvent("buffdamage")
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
end

View attachment 88857
I'm creating a spell that increases the player's damage, I'm using this script:
LUA:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not creature or not attacker or not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
 
    if attacker:getStorageValue(3482101) == 1 then
        primaryDamage = primaryDamage + (primaryDamage * 50 / 100)
        secondaryType = secondaryType + (secondaryType * 50 / 100)
        print("Test")
    end
    return primaryDamage, primaryType, primaryDamage, secondaryType
end
If you're using your original code, you have a couple problems there.

change line 8
LUA:
secondaryType = secondaryType + (secondaryType * 50 / 100)
to
LUA:
secondaryDamage = secondaryDamage + (secondaryDamage * 50 / 100)

and change line 11
LUA:
return primaryDamage, primaryType, primaryDamage, secondaryType
to
LUA:
return primaryDamage, primaryType, secondaryDamage, secondaryType
 
Last edited by a moderator:
Back
Top