• 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 Tfs 1.4 crit system

MrVilhelm

Mafia Penguin
Joined
Dec 11, 2020
Messages
141
Solutions
1
Reaction score
113
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 =...
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
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
 
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
 
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
 
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
 
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! ā¤ļø
 
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
No, i see @Xikini posted correct solution, but the answer why its at very top of the script its because of returns, return mean that rest of function will be skiped, in this case it check if event has callback, if yes it return to this callback, if not it return true so your line
Lua:
self:registerEvent("CritSystem")
will never be executed.
 
Back
Top