• 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...
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
    elseif config.weapon_type == 'distance' then
        SKILL = SKILL_DISTANCE
    end
  
    if config.combat_type == 'energy' then
        COMBAT = COMBAT_ENERGYDAMAGE
    elseif config.combat_type == 'earth' then
        COMBAT = COMBAT_EARTHDAMAGE
    elseif config.combat_type == 'fire' then
        COMBAT = COMBAT_FIREDAMAGE
    elseif config.combat_type == 'undefined' then
        COMBAT = COMBAT_UNDEFINEDDAMAGE
    elseif config.combat_type == 'lifedrain' then
        COMBAT = COMBAT_LIFEDRAIN
    elseif config.combat_type == 'manadrain' then
        COMBAT = COMBAT_MANADRAIN
    elseif config.combat_type == 'drown' then
        COMBAT = COMBAT_DROWNDAMAGE
    elseif config.combat_type == 'ice' then
        COMBAT = COMBAT_ICEDAMAGE
    elseif config.combat_type == 'holy' then
        COMBAT = COMBAT_HOLYDAMAGE
    elseif config.combat_type == 'death' then
        COMBAT = COMBAT_DEATHDAMAGE
    elseif config.combat_type == 'physical' then
        COMBAT = COMBAT_PHYSICALDAMAGE
    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 = Creature(variantToNumber(var))
    doTargetCombatHealth(0, target, COMBAT, -dmg_amount, -dmg_amount, config.magicEffect)
    player:addHealth(heal_amount)
    doCombat(player, combat, var)
    return true
end

How can I make that the healing is calculated from the normal damage of a weapon, so I can use it on any weapon without adding a damage formula.
 
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
would you like to do it for tfs 0.3 6
error :

[Error - Weapon Interface]
data/weapons/scripts/weapon.lua
Description:
data/weapons/scripts/weapon.lua:5: attempt to call global 'Combat' (a nil value)
[Warning - Event::loadScript] Cannot load script (data/weapons/scripts/weapon.lua)

[Error - CreatureEvent::configureEvent] No valid type for creature event.healthchange
[Warning - BaseEvents::loadFromXml] Cannot configure an event
 
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
is there a way to do it for tfs 0.3.6 ?
 
is there a way to do it for tfs 0.3.6 ?
would you like to do it for tfs 0.3 6
To answer your questions:
There is a way, but I will not be the one to write it for a very outdated engine. I also haven't written anything TFS related in about 4 years so I'm very out of practice regardless.
The concepts of the two engines aren't that different, if you can just read documentation, learn how to Google/use the search feature on here efficiently and dedicate a little time to learning Lua you'll be able to do it yourself. It will also be much faster to learn the language to create things yourself rather than relying on waiting for somebody on here to come around out of the goodness of their heart and make features for you. I learned this lesson the hard way if you dig all the way back in my own threads, I was in this exact same position.
 
To answer your questions:
There is a way, but I will not be the one to write it for a very outdated engine. I also haven't written anything TFS related in about 4 years so I'm very out of practice regardless.
The concepts of the two engines aren't that different, if you can just read documentation, learn how to Google/use the search feature on here efficiently and dedicate a little time to learning Lua you'll be able to do it yourself. It will also be much faster to learn the language to create things yourself rather than relying on waiting for somebody on here to come around out of the goodness of their heart and make features for you. I learned this lesson the hard way if you dig all the way back in my own threads, I was in this exact same position.
ok. I learned how to make chatgpt correct the codes and even transform them.
 
Back
Top