• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua mana, health change (magic shield)

Darius93

Active Member
Joined
Oct 16, 2022
Messages
88
Reaction score
27
Hello, I have a small problem. I’m using a script in creaturescripts -> onHealthChange and onManaChange.
onHealth:
LUA:
if attacker and attacker:isMonster() then
    local name = attacker:getName():lower()
    local desc = attacker:getDescription():lower()


    if desc:find("survival monster") and name:find("lv]") then

        local monsterLevel = Survival.getMonsterLevelByName(attacker)

        local percent = SurvivalConfig.settings.damagePerLevel

        local multiplier = 1 + (monsterLevel * percent)

        primaryDamage = math.floor(primaryDamage * multiplier)

        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
end

onMana:
LUA:
if attacker and attacker:isMonster() then
    local name = attacker:getName():lower()
    local desc = attacker:getDescription():lower()

    if desc:find("survival monster") and name:find("lv]") then

        local monsterLevel = Survival.getMonsterLevelByName(attacker)

        local percent = SurvivalConfig.settings.damagePerLevel

        local multiplier = 1 + (monsterLevel * percent)

        manaChange = math.floor(manaChange * multiplier)

        return manaChange
    end
end


The script is supposed to deal damage based on the monster’s level.


The problem is, for example: a monster hits for 2000 damage. When a player has 1000 mana and is using utamo vita, it should work like this: when the monster hits, it should take 1000 mana and 1000 HP from the player.


Right now it works like this: it only takes 1000 mana and that’s it. If I had 300 mana, it would only hit for 300 and wouldn’t take anything from HP.


How can I achieve the desired effect?
TFS 1.2
 
Solution
How can I achieve the desired effect?
TFS 1.2
It should work like this. Post your full .lua scripts. Maybe you skip damage 'origin' attribute in script.
TFS 1.2 logic:
- if HP damage is negative (reduce HP) -> check for magic shield -> if has magic shield -> call onManaChange -> deal damage to mana -> reduce HP by 'mana damage' -> call onHealthChange with reduced value -> deal damage

Anyway, you should move 'monster boost level' to C++ and apply it when monster deals damage, not when player receives damage.
Hello, I have a small problem. I’m using a script in creaturescripts -> onHealthChange and onManaChange.
onHealth:
LUA:
if attacker and attacker:isMonster() then
    local name = attacker:getName():lower()
    local desc = attacker:getDescription():lower()


    if desc:find("survival monster") and name:find("lv]") then

        local monsterLevel = Survival.getMonsterLevelByName(attacker)

        local percent = SurvivalConfig.settings.damagePerLevel

        local multiplier = 1 + (monsterLevel * percent)

        primaryDamage = math.floor(primaryDamage * multiplier)

        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
end

onMana:
LUA:
if attacker and attacker:isMonster() then
    local name = attacker:getName():lower()
    local desc = attacker:getDescription():lower()

    if desc:find("survival monster") and name:find("lv]") then

        local monsterLevel = Survival.getMonsterLevelByName(attacker)

        local percent = SurvivalConfig.settings.damagePerLevel

        local multiplier = 1 + (monsterLevel * percent)

        manaChange = math.floor(manaChange * multiplier)

        return manaChange
    end
end


The script is supposed to deal damage based on the monster’s level.


The problem is, for example: a monster hits for 2000 damage. When a player has 1000 mana and is using utamo vita, it should work like this: when the monster hits, it should take 1000 mana and 1000 HP from the player.


Right now it works like this: it only takes 1000 mana and that’s it. If I had 300 mana, it would only hit for 300 and wouldn’t take anything from HP.


How can I achieve the desired effect?
TFS 1.2


It looks like the code needs additional checking for the player's mana
And adding an if - if the player has 0 mana, get the difference from the HP

If you still need help, leave a private message. I'll try to help
 
But how am I supposed to pass the “missing difference” from onManaChange to onHealthChange?


When I have Magic Shield (utamo vita) active, the damage is handled through onManaChange, not onHealthChange, so I only see the mana loss there.
 
But how am I supposed to pass the “missing difference” from onManaChange to onHealthChange?


When I have Magic Shield (utamo vita) active, the damage is handled through onManaChange, not onHealthChange, so I only see the mana loss there.

If both functions are in the same file, you could use logic and assign local my_mana = 0 at the top
Then in onManaChange send the differences here, and below that, the onHealthChange function will retrieve the value from my_mana

Of course first of all, the first question: did you make sure the change was in the right place?
I usually made changes to monsters dmg in events/creature.lua
 
LUA:
    <!-- Creature methods -->
    <event class="Creature" method="onChangeOutfit" enabled="0" />
    <event class="Creature" method="onAreaCombat" enabled="0" />
    <event class="Creature" method="onTargetCombat" enabled="1" />

    <!-- Party methods -->
    <event class="Party" method="onJoin" enabled="0" />
    <event class="Party" method="onLeave" enabled="0" />
    <event class="Party" method="onDisband" enabled="0" />
    <event class="Party" method="onShareExperience" enabled="1" />

    <!-- Player methods -->
    <event class="Player" method="onLook" enabled="1" />
    <event class="Player" method="onLookInBattleList" enabled="1" />
    <event class="Player" method="onLookInTrade" enabled="1" />
    <event class="Player" method="onLookInShop" enabled="0" />
    <event class="Player" method="onMoveItem" enabled="1" />
    <event class="Player" method="onMoveCreature" enabled="0" />
    <event class="Player" method="onTurn" enabled="1" />
    <event class="Player" method="onTradeRequest" enabled="1" />
    <event class="Player" method="onTradeAccept" enabled="0" />
    <event class="Player" method="onGainExperience" enabled="1" />
    <event class="Player" method="onLoseExperience" enabled="1" />
    <event class="Player" method="onGainSkillTries" enabled="1" />
    
        <!-- Monster methods -->
    <event class="Monster" method="onSpawn" enabled="1" />
    <event class="Monster" method="onDropLoot" enabled="1" />

LUA:
function Creature:onChangeOutfit(outfit)
    return true
end

function Creature:onAreaCombat(tile, isAggressive)
    return RETURNVALUE_NOERROR
end

function Creature:onTargetCombat(target)
    if not self then
        return true
    end

    if self:isPlayer() and target:isPlayer() then
        local party = self:getParty()
        if party then
            local targetParty = target:getParty()
            if targetParty and targetParty == party then
                return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER
            end
        end
    end
    return true
end


function Creature:onTargetCombat(target)
    if not self then
        return true
    end

    if self:isPlayer() and target:isMonster() then
        if target:getName():lower() == "kamien" then
            local weaponRight = self:getSlotItem(CONST_SLOT_RIGHT)
            local weaponLeft = self:getSlotItem(CONST_SLOT_LEFT)

            local hasPick = (weaponRight and weaponRight:getId() == 2440)
                         or (weaponLeft and weaponLeft:getId() == 2440)

            if not hasPick then
                self:sendCancelMessage("Aby kopac musisz miec kilof w rece!")
                return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE
            end
        end
    end

    return true
end

That's what I have in events.xml.

So I guess there's no place to put it here?
 
How can I achieve the desired effect?
TFS 1.2
It should work like this. Post your full .lua scripts. Maybe you skip damage 'origin' attribute in script.
TFS 1.2 logic:
- if HP damage is negative (reduce HP) -> check for magic shield -> if has magic shield -> call onManaChange -> deal damage to mana -> reduce HP by 'mana damage' -> call onHealthChange with reduced value -> deal damage

Anyway, you should move 'monster boost level' to C++ and apply it when monster deals damage, not when player receives damage.
 
Solution
It should work like this. Post your full .lua scripts. Maybe you skip damage 'origin' attribute in script.
TFS 1.2 logic:
- if HP damage is negative (reduce HP) -> check for magic shield -> if has magic shield -> call onManaChange -> deal damage to mana -> reduce HP by 'mana damage' -> call onHealthChange with reduced value -> deal damage

Anyway, you should move 'monster boost level' to C++ and apply it when monster deals damage, not when player receives damage.

I believe that someone as experienced as Gesior.pl will propose advanced solutions based on their expertise, not tailored for intermediate-level users
I would like to point out that if we moved these values to events/creature.lua, we could rely solely on the damage increase, while the HP/MP function would still work according to the C++ logic

It is also important to remember that onHealthChange should work correctly in the case of Utamo Vita
What I mean is that the onHealthChange code does respond, but when the player has Utamo Vita active, mana is reduced first and only then HP.
That is why I usually encourage you to contact me privately for more detailed questions
 
View attachment 97010
You lose 5 mana.
You lose 42 hitpoints.

Have you tried using only
doTargetCombatHealth(cid, targetcreature, COMBAT_PHYSICALDAMAGE, -damage, -damage)
in the script?

Yes, it was one of the ideas for a private message, but no private conversation took place. He finally solved it via C++
 
Back
Top