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

Solved Lifesteal Melee Weapon [TFS 1.2]

X X X

Newb
Joined
Jul 26, 2015
Messages
148
Reaction score
13
As the title suggests, I have been trying to create a script for a melee weapon that damages a monster/player each turn, AND heals the user for a percentage of the damage dealt.

I've seen the different threads about "vampriric spell" and "vampiric touch" (Spell - +[Creaturescript] Vampiric touch(Lifesteal atk) Spell - Life steal / Life drain (for X seconds)) or whatever but I've read through those and they aren't what I'm looking for.

I want to be able to just change one club weapon so that it returns health to the user when you attack something with it.

I have a script that I've created, but I know I've done something wrong. I've tried to set up 2 different combats and I'm not good at that. Could someone point me in the right direction? Again, TFS 1.2.

Script:
Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_BLOCKARMOR, 1)
setCombatParam(combat, COMBAT_PARAM_BLOCKSHIELD, 1)
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatFormula(combat, COMBAT_FORMULA_SKILL, 0, 0, 1.0, 0)

local combat2 = createCombatObject()
setCombatParam(combat2, COMBAT_PARAM_TYPE, COMBAT_HEALING)
setCombatParam(combat2, COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_BLUE)
setCombatParam(combat2, COMBAT_PARAM_AGGRESSIVE, FALSE)
setCombatFormula(combat2, COMBAT_FORMULA_LEVELMAGIC, -0.2, -0.2, -0.2, -0.2)
 
function onUseWeapon(cid, var, creature)
    doCombat(cid, combat, var)
    doCombat(creature, combat2, var)
    return true
end

I'm not getting any console errors with the script, but it doesn't quite work. Attacking a monster with the weapon will deal damage to the monster, but it will also "heal" the monster (that is, the monster will have the blue sparkle effect but wont actually gain any HP) instead of healing the player.

Any help is appreciated, I feel like it's really close but I'm making a silly mistake
Regards,
X X X

edit: I know my script I have isn't for a %, but I was trying to get it to heal PERIOD before I worked on healing at a %.
 
Last edited:
Solution
You need to learn some on your own. Here...

Lua:
local combat = createCombatObject()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
local config = {
    weapon_type = 'club', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    percent_heal_health = 10, --10 percent is added
    combat_type = 'physical', --Options: energy, earth, fire, undefined, lifedrain, manadrain, drown, ice, holy, death
    magicEffect = 12
}
function onUseWeapon(player, var)
    if config.weapon_type == 'sword' then
        SKILL = SKILL_SWORD
    elseif config.weapon_type == 'axe' then
        SKILL = SKILL_AXE
    elseif config.weapon_type == 'club' then
        SKILL = SKILL_CLUB...
Try this:

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_BLOCKARMOR, 1)
setCombatParam(combat, COMBAT_PARAM_BLOCKSHIELD, 1)
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatFormula(combat, COMBAT_FORMULA_SKILL, 0, 0, 1.0, 0)
  
function onUseWeapon(cid, var)
    doCreatureAddHealth(cid, 10)
    return doCombat(cid, combat, var)
end
 
if you want to heal the attacker a % then you need to use on healthchange.

not tested

Code:
<event type="healthchange" name="Pokeplayer" script="lifesteal.lua"/>

lifesteal.lua

Code:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
local weapon = 16162 // id of weapon
local chance = 10 // the chance to life steal
local percent = 10 // the percent healed of the attacker
local damage = (primaryDamage + secondaryDamage)

if math.random(100) <= chance then
if attacker:getSlotItem(CONST_SLOT_LEFT):getId() == weapon then
attacker:addHealth((damage/100) * percent)
end
end
     
return primaryDamage, primaryType, secondaryDamage, secondaryType
end

you need to register this event on login.
 
weapon
Lua:
function onTargetCreature(creature, target)
    return target:registerEvent("lifesteal")
end

local combat = Combat()
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, 1)
combat:setParameter(COMBAT_PARAM_BLOCKSHIELD, 1)
combat:setParameter(COMB, COMBAT_PHYSICALDAMAGE)
combat:setFormula(COMBAT_FORMULA_SKILL, 0, 0, 1.0, 0)
combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onUseWeapon(player, variant)
    return combat:execute(player, variant)
end

creaturescripts/creaturescripts.xml
XML:
<event type="healthchange" name="lifesteal" script="lifesteal.lua"/>

creaturescripts/scripts/lifesteal.lua
Lua:
local cfg = {
    weaponId = 16162,
    chance = 10,
    percent = 10
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local damage = primaryDamage + secondaryDamage
    if attacker:isPlayer() then
        if math.random(100) <= cfg.chance then
            local weapon = attacker:getSlotItem(CONST_SLOT_LEFT)
            if weapon and weapon:getId() == cfg.weaponId then
                attacker:addHealth(damage * (cfg.percent/100))
            end
        end
    end
    creature:unregisterEvent("lifesteal")
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

no need to register it in login.lua (it wouldn't do anything anyways)
if you want more weapons there can be more flexibility with this script and alter weapon ids, chance, and percent for each weapon
this should do for now though
 
Last edited:
Thanks for the reply @Xeraphus! I will check this when I get off work!

@Xeraphus I tried the script and It doesn't appear to be working (yes I added the event in creaturescript.xml) The only thing I changed was the weapon ID:

Code:
local cfg = {
    weaponId = 2421,
    chance = 10,
    percent = 10
}
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local damage = primaryDamage + secondaryDamage
    if attacker:isPlayer() then
        if math.random(100) <= cfg.chance then
            local weapon = attacker:getSlotItem(CONST_SLOT_LEFT)
            if weapon and weapon:getId() == cfg.weaponId then
                attacker:addHealth(damage * (cfg.percent/100))
            end
        end
    end
    creature:unregisterEvent("lifesteal")
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

I sat attacking something for a long time and never gained any HP. Any ideas? Also how can I change this so that the lifesteal happens 100% of the time? Chance=100? Or delete the math.random function?

Thanks,
X X X
 
Last edited by a moderator:
If you do it.....do it right.... Weapon script. No need for creaturescript....

Code:
local combat = createCombatObject()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_DEATHDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_DEATH)

local config = {
    weapon_type = 'sword', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    percent_heal_health = 10 --10 percent is added
}

function onUseWeapon(cid, var)
   
    if config.weapon_type == 'sword' then
        SKILL = SKILL_SWORD
    elseif config.weapon_type == 'axe' then
        SKILL = SKILL_AXE
    elseif config.weapon_type == 'club' then
        SKILL = SKILL_CLUB
    elseif config.weapon_type == 'distance' then
        SKILL = SKILL_DISTANCE
    end
   
    if SKILL then
        dmg_min = (getPlayerSkillLevel(cid, SKILL) * 2) + (getPlayerLevel(cid))
        dmg_max = (getPlayerSkillLevel(cid, SKILL) * 2) + (getPlayerLevel(cid) + 20)
    else
        dmg_min = (getPlayerMagLevel(cid) * 2) + (getPlayerLevel(cid))
        dmg_max = (getPlayerMagLevel(cid) * 2) + (getPlayerLevel(cid) + 20)
    end
   
    dmg_amount = math.random(dmg_min, dmg_max)
    heal_amount = (config.percent_heal_health / 100) * dmg_amount

    target = variantToNumber(var)
    doPlayerAddHealth(target, -dmg_amount)
    doPlayerAddHealth(cid, heal_amount)
    doCombat(cid, combat, var)
end

TFS 1.2 code of this

Code:
local combat = createCombatObject()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_DEATHDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_DEATH)

local config = {
    weapon_type = 'sword', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    percent_heal_health = 10 --10 percent is added
}

function onUseWeapon(player, var)
   
    if config.weapon_type == 'sword' then
        SKILL = SKILL_SWORD
    elseif config.weapon_type == 'axe' then
        SKILL = SKILL_AXE
    elseif config.weapon_type == 'club' then
        SKILL = SKILL_CLUB
    elseif config.weapon_type == 'distance' then
        SKILL = SKILL_DISTANCE
    end
   
    if SKILL then
        dmg_min = (player:getSkillLevel(SKILL) * 2) + (player:getLevel())
        dmg_max = (player:getSkillLevel(SKILL) * 2) + (player:getLevel() + 20)
    else
        dmg_min = (player:getMagicLevel() * 2) + (player:getLevel())
        dmg_max = (player:getMagicLevel() * 2) + (player:getLevel() + 20)
    end
   
    dmg_amount = math.random(dmg_min, dmg_max)
    heal_amount = (config.percent_heal_health / 100) * dmg_amount

    target = variantToNumber(var)
    target:addHealth(-dmg_amount)
    player:addHealth(heal_amount)
    doCombat(player, combat, var)
end
 
Last edited by a moderator:
@Itutorial Thanks for the reply I will try it when I get home! @Xeraphus I fixed my issue with healing it works now, I was making a silly error in the weapon script. However I am having troubles changing the lifesteal script so that it heals every turn, what do I need to change?


edit @Itutorial hmmm yours doesn't seem to work...is there something else I need to add?
 
Last edited:
@Itutorial Thanks for the reply I will try it when I get home! @Xeraphus I fixed my issue with healing it works now, I was making a silly error in the weapon script. However I am having troubles changing the lifesteal script so that it heals every turn, what do I need to change?
Lua:
function onTargetCreature(creature, target)
    return target:registerEvent("lifesteal")
end

local combat = Combat()
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, 1)
combat:setParameter(COMBAT_PARAM_BLOCKSHIELD, 1)
combat:setParameter(COMB, COMBAT_PHYSICALDAMAGE)
combat:setFormula(COMBAT_FORMULA_SKILL, 0, 0, 1.0, 0)
combat:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

function onUseWeapon(player, variant)
    return combat:execute(player, variant)
end

local cfg = {
    weaponId = 2421,
    percent = 10
}
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local damage = primaryDamage + secondaryDamage
    if attacker:isPlayer() then
        local weapon = attacker:getSlotItem(CONST_SLOT_LEFT)
        if weapon and weapon:getId() == cfg.weaponId then
            attacker:addHealth(damage * (cfg.percent/100))
        end
    end
    creature:unregisterEvent("lifesteal")
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Thanks for the quick reply again. I actually tried the same thing (removing the math.random function and the chance local cfg) but I still get the same issue...it heals but not every turn. Even when I deal like 70 damage it should heal by 7, but it does nothing. There's no pattern either, sometimes it heals 3 turns in a row, sometimes it goes 6 or 7 turns without healing. Am I making a mistake somewhere? Also I assume the code above is meant to be two separate scripts yes? You posted it as 1 but it should be one for the weapon and one for the creaturescript right? Or is it really meant to all be in one?

Thanks~
 
Last edited:
Code:
function onTargetCreature(creature, target)
return target:registerEvent("lifesteal")
end

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
local weaponid = 16162 // id of weapon
local chance = 10 // the chance to life steal
local percent = 10 // the percent healed of the attacker
local damage = (primaryDamage + secondaryDamage)

if math.random(100) <= chance then
local weapon = attacker:getSlotItem(CONST_SLOT_LEFT):getId()
if  weapon  and weapon == weaponid then
attacker:addHealth((damage/100) * percent)
end
end
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
@Il Knight your script does heal...but it has the same problem as the one @Xeraphus gave: Even if I set chance to 100 OR remove the math.random function completely, it still does not heal every time. Even if I set the heal% to 100, it still does not heal every time. Is there a storage problem?

Actually I am getting an error with your script:
Code:
data/creaturescripts/scripts/lifesteal.lua:onHealthChange
data/creaturescripts/scripts/lifesteal.lua:12 attempt to call method 'getSlotItem' (a nil value)
stack traceback
[C]: in function 'getSlotItem'
data/creaturescripts/scripts/lifesteal.lua:12 infunction <data/creaturescripts/scripts/lifesteal.lua:5>

I understand that there's something wrong with finding the slot item, but I don't see anything different with your line 5 vs. the other people's line 5. Any ideas?
 
Last edited:
edit @Itutorial hmmm yours doesn't seem to work...is there something else I need to add?

Edit: I forgot, there was errors. The weapon does not attack at all if I use that script, I get the following error:

Lua Script Error: [Weapon Interface]
data/weapons/scripts/thunder hammer.lua:eek:nUseWeapon
data/weapons/scripts/thunder hammer.lua:25 attempt to call global 'getPlayerSkillLevel' (a nil value)
stack traceback
[C]: in function 'getPlayerSkillLevel'
data/weapons/scripts/thunder hammer.lua:25: in function <data/weapons/scripts/thunder hammer.lua:12>
 
Use the TFS 1.2 script not the top one...

Lua:
local combat = createCombatObject()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_DEATHDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_DEATH)

local config = {
    weapon_type = 'sword', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    percent_heal_health = 10 --10 percent is added
}

function onUseWeapon(player, var)
  
    if config.weapon_type == 'sword' then
        SKILL = SKILL_SWORD
    elseif config.weapon_type == 'axe' then
        SKILL = SKILL_AXE
    elseif config.weapon_type == 'club' then
        SKILL = SKILL_CLUB
    elseif config.weapon_type == 'distance' then
        SKILL = SKILL_DISTANCE
    end
  
    if SKILL then
        dmg_min = (player:getSkillLevel(SKILL) * 2) + (player:getLevel())
        dmg_max = (player:getSkillLevel(SKILL) * 2) + (player:getLevel() + 20)
    else
        dmg_min = (player:getMagicLevel() * 2) + (player:getLevel())
        dmg_max = (player:getMagicLevel() * 2) + (player:getLevel() + 20)
    end
  
    dmg_amount = math.random(dmg_min, dmg_max)
    heal_amount = (config.percent_heal_health / 100) * dmg_amount

    target = variantToNumber(var)
    target:addHealth(-dmg_amount)
    player:addHealth(heal_amount)
    doCombat(player, combat, var)
end
 
Last edited:
Back
Top