• 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 Spell attack and heal

norrow

Member
Joined
Dec 16, 2012
Messages
129
Reaction score
7
Location
Poland
hello, I would like to ask you experienced users for help with a spell because I can't deal with it, I don't know how to bite this topic, the point is that I would like a spell that is a target but depending on how much damage it deals, it heals the user by 30%
I hope someone can help me because I really don't know what to do
Tfs 1.4
 
If it's the full unmitigated damage, you would want to do that inside of the spell.

Calculate damage -> heal user.

If it's the damage the target actually takes after mitigations from armor/resistances, you'd have to do that inside of onHealthChange and onManaChange (assuming you mana hits to count towards the healing of the user.)

In the above scenario you'd need to register the scripts to all players and monsters. (onLogin/onSpawn)
And when casting the spell, give a storage to the caster.. as a tag to verify if they should be healed or not.

Unfortunately this does have a small drawback of it being possible (albeit very unlikely) for another damage to 'slip through' beforehand.
Like if you had 4 dot's on someone and cast the spell multiple times, and they happen to trigger at the same time.. meh
probably not worth mentioning.
 
function onCastSpell(creature, variant) local target = variant:getNumber() if not target then creature:sendCancelMessage("You have to target a creature.") return false end local target = Creature(target) if not target then creature:sendCancelMessage("You have to target a creature.") return false end if target:isPlayer() and target:getGuid() ~= creature:getGuid() and not creature:getGroup():getAccess() then creature:sendCancelMessage("You cannot cast this spell on this target.") return false end local level = creature:getLevel() local magicLevel = creature:getMagicLevel() local damage = math.floor((level + magicLevel) * 0.3) target:addHealth(-damage, false, "spell", false) creature:addHealth(damage * 0.3, false, "spell", false) creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE) return true end
Just change: local damage = math.floor((level + magicLevel) * 0.3) to calculate the damage
 
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, 5)

function onGetFormulaValues(player, level, magicLevel)
    local min = (level / 5) + (magicLevel * 1.4) + 8
    local max = (level / 5) + (magicLevel * 2.2) + 14
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(creature, variant)
local target = variant:getNumber()
    if not target then
    creature:sendCancelMessage("You have to target a creature.")
    return false
end

local target = Creature(target)
    if not target then
    creature:sendCancelMessage("You have to target a creature.")
    return false
end

if target:isPlayer() and target:getGuid() ~= creature:getGuid() and not creature:getGroup():getAccess() then
    creature:sendCancelMessage("You cannot cast this spell on this target.")
    return false
end

local level = creature:getLevel()
local magicLevel = creature:getMagicLevel()
local damage = math.floor((level + magicLevel) * 0.9)
target:addHealth(-damage, false, "spell", false)
creature:addHealth(damage * 0.9, false, "spell", false)
creature:getPosition():sendMagicEffect(5)
return true
end
In general, it works as it should, that is, deals damage and heals the user, I have only one problem that the effect shows only on the user during the treatment of it and the purpose of the effect does not appear
 
Code:
function onCastSpell(creature, variant)
    local target = variant:getNumber()
    if not target then
        creature:sendCancelMessage("You have to target a creature.")
        return false
    end

    local targetCreature = Creature(target)
    if not targetCreature then
        creature:sendCancelMessage("You have to target a creature.")
        return false
    end

    if targetCreature:isPlayer() and targetCreature:getGuid() ~= creature:getGuid() and not creature:getGroup():getAccess() then
        creature:sendCancelMessage("You cannot cast this spell on this target.")
        return false
    end

    local level = creature:getLevel()
    local magicLevel = creature:getMagicLevel()
    local damage = math.floor((level + magicLevel) * 0.3)

    local projectile = creature:getPosition():createDistanceEffect(targetCreature:getPosition(), CONST_ANI_ARROW)
    projectile:setMissileHeight(1)
    addEvent(function() 
        if not targetCreature:isRemoved() then
            targetCreature:addHealth(-damage, false, "spell", false)
            targetCreature:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        end
    end, 400)

    creature:addHealth(damage * 0.3, false, "spell", false)
    creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)

    return true
end
try this
 
I managed to improve and everything is as I wanted it to be, I have a question if this script looks correct or is there something wrong/poorly done in it?
Lua:
function onCastSpell(creature, variant)
local target = variant:getNumber()
local level = creature:getLevel()
local magicLevel = creature:getMagicLevel()
local damage = math.floor((level + magicLevel) * 0.9)

if not target then
    creature:sendCancelMessage("You have to target a creature.")
    return false
end

local target = Creature(target)
    if not target then
    creature:sendCancelMessage("You have to target a creature.")
    creature:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
    return false
end

if target:isPlayer() and target:getGuid() ~= creature:getGuid() and not creature:getGroup():getAccess() then
    creature:sendCancelMessage("You cannot cast this spell on this target.")
    return false
end

    target:addHealth(-damage, false, "spell", false)
    target:getPosition():sendMagicEffect(5)
    creature:addHealth(damage * 0.9, false, "spell", false)
    creature:getPosition():sendMagicEffect(5)
    return true
end
 
Back
Top