• 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 Multiple damage system request

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
Hello guys,

I would like a system that the player has a chance to do a second damage. For example, the player is killing a enemy and is using a weapon that has 10% chance of having multiple damage. When the multiple damage chance happens, the player will hit the enemy twice with no cooldown but it will show 2 damages, not the sum of the both damages.
I would like for this multiple damage to be applied to specific weapons (set by item ID) and the % chance of multiple damage happen, will be different for each weapon.

TFS 1.3
 
Last edited:
Solution
What are the risks of a unsafe script? The worst case would shut down the server?
To be honest, I don't really know. I guess so.

This works for me:

Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SPEAR)
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)

function onGetFormulaValues(player, skill, attack, factor)
    local maxHealth = player:getMaxHealth()
    local maxMana = player:getMaxMana()
    local playlvl = math.min(player:getLevel(), 300)
    local dmg = -playlvl - (maxHealth + maxMana) / 16 - attack * 4 - skill * 2
    return dmg * 0.5, dmg
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

local...

Musaab

NoxOts
Premium User
Joined
May 13, 2010
Messages
176
Reaction score
74
Location
Sweden
Hey!

I dont know exactly how that would apply to your version but i have something similiar to one of my spells

Lua:
function spell14.onCastSpell(creature, variant)
    if math.random(1, 100) <= 70 then  --- 70% chance that the spell will hit a second time
        addEvent(secondSpell, 500, creature:getId(), variant)
    end
    combat:execute(creature, variant)
    return true
end

Maybe u know how to alter it to ur version,

Also dont forget to mention what TFS version ur using to get help faster :]

Good luck!
 
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
Hey!

I dont know exactly how that would apply to your version but i have something similiar to one of my spells

Lua:
function spell14.onCastSpell(creature, variant)
    if math.random(1, 100) <= 70 then  --- 70% chance that the spell will hit a second time
        addEvent(secondSpell, 500, creature:getId(), variant)
    end
    combat:execute(creature, variant)
    return true
end

Maybe u know how to alter it to ur version,

Also dont forget to mention what TFS version ur using to get help faster :]

Good luck!
I forgot about the version, thanks for reminder me. Thanks, I will try to change those things to work with my version.
 
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
I didn't manage to do the script. The best I could do was this:

local firstcombat = Combat()
setCombatParam(firstcombat, COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
setCombatParam(firstcombat, COMBAT_PARAM_EFFECT, CONST_ME_SOUND_WHITE)
setCombatParam(firstcombat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_HUNTINGSPEAR)

function onGetFormulaValues(player, skill, attack)

local playlvl = player:getLevel()

if (playlvl<300) then
local min = (player:getLevel() / 2) + (skill * 2) + (attack * 3)
local max = (player:getLevel() / 2) + (skill * 2) + (attack * 3) * 1,5
return -min, -max

else
local min = (300 / 2) + (skill * 2) + (attack * 3)
local max = (300 / 2) + (skill * 2) + (attack * 3) * 1,5
return -min, -max
end
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

local area = createCombatArea( { {0, 0, 0}, {0, 3, 0}, {0, 0, 0} } )
setCombatArea(combat, area)

local secondcombat = Combat()
setCombatParam(secondcombat, COMBAT_PARAM_TYPE, COMBAT_ICEDAMAGE)
setCombatParam(secondcombat, COMBAT_PARAM_EFFECT, CONST_ME_SOUND_WHITE)
setCombatParam(secondcombat, COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_HUNTINGSPEAR)

function onGetFormulaValues(player, skill, attack)

local playlvl = player:getLevel()

if (playlvl<300) then
local min = (player:getLevel() / 2) + (skill * 2) + (attack * 3)
local max = (player:getLevel() / 2) + (skill * 2) + (attack * 3) * 1,5
return -min, -max

else
local min = (300 / 2) + (skill * 2) + (attack * 3)
local max = (300 / 2) + (skill * 2) + (attack * 3) * 1,5
return -min, -max
end
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

local area = createCombatArea( { {0, 0, 0}, {0, 3, 0}, {0, 0, 0} } )
setCombatArea(combat, area)

function onUseWeapon(cid, variant)
if math.random(1, 100) <= 70 then
dosecondCombat(cid, combat, variant)
return dofirstCombat(cid, combat, variant)
end[/CODE]

It's a weapon script, it work, deal normal damage and does not have any error on TFS, but the multiple shot ain't working. I also tried to put "addevent" on the script, but it didn't work. "addevent" don't work on weapons?
 

oen432

Legendary OT User
Joined
Oct 3, 2014
Messages
1,452
Solutions
49
Reaction score
1,545
Location
Poland
GitHub
Oen44
Am I going the right way with the script? For it to work I just need to manage to put the addevent?
No. You are using 2 functions that don't exist, you are mixing old syntax with new one which makes me want to leave my body. Also pay attention to formatting code in your post.
 
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
No. You are using 2 functions that don't exist, you are mixing old syntax with new one which makes me want to leave my body. Also pay attention to formatting code in your post.
I'm very noob with scripts. I usually try to look a similar script and copy and edit some parts.
I think I should have done this:
Lua:
function onUseWeapon(cid, variant)
if math.random(1, 100) <= 70 then
doCombat(cid, secondcombat, variant)
return doCombat(cid, firstcombat, variant)
end
 
Last edited:

amatria

Active Member
Joined
May 15, 2021
Messages
45
Solutions
4
Reaction score
42
Location
Spain
GitHub
amatria
I haven't done thorough testing, but the following snippet may do the job:

Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SPEAR)
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)
combat:setFormula(COMBAT_FORMULA_SKILL, 0, 0, 1, 0)

local function doCombat(player, variant, repeatEvent)
    if not player then
        return false
    end
    if repeatEvent and math.random(1, 100) < 50 then
        addEvent(doCombat, 250, player, variant, false)
    end
    combat:execute(player, variant)
    return true
end

function onUseWeapon(player, variant)
    return doCombat(player, variant, true)
end

However, I'm pretty sure TFS will warn you about it being an unsafe script. It is not okay to pass a player metatable as an addEvent parameter.

Edit
===
 
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
local combat = Combat() combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE) combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SPEAR) combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true) combat:setFormula(COMBAT_FORMULA_SKILL, 0, 0, 1, 0) local function doCombat(player, variant, repeatEvent) if not player then return false end if repeatEvent and math.random(1, 100) < 50 then addEvent(doCombat, 250, player, variant, false) end combat:execute(player, variant) return true end function onUseWeapon(player, variant) return doCombat(player, variant, true) end
It work perfectly, but the TFS did warn me about the unsafe script:
Lua Script Error: [Weapon Interface]
data/weapons/scripts/heroicbow.lua:eek:nUseWeapon
LuaScriptInterface::luaAddEvent(). Argument #3 is unsafe
stack traceback:
[C]: in function 'addEvent'
data/weapons/scripts/heroicbow.lua:12: in function <data/weapons/scripts/heroicbow.lua:7>
Can you suggest a way to fix it?
 

amatria

Active Member
Joined
May 15, 2021
Messages
45
Solutions
4
Reaction score
42
Location
Spain
GitHub
amatria
Can you suggest a way to fix it?
To remove the warning you should avoid passing a player metatable as a parameter to the addEvent procedure. However, I can't think of a solution that does not involve doing exactly that. Let's see if some other user with more experience than me comes up with a better solution. In the meanwhile, although it is discouraged, you can modify your config.lua file and set warnUnsafeScripts to false to get rid of that message.
 
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
To remove the warning you should avoid passing a player metatable as a parameter to the addEvent procedure. However, I can't think of a solution that does not involve doing exactly that. Let's see if some other user with more experience than me comes up with a better solution. In the meanwhile, although it is discouraged, you can modify your config.lua file and set warnUnsafeScripts to false to get rid of that message.
What are the risks of a unsafe script? The worst case would shut down the server? I'm on develop fase on my server still, so set warnUnsafeScript to false ain't a bad idea, I did it and no more errors on TFS. Now let's say I would like to add a more complex damage formula for the script you made, how would I do it?

I tried to put the function that I used before, but it deals no damage to the creature.

Lua:
function onGetFormulaValues(player, skill, attack, skill, factor)

    local maxHealth = player:getMaxHealth()
    local maxMana = player:getMaxMana()
    local playlvl = player:getLevel()

    if (playlvl<300) then
    local min = ((player:getLevel()) + ((maxHealth + maxMana) / 16) + (attack * 4) + (skill * 2)) * 0,5
    local max = ((player:getLevel()) + ((maxHealth + maxMana) / 16) + (attack * 4) + (skill * 2)) * 1,0
    return -min, -max
    
    else
    local min = ((300) + ((maxHealth + maxMana) / 16) + (attack * 4) + (skill * 2)) * 0,5
    local max = ((300) + ((maxHealth + maxMana) / 16) + (attack * 4) + (skill * 2)) * 1,0
    return -min, -max
    end   
end
combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
 

amatria

Active Member
Joined
May 15, 2021
Messages
45
Solutions
4
Reaction score
42
Location
Spain
GitHub
amatria
What are the risks of a unsafe script? The worst case would shut down the server?
To be honest, I don't really know. I guess so.

This works for me:

Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SPEAR)
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)

function onGetFormulaValues(player, skill, attack, factor)
    local maxHealth = player:getMaxHealth()
    local maxMana = player:getMaxMana()
    local playlvl = math.min(player:getLevel(), 300)
    local dmg = -playlvl - (maxHealth + maxMana) / 16 - attack * 4 - skill * 2
    return dmg * 0.5, dmg
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

local function doCombat(player, variant, repeatEvent)
    if not player then
        return false
    end
    if repeatEvent and math.random(1, 100) < 50 then
        addEvent(doCombat, 250, player, variant, false)
    end
    combat:execute(player, variant)
    return true
end

function onUseWeapon(player, variant)
    return doCombat(player, variant, true)
end
 
Solution
OP
OP
I

Icaraii

Active Member
Joined
Jan 5, 2020
Messages
295
Solutions
1
Reaction score
37
To be honest, I don't really know. I guess so.

This works for me:

Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_SPEAR)
combat:setParameter(COMBAT_PARAM_BLOCKARMOR, true)

function onGetFormulaValues(player, skill, attack, factor)
    local maxHealth = player:getMaxHealth()
    local maxMana = player:getMaxMana()
    local playlvl = math.min(player:getLevel(), 300)
    local dmg = -playlvl - (maxHealth + maxMana) / 16 - attack * 4 - skill * 2
    return dmg * 0.5, dmg
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

local function doCombat(player, variant, repeatEvent)
    if not player then
        return false
    end
    if repeatEvent and math.random(1, 100) < 50 then
        addEvent(doCombat, 250, player, variant, false)
    end
    combat:execute(player, variant)
    return true
end

function onUseWeapon(player, variant)
    return doCombat(player, variant, true)
end
Thanks mate, that works very well, just what I was looking for.
 
Top