• 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!
  • If you're using Gesior 2012 or MyAAC, please review this thread for information about a serious security vulnerability and a fix.

Lua Tfs 1.4 crit system

MrVilhelm

Mafia Penguin
Joined
Dec 11, 2020
Messages
140
Solutions
1
Reaction score
108
Location
Sweden
GitHub
MrVilhelm
Hola amigos! šŸ„

Im fairly new to Lua and Im trying to rap my head around how the TFS functions work etc.
So I was planning on adding a simple crit chance + crit damage system. So I thought that was a good place to start!

But Im running into some problems (error free).

I wrote this in CritSystem.lua
Lua:
local config = {
    effect = 173,
    multiplier = 10,
    chance = math.random(10,0)
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)

    if(config.chance > 5) then
        creature:getPosition().sendMagicEffect(config.effect)
        return primaryDamage * config.multiplier, primaryType, secondaryDamage, secondaryType
    end
end

And this in creaturescripts.xml
XML:
<event type="healthchange" name="CritSystem" script="CritSystem.lua" />

Finally, this in login.lua as I searched around a lil for how other people made the crit system and found THIS script made by @Mackerel
Lua:
player:registerEvent("CritSystem")

But it does not seem to do anything. Not giving any errors nor giving any crit chance or damage.
Where am I going wrong? o_O

All help appreciated! <3
 
Solution
chance = math.random(10,0)

This is outside the scope of function, so will only randomise once.
Put math.random inside functions if you want them to randomise more then once.

Further, the math.random function requires the first number to be less then the 2nd number, otherwise it doesn't have an acceptable range to use. So math.random(10, 0) is invalid, and should cause an error.

creature:getPosition().sendMagicEffect(config.effect) this is incorrect, and will error (change . to :)

primary and secondary damage should both receive multiplier
return primaryDamage * config.multiplier, primaryType, secondaryDamage, secondaryType

-----

Try this
Lua:
local config = {
    effect = 173,
    multiplier =...

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
6,107
Solutions
506
Reaction score
4,274
chance = math.random(10,0)

This is outside the scope of function, so will only randomise once.
Put math.random inside functions if you want them to randomise more then once.

Further, the math.random function requires the first number to be less then the 2nd number, otherwise it doesn't have an acceptable range to use. So math.random(10, 0) is invalid, and should cause an error.

creature:getPosition().sendMagicEffect(config.effect) this is incorrect, and will error (change . to :)

primary and secondary damage should both receive multiplier
return primaryDamage * config.multiplier, primaryType, secondaryDamage, secondaryType

-----

Try this
Lua:
local config = {
    effect = 173,
    multiplier = 10,
    chance = 10 -- chance/100
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if math.random(100) <= config.chance then
        if creature then
            creature:getPosition():sendMagicEffect(config.effect)
        end
        return primaryDamage * config.multiplier, primaryType, secondaryDamage * config.multiplier, secondaryType
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
 
Solution
OP
OP
MrVilhelm

MrVilhelm

Mafia Penguin
Joined
Dec 11, 2020
Messages
140
Solutions
1
Reaction score
108
Location
Sweden
GitHub
MrVilhelm
chance = math.random(10,0)

This is outside the scope of function, so will only randomise once.
Put math.random inside functions if you want them to randomise more then once.

Further, the math.random function requires the first number to be less then the 2nd number, otherwise it doesn't have an acceptable range to use. So math.random(10, 0) is invalid, and should cause an error.

creature:getPosition().sendMagicEffect(config.effect) this is incorrect, and will error (change . to :)

primary and secondary damage should both receive multiplier
return primaryDamage * config.multiplier, primaryType, secondaryDamage, secondaryType

-----

Try this
Lua:
local config = {
    effect = 173,
    multiplier = 10,
    chance = 10 -- chance/100
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if math.random(100) <= config.chance then
        if creature then
            creature:getPosition():sendMagicEffect(config.effect)
        end
        return primaryDamage * config.multiplier, primaryType, secondaryDamage * config.multiplier, secondaryType
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
Thanks for your response! You really helped me understand the script. But this does not seem to work either. I tried putting the chance to 100 just to see if it worked but still no effect nor extra dmg

Edit, Im just a slow thinker. Worked like a charm. Expect for players, monsters do crit dmg now but players do not
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
6,107
Solutions
506
Reaction score
4,274
Thanks for your response! You really helped me understand the script. But this does not seem to work either. I tried putting the chance to 100 just to see if it worked but still no effect nor extra dmg
onHealthChange triggers on the creature that is receiving the damage.

Currently you have it setup to work on players only. (because only players are being registered with the onHealthChange script)

if you want it to work on creatures as well, you'll need to register the creaturescript to monsters.

Easiest way would be to trigger in onSpawn. data/events/scripts/monster.lua
Inside the onSpawn portion..
add
self:registerEvent("CritSystem")

and inside data/events/events.xml make sure that
<event class="Monster" method="onSpawn" enabled="0" />
is enabled. change 0 to 1
 
OP
OP
MrVilhelm

MrVilhelm

Mafia Penguin
Joined
Dec 11, 2020
Messages
140
Solutions
1
Reaction score
108
Location
Sweden
GitHub
MrVilhelm
onHealthChange triggers on the creature that is receiving the damage.

Currently you have it setup to work on players only. (because only players are being registered with the onHealthChange script)

if you want it to work on creatures as well, you'll need to register the creaturescript to monsters.

Easiest way would be to trigger in onSpawn. data/events/scripts/monster.lua
Inside the onSpawn portion..
add
self:registerEvent("CritSystem")

and inside data/events/events.xml make sure that
<event class="Monster" method="onSpawn" enabled="0" />
is enabled. change 0 to 1
Should monster.lua look like this?
Lua:
function Monster:onDropLoot(corpse)
    if hasEventCallback(EVENT_CALLBACK_ONDROPLOOT) then
        EventCallback(EVENT_CALLBACK_ONDROPLOOT, self, corpse)
    end
end

function Monster:onSpawn(position, startup, artificial)
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
    self:registerEvent("CritSystem")
end
 

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
6,107
Solutions
506
Reaction score
4,274
Should monster.lua look like this?
Lua:
function Monster:onDropLoot(corpse)
    if hasEventCallback(EVENT_CALLBACK_ONDROPLOOT) then
        EventCallback(EVENT_CALLBACK_ONDROPLOOT, self, corpse)
    end
end

function Monster:onSpawn(position, startup, artificial)
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
    self:registerEvent("CritSystem")
end
Lua:
function Monster:onSpawn(position, startup, artificial)
    self:registerEvent("CritSystem")
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
end
 
OP
OP
MrVilhelm

MrVilhelm

Mafia Penguin
Joined
Dec 11, 2020
Messages
140
Solutions
1
Reaction score
108
Location
Sweden
GitHub
MrVilhelm
Lua:
function Monster:onSpawn(position, startup, artificial)
    self:registerEvent("CritSystem")
    if hasEventCallback(EVENT_CALLBACK_ONSPAWN) then
        return EventCallback(EVENT_CALLBACK_ONSPAWN, self, position, startup, artificial)
    else
        return true
    end
end
Ohh, yea I have a lot to learn! Thanks a lot for explaining everything + helping me! Much love! ā¤ļø
 
Top