• 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...
Hey @Itutorial that script gave me a similar error:

Lua Script Error: [Weapon Interface]
data/weapons/scripts/thunder hammer.lua: onUseWeapon
data/weapons/scripts/thunder hammer.lua:34 attempt to index global 'target' (a number value)
stack traceback
[C]: in function '_index'
data/weapons/scripts/thunder hammer.lua:34: in function <data/weapons/scripts/thunder hammer.lua:10>

Heres my script (I just changed the damage type and removed the distance effect):
Code:
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
}
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

Thanks so much for the help man!
 
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
}
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 = Creature(variantToNumber(var))
    target:addHealth(-dmg_amount)
    player:addHealth(heal_amount)
    doCombat(player, combat, var)
end
 
Hey thanks for your answer! I will check it when I get home from work. I see you changed

Code:
target = variantToNumber(var)
to
Code:
target = Creature(variantToNumber(var))

I'll let you know if a new error pops up.
 
Last edited:
Hey so there's no more error in the console, and it works...kind of. So it heals the user (good) and does damage to the target (good) but no damage numbers appear xD I think because we are using a negative addHealth function so it's just subtracting the monster's HP and not actually dealing damage? Any ideas?
 
replace the negetive add health with this

Code:
doTargetCombatHealth(0, target, COMBAT_HEALING, -dmgamount, -dmgamount, CONST_ME_MAGIC_RED)

You can set the variables to w/e you want.
 
Hmm so like this? (last part of the script):

Code:
 dmg_amount = math.random(dmg_min, dmg_max)
    heal_amount = (config.percent_heal_health / 100) * dmg_amount
    target = Creature(variantToNumber(var))
    player:addHealth(heal_amount)
   doTargetCombatHealth(0, target, COMBAT_HEALING, -dmgamount, -dmgamount, CONST_ME_MAGIC_RED)
end
 
Thanks! That did remove the error, but the damage numbers are still not appearing. The monster's health bar just magically disappears :eek:
 
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
 
Solution
So the script provided by @Itutorial works awesome, however the player is not being recognized as the killer, so when a monster is killed with this weapon, no exp is earned.

The game is treating it the same way it would when a monster steps on a jungle maw or a neutral magic field -- it dies but no one gains exp.

Do I need to add an additional onKill function to the script? As always any help is appreciated. Thank you.
 
try to change this

Lua:
doTargetCombatHealth(player, target, COMBAT, -dmg_amount, -dmg_amount, config.magicEffect)
 
Thank you for your reply! Changing "0" to "player yes? I will try it thank you. Just curious, what does the zero mean in the first place? I've never see that in the variables before.
 
Its the creature id of who is doing the damage/healing. Its set to 0 meaning no one is doing the damage/healing.
 
Back
Top