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

TFS 1.X+ [TFS 1.3] onHealthChange problems

Demnish

Tibian Hero
Joined
Sep 28, 2011
Messages
402
Solutions
2
Reaction score
65
Location
Sweden
Trying to implement this script:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    --if math.random(1, 1) == 1 then
    if origin == ORIGIN_MELEE then
        creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
        return primaryDamage * 1000, primaryType, secondaryDamage, secondaryType
    end
    return primaryDamage * 1000, primaryType, secondaryDamage, secondaryType
end
But it won't work, it doesn't do anything.

But if I use this one:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
    return true
end
I seem to only get it to work when I heal a player with an UH and if I use it on myself I get an added CONST_ME_POFF.

Any ideas why this is not working/happening?
 
Solution
You could also do
Lua:
local mType = MonsterType("rat")
mType:registerEvent("eventName")
This would fire for all monsters of that type.
or you take a look at
where you have the necessary function to make something like this now:
Lua:
local fireMonsterEvents = GlobalEvent("fireMonsterEvents")

fireMonsterEvents.onStartup = function()
    for name, mType in pairs(Game.getMonsterTypes()) do
        mType:registerEvent("name_of_event")
    end
end

fireMonsterEvents:register()
Which would register it for all spawn existing monster types
Do you know what origin means?
You didnt tell us anything on how you register these
 
origin = attack type ?

creaturescripts.xml
Code:
<event type="healthchange" name="Test" script="custom/test.lua" />

Then I registered it into login.lua as usual.

Was following an old guide by @Evan on how to implement it, but can't get it to work.
Also tried to copy paste the first script he posted straight up, but with no result.
However this was from 2014.

First time I'm using the onHealthChange function.
I have no idea how to register creatures as per the wiki statement.
 
Last edited:
"If you heal player", onHealthChange works only for damage received by creature that it is registered on, it wont execute for damage done to other creature, for origins usually weapons are melee, wands and distance weapons are distance.
 
How is a monster for example registered?

EDIT: I seem to have found a solution here.
Now I'll begin some testing, if it turns out okay, I'll mark thread as "solved". :cool:
 
Last edited:
on spawn.
Code:
local monster = Game.createMonster("rat", Position(x, y, z))
if monster then
    monster:registerEvent("eventName")
end
 
You could also do
Lua:
local mType = MonsterType("rat")
mType:registerEvent("eventName")
This would fire for all monsters of that type.
or you take a look at
where you have the necessary function to make something like this now:
Lua:
local fireMonsterEvents = GlobalEvent("fireMonsterEvents")

fireMonsterEvents.onStartup = function()
    for name, mType in pairs(Game.getMonsterTypes()) do
        mType:registerEvent("name_of_event")
    end
end

fireMonsterEvents:register()
Which would register it for all spawn existing monster types
 
Solution
Thanks guys for the info.
I'll check them out. :cool:

Managed to get it to work, now I'll just gotta figure out how to incorporate Crit Chance + Crit Damage item attributes into it.
Here's the code if anyone wants to use it:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker:isPlayer() then
        if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
            if math.random(100) <= 5 then -- 5% Crit Chance
                creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                return primaryDamage * 1.25, primaryType, secondaryDamage * 1.25, secondaryType -- 25% Crit Damage
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Thanks guys for the info.
I'll check them out. :cool:

Managed to get it to work, now I'll just gotta figure out how to incorporate Crit Chance + Crit Damage item attributes into it.
Here's the code if anyone wants to use it:
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker:isPlayer() then
        if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
            if math.random(100) <= 5 then -- 5% Crit Chance
                creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                return primaryDamage * 1.25, primaryType, secondaryDamage * 1.25, secondaryType -- 25% Crit Damage
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
Did you test this script? This cause console errors when use potions
change: doTargetCombatHealth() to player:addHealth() in potions
 
Last edited:
???

I get no such error, my potions works just fine. 🤔

However I did find something else; natural fire fields etc do no damage, as if they doesn't exist.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker then
        if attacker:isPlayer() then
            if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
                if math.random(100) <= 50 then
                    creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                    return creature, attacker, primaryDamage * 2, primaryType, secondaryDamage * 2, secondaryType, origin
                end
            end
        end
        return creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin
    end
    return false
end

Its as if FIELDS are nil in "attacker".
 
Last edited:
???

I get no such error, my potions works just fine. 🤔

However I did find something else; natural fire fields etc do no damage, as if they doesn't exist.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker then
        if attacker:isPlayer() then
            if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
                if math.random(100) <= 50 then
                    creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                    return creature, attacker, primaryDamage * 2, primaryType, secondaryDamage * 2, secondaryType, origin
                end
            end
        end
        return creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin
    end
    return false
end

Its as if FIELDS are nil in "attacker".

Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker then
        if attacker:isPlayer() then
            if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
                if math.random(100) <= 50 then
                    creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                    primaryDamage = primaryDamage * 2
                    secondaryDamage = secondaryDamage * 2
                end
            end
        end
    end
    return creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin
end
 
Thanks @Leo32. :)
I'll do some testing but it seems to work good so far and if anyone finds any problems, post them here and I'll check it out.

EDIT: I modified it a bit, added a config for easy change of values.
Lua:
local config = {
    critChance = 5, -- % (Anything >100% is treated as 100%)
    critDamage = 200 -- % (100% is normal damage)
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker then
        if attacker:isPlayer() then
            if origin == ORIGIN_MELEE or origin == ORIGIN_RANGED or origin == ORIGIN_SPELL then
                if math.random(100) <= config.critChance then
                    creature:getPosition():sendMagicEffect(CONST_ME_CRITICAL_DAMAGE)
                    primaryDamage = primaryDamage * (config.critDamage/100)
                    secondaryDamage = secondaryDamage * (config.critDamage/100)
                end
            end
        end
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Last edited:
Health potions are classed as ORIGIN_SPELL so they will crit.

Change line 8:
Lua:
 if attacker:isPlayer() then
to:
Lua:
 if attacker:isPlayer() and primaryType ~= 128 then

EDIT: Maybe not then.
attacker must == nil when using potions.
 
Last edited:
Somehow, I don't have that problem.
But I more or less have a clean and recent version of TFS 1.3.

notforme.png
(With 100% Crit Chance)

This is the code I have; which doesn't seem to be affected by the crit system:
Lua:
if potion.health then
            doTargetCombatHealth(0, target, COMBAT_HEALING, potion.health[1], potion.health[2])
        end

        if potion.mana then
            doTargetCombatMana(0, target, potion.mana[1], potion.mana[2])
        end
 
Back
Top