Solved Lifesteal Melee Weapon [TFS 1.2]

Discussion in 'Support' started by X X X, Mar 18, 2017.

  1. Crevasse

    Crevasse 惡名昭彰

    Joined:
    Jan 13, 2017
    Messages:
    60
    Likes Received:
    8
    Best Answers:
    3
    @Itutorial This script looks sweet! Would I need to change anything at all to get it to work for OTX3?
     
  2. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    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 (Text):
    1. local combat = createCombatObject()
    2. combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    3. combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
    4. local config = {
    5.     weapon_type = 'club', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    6.     percent_heal_health = 10 --10 percent is added
    7. }
    8. function onUseWeapon(player, var)
    9.     if config.weapon_type == 'sword' then
    10.         SKILL = SKILL_SWORD
    11.     elseif config.weapon_type == 'axe' then
    12.         SKILL = SKILL_AXE
    13.     elseif config.weapon_type == 'club' then
    14.         SKILL = SKILL_CLUB
    15.     elseif config.weapon_type == 'distance' then
    16.         SKILL = SKILL_DISTANCE
    17.     end
    18.     if SKILL then
    19.         dmg_min = (player:getSkillLevel(SKILL) * 2) + (player:getLevel())
    20.         dmg_max = (player:getSkillLevel(SKILL) * 2) + (player:getLevel() + 20)
    21.     else
    22.         dmg_min = (player:getMagicLevel() * 2) + (player:getLevel())
    23.         dmg_max = (player:getMagicLevel() * 2) + (player:getLevel() + 20)
    24.     end
    25.     dmg_amount = math.random(dmg_min, dmg_max)
    26.     heal_amount = (config.percent_heal_health / 100) * dmg_amount
    27.     target = variantToNumber(var)
    28.     target:addHealth(-dmg_amount)
    29.     player:addHealth(heal_amount)
    30.     doCombat(player, combat, var)
    31. end
    Thanks so much for the help man!
     
  3. Crevasse

    Crevasse 惡名昭彰

    Joined:
    Jan 13, 2017
    Messages:
    60
    Likes Received:
    8
    Best Answers:
    3
    I'm getting the same issues as OP, healing is inconsistent even though I set chance to 100% or remove the chance function altogether.
     
  4. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Been over 24 hrs, bumping this cuz I can't seem to resolve the errors I'm getting on my own. Thanks
     
  5. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    Code (Lua):
    1.  
    2. local combat = createCombatObject()
    3. combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    4. combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
    5. local config = {
    6.     weapon_type = 'club', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    7.     percent_heal_health = 10 --10 percent is added
    8. }
    9. function onUseWeapon(player, var)
    10.     if config.weapon_type == 'sword' then
    11.         SKILL = SKILL_SWORD
    12.     elseif config.weapon_type == 'axe' then
    13.         SKILL = SKILL_AXE
    14.     elseif config.weapon_type == 'club' then
    15.         SKILL = SKILL_CLUB
    16.     elseif config.weapon_type == 'distance' then
    17.         SKILL = SKILL_DISTANCE
    18.     end
    19.     if SKILL then
    20.         dmg_min = (player:getSkillLevel(SKILL) * 2) + (player:getLevel())
    21.         dmg_max = (player:getSkillLevel(SKILL) * 2) + (player:getLevel() + 20)
    22.     else
    23.         dmg_min = (player:getMagicLevel() * 2) + (player:getLevel())
    24.         dmg_max = (player:getMagicLevel() * 2) + (player:getLevel() + 20)
    25.     end
    26.     dmg_amount = math.random(dmg_min, dmg_max)
    27.     heal_amount = (config.percent_heal_health / 100) * dmg_amount
    28.     target = Creature(variantToNumber(var))
    29.     target:addHealth(-dmg_amount)
    30.     player:addHealth(heal_amount)
    31.     doCombat(player, combat, var)
    32. end
    33.  
     
    X X X likes this.
  6. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Hey thanks for your answer! I will check it when I get home from work. I see you changed

    Code (Text):
    1. target = variantToNumber(var)
    to
    Code (Text):
    1. target = Creature(variantToNumber(var))
    I'll let you know if a new error pops up.
     
    Last edited: Mar 22, 2017
  7. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    Yeah, its going to be something small like that to fix this code. I cant test it unfortunately but...
     
  8. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    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?
     
  9. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    replace the negetive add health with this

    Code (Text):
    1.  
    2. doTargetCombatHealth(0, target, COMBAT_HEALING, -dmgamount, -dmgamount, CONST_ME_MAGIC_RED)
    3.  
    You can set the variables to w/e you want.
     
  10. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Hmm so like this? (last part of the script):

    Code (Text):
    1.  dmg_amount = math.random(dmg_min, dmg_max)
    2.     heal_amount = (config.percent_heal_health / 100) * dmg_amount
    3.     target = Creature(variantToNumber(var))
    4.     player:addHealth(heal_amount)
    5.    doTargetCombatHealth(0, target, COMBAT_HEALING, -dmgamount, -dmgamount, CONST_ME_MAGIC_RED)
    6. end
     
  11. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    should be right
     
  12. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Hmm an error this time:
    Attempt to perform arithmetic on global 'dmgamount' (a nil value)
     
  13. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    change dmgamount to dmg_amount in the script
     
  14. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    Thanks! That did remove the error, but the damage numbers are still not appearing. The monster's health bar just magically disappears :eek:
     
  15. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    Best Answer
    You need to learn some on your own. Here...

    Code (Lua):
    1.  
    2. local combat = createCombatObject()
    3. combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
    4. combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MORTAREA)
    5. local config = {
    6.     weapon_type = 'club', --Options: 'sword' 'axe' 'club' 'distance' 'magic'
    7.     percent_heal_health = 10, --10 percent is added
    8.     combat_type = 'physical', --Options: energy, earth, fire, undefined, lifedrain, manadrain, drown, ice, holy, death
    9.     magicEffect = 12
    10. }
    11. function onUseWeapon(player, var)
    12.     if config.weapon_type == 'sword' then
    13.         SKILL = SKILL_SWORD
    14.     elseif config.weapon_type == 'axe' then
    15.         SKILL = SKILL_AXE
    16.     elseif config.weapon_type == 'club' then
    17.         SKILL = SKILL_CLUB
    18.     elseif config.weapon_type == 'distance' then
    19.         SKILL = SKILL_DISTANCE
    20.     end
    21.    
    22.     if config.combat_type == 'energy' then
    23.         COMBAT = COMBAT_ENERGYDAMAGE
    24.     elseif config.combat_type == 'earth' then
    25.         COMBAT = COMBAT_EARTHDAMAGE
    26.     elseif config.combat_type == 'fire' then
    27.         COMBAT = COMBAT_FIREDAMAGE
    28.     elseif config.combat_type == 'undefined' then
    29.         COMBAT = COMBAT_UNDEFINEDDAMAGE
    30.     elseif config.combat_type == 'lifedrain' then
    31.         COMBAT = COMBAT_LIFEDRAIN
    32.     elseif config.combat_type == 'manadrain' then
    33.         COMBAT = COMBAT_MANADRAIN
    34.     elseif config.combat_type == 'drown' then
    35.         COMBAT = COMBAT_DROWNDAMAGE
    36.     elseif config.combat_type == 'ice' then
    37.         COMBAT = COMBAT_ICEDAMAGE
    38.     elseif config.combat_type == 'holy' then
    39.         COMBAT = COMBAT_HOLYDAMAGE
    40.     elseif config.combat_type == 'death' then
    41.         COMBAT = COMBAT_DEATHDAMAGE
    42.     elseif config.combat_type == 'physical' then
    43.         COMBAT = COMBAT_PHYSICALDAMAGE
    44.     end
    45.  
    46.     if SKILL then
    47.         dmg_min = (player:getSkillLevel(SKILL) * 2) + (player:getLevel())
    48.         dmg_max = (player:getSkillLevel(SKILL) * 2) + (player:getLevel() + 20)
    49.     else
    50.         dmg_min = (player:getMagicLevel() * 2) + (player:getLevel())
    51.         dmg_max = (player:getMagicLevel() * 2) + (player:getLevel() + 20)
    52.     end
    53.     dmg_amount = math.random(dmg_min, dmg_max)
    54.     heal_amount = (config.percent_heal_health / 100) * dmg_amount
    55.     target = Creature(variantToNumber(var))
    56.     doTargetCombatHealth(0, target, COMBAT, -dmg_amount, -dmg_amount, config.magicEffect)
    57.     player:addHealth(heal_amount)
    58.     doCombat(player, combat, var)
    59.     return true
    60. end
    61.  
     
    SN4KY likes this.
  16. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    I cannot thank you enough. Thank you for your help and patience! I am learning but I just started :)
     
  17. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    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.
     
  18. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    try to change this

    Code (Lua):
    1.  
    2. doTargetCombatHealth(player, target, COMBAT, -dmg_amount, -dmg_amount, config.magicEffect)
    3.  
     
  19. X X X

    X X X Newb

    Joined:
    Jul 26, 2015
    Messages:
    78
    Likes Received:
    3
    Best Answers:
    0
    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.
     
  20. Itutorial

    Itutorial Active Member

    Joined:
    Dec 23, 2014
    Messages:
    949
    Likes Received:
    155
    Best Answers:
    12
    Its the creature id of who is doing the damage/healing. Its set to 0 meaning no one is doing the damage/healing.
     

Share This Page

Loading...