• 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!

[TFS 1.2] Need Critical hit system

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
Hi
maybe someone have critical system script? That works like this, lets say you have "10 sword fighting you have 1% chance to hit critical hit", "20 sword fighting you have 2% chance to hit critical hit", "30 sword fighting you have 3% chance to hit critical hit" and etc and when you hit critical hit it sends some kind of magic effect and thats it.
 
Solution

Try this:

1. Create file critical_hit_system.lua in data\creaturescripts\scripts and add the following:

Lua:
local config = {
    magic_effect = 15, -- magic effect you want to send when critical hit lands
    damage_multiplier = 10 -- default damage * 10 = critical damage
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if attacker == nil then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
 
    if not attacker:isPlayer() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
 
    local skill = attacker:getEffectiveSkillLevel(SKILL_SWORD)
    local chance = (skill...
Hi
maybe someone have critical system script? That works like this, lets say you have "10 sword fighting you have 1% chance to hit critical hit", "20 sword fighting you have 2% chance to hit critical hit", "30 sword fighting you have 3% chance to hit critical hit" and etc and when you hit critical hit it sends some kind of magic effect and thats it.
Just have to know what to look for... or at least how to word your search term/phrase.
[TFS 1.0] Critical Hit % - Permanent

Of course I've seen this thread before but was unsure of where to find it.
I searched in google "evan otland onhealthchange"

You'll need to upgrade the script/examples but I am sure if you read the entire thread adapting it to 1.2 won't be an issue.
 
Just have to know what to look for... or at least how to word your search term/phrase.
[TFS 1.0] Critical Hit % - Permanent

Of course I've seen this thread before but was unsure of where to find it.
I searched in google "evan otland onhealthchange"

You'll need to upgrade the script/examples but I am sure if you read the entire thread adapting it to 1.2 won't be an issue.
Yea he explained everything perfectly. But i'm looking to this script
Code:
[LIST=1]
[*]function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
[*]    if math.random(100) <= 50 then
[*]        if origin == ORIGIN_MELEE then
[*]            creature:say("SKULLBASH!", TALKTYPE_MONSTER_SAY)
[*]        elseif origin == ORIGIN_RANGED then
[*]            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
[*]        end
[*]        return primaryDamage * 10, primaryType, secondaryDamage, secondaryType
[*]    end
[*]

[*]    return primaryDamage, primaryType, secondaryDamage, secondaryType
[*]end
[/LIST]
It hits randomly it doesnt look at the sword fight parameter like i mentioned at the top so i guess thats the hardest part to make. Because this script hits random critical hit that multiplied by *10
 
I'm not sure if this works:


Lua:
if(not attacker:isPlayer()) then return end
 
local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                end
-- add the others
                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
-- add the rest of the code you already posted.
 
I'm not sure if this works:


Lua:
if(not attacker:isPlayer()) then return end
 
local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                end
-- add the others
                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
-- add the rest of the code you already posted.
Maybe can you explain how this script works? I want to know because it looks pretty weird lets say CONST_SLOT_LEFT so its slot of weapon? So what if its two weapon system, just asking but im not using two weapon system i'm just wondering how this code works. I'll test it soon does it work.
 
Maybe can you explain how this script works? I want to know because it looks pretty weird lets say CONST_SLOT_LEFT so its slot of weapon? So what if its two weapon system, just asking but im not using two weapon system i'm just wondering how this code works. I'll test it soon does it work.
CONST_SLOT_LEFT and CONST_SLOT_RIGHT are the hand slots
 
About error from console dunno why it gives this kind of error so this is the whole code
Code:
if(not attacker:isPlayer()) then return end

local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                end
-- add the others
                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
end

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if math.random(100) <= 50 then
        if origin == ORIGIN_MELEE then
            creature:say("SKULLBASH!", TALKTYPE_MONSTER_SAY)
        elseif origin == ORIGIN_RANGED then
            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
        end
        return primaryDamage * 10, primaryType, secondaryDamage, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
And this is the error? So i'm assume i get this error because of different tfs version? But i'm not sure.
Untitled.png
 
Lua:
local threshold = 80

local slots = {
    CONST_SLOT_RIGHT,
    CONST_SLOT_LEFT
}

local weapons = {
    [2383] = { chance = 50, multiplier = 2} -- spiked sword
    [2392] = { chance = 5, multiplier = 3}, -- fire sword
}

function getSlottedItems(cid)
    local player = Player(cid)
    local equippedItems = {}
    if player then
        for i = 1, #slots do
            local id = pushThing(player:getSlotItem(slots[i]).itemid
            equippedItems[id] = ItemType(id):getWeaponType()
        end
    end
    return equippedItems
end


function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local items = getSlottedItems(creature:getId())
    local bonus = {chance = 0, multiplier = 0}
    if items and next(items) then
       for itemid, weaponType in pairs(items) do
            if weapons[itemid] and next(weapons[itemid]) then
                bonus.chance = bonus.chance + weapons[itemid].chance
                bonus.multiplier = bonus.multiplier + weapons[itemid].multiplier
            end
       end
    end

    if math.random(bonus.chance) >= threshold then
        primaryDamage = primaryDamage ~= 0 and primaryDamage * bonus.multiplier or 0
        secondaryDamage = secondaryDamage ~= 0 and secondaryDamage * bonus.multiplier or 0
    end
 
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

You can swap out creature for attacker if you like where it says creature:getId(), just do attacker:getId() instead.
 
Last edited:
Lua:
local threshold = 80

local slots = {
    CONST_SLOT_RIGHT,
    CONST_SLOT_LEFT
}

local weapons = {
    [2383] = { chance = 50, multiplier = 2} -- spiked sword
    [2392] = { chance = 5, multiplier = 3}, -- fire sword
}

function getSlottedItems(cid)
    local player = Player(cid)
    local equippedItems = {}
    if player then
        for i = 1, #slots do
            local id = pushThing(player:getSlotItem(slots[i]).itemid
            equippedItems[id] = ItemType(id):getWeaponType()
        end
    end
    return equippedItems
end


function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local items = getSlottedItems(creature:getId())
    local bonus = {chance = 0, multiplier = 0}
    if items and next(items) then
       for itemid, weaponType in pairs(items) do
            if weapons[itemid] and next(weapons[itemid]) then
                bonus.chance = bonus.chance + weapons[itemid].chance
                bonus.multiplier = bonus.multiplier + weapons[itemid].multiplier
            end
       end
    end

    if math.random(bonus.chance) >= threshold then
        primaryDamage = primaryDamage ~= 0 and primaryDamage * bonus.multiplier or 0
        secondaryDamage = secondaryDamage ~= 0 and secondaryDamage * bonus.multiplier or 0
    end
 
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

You can swap out creature for attacker if you like where it says creature:getId(), just do attacker:getId() instead.
This code work sword principle? Like lets say sword with 2383id has 50% chance to deal crit dmg?
 
Your post deserve a like but not a best answer yet :D
The code is currently written for any item that is stored in your hands.. however it will read whatever slots you put in the slots table. In order for it to respond with critical damage you must be using 1 of the items listed in the weapons table when the damage is done, since there is a threshold and a random chance of that item it must be higher than the threshold which you can adjust... Now as I said before switch creature:getId() with attacker:getId()

I switched it for you.
Lua:
local threshold = 80
local slots = {
    CONST_SLOT_RIGHT,
    CONST_SLOT_LEFT
}
local weapons = {
    [2383] = { chance = 50, multiplier = 2} -- spiked sword
    [2392] = { chance = 5, multiplier = 3}, -- fire sword
}
function getSlottedItems(cid)
    local player = Player(cid)
    local equippedItems = {}
    if player then
        for i = 1, #slots do
            local id = pushThing(player:getSlotItem(slots[i]).itemid
            equippedItems[id] = ItemType(id):getWeaponType()
        end
    end
    return equippedItems
end
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local items = getSlottedItems(attacker:getId())
    local bonus = {chance = 0, multiplier = 0}
    if items and next(items) then
       for itemid, weaponType in pairs(items) do
            if weapons[itemid] and next(weapons[itemid]) then
                bonus.chance = bonus.chance + weapons[itemid].chance
                bonus.multiplier = bonus.multiplier + weapons[itemid].multiplier
            end
       end
    end
    if math.random(bonus.chance) >= threshold then
        primaryDamage = primaryDamage ~= 0 and primaryDamage * bonus.multiplier or 0
        secondaryDamage = secondaryDamage ~= 0 and secondaryDamage * bonus.multiplier or 0
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

This script will only work if a player is an attacker, of course you don't have to actually physically strike the creature. You just have to have any of the items or all the items in the weapons table... the more the items.. the better your chances that you will critical strike.. remember the multiplier is also increased.

If you want more explicit or fine tuned then I can't help you, this was just an example.. its just a bad habit I have and I want to break from writing complete scripts for people.
 
The code is currently written for any item that is stored in your hands.. however it will read whatever slots you put in the slots table. In order for it to respond with critical damage you must be using 1 of the items listed in the weapons table when the damage is done, since there is a threshold and a random chance of that item it must be higher than the threshold which you can adjust... Now as I said before switch creature:getId() with attacker:getId()

I switched it for you.
Lua:
local threshold = 80
local slots = {
    CONST_SLOT_RIGHT,
    CONST_SLOT_LEFT
}
local weapons = {
    [2383] = { chance = 50, multiplier = 2} -- spiked sword
    [2392] = { chance = 5, multiplier = 3}, -- fire sword
}
function getSlottedItems(cid)
    local player = Player(cid)
    local equippedItems = {}
    if player then
        for i = 1, #slots do
            local id = pushThing(player:getSlotItem(slots[i]).itemid
            equippedItems[id] = ItemType(id):getWeaponType()
        end
    end
    return equippedItems
end
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    local items = getSlottedItems(attacker:getId())
    local bonus = {chance = 0, multiplier = 0}
    if items and next(items) then
       for itemid, weaponType in pairs(items) do
            if weapons[itemid] and next(weapons[itemid]) then
                bonus.chance = bonus.chance + weapons[itemid].chance
                bonus.multiplier = bonus.multiplier + weapons[itemid].multiplier
            end
       end
    end
    if math.random(bonus.chance) >= threshold then
        primaryDamage = primaryDamage ~= 0 and primaryDamage * bonus.multiplier or 0
        secondaryDamage = secondaryDamage ~= 0 and secondaryDamage * bonus.multiplier or 0
    end
    return primaryDamage, primaryType, secondaryDamage, secondaryType
end

This script will only work if a player is an attacker, of course you don't have to actually physically strike the creature. You just have to have any of the items or all the items in the weapons table... the more the items.. the better your chances that you will critical strike.. remember the multiplier is also increased.

If you want more explicit or fine tuned then I can't help you, this was just an example.. its just a bad habit I have and I want to break from writing complete scripts for people.
I'm not offended its fine. But how does it have to do anything with SKILL_SWORD parameter or any other skill? It was the whole point of this thread.
 
About error from console dunno why it gives this kind of error so this is the whole code
Code:
if(not attacker:isPlayer()) then return end

local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                end
-- add the others
                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
end

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if math.random(100) <= 50 then
        if origin == ORIGIN_MELEE then
            creature:say("SKULLBASH!", TALKTYPE_MONSTER_SAY)
        elseif origin == ORIGIN_RANGED then
            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
        end
        return primaryDamage * 10, primaryType, secondaryDamage, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end
And this is the error? So i'm assume i get this error because of different tfs version? But i'm not sure.
Untitled.png

Actually you put the code in the wrong place

Code:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    
if (not attacker:isPlayer()) then return end 

local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                elseif weaponType == WEAPON_CLUB then
                                     skill = SKILL_CLUB
                                elseif weaponType == WEAPON_AXE then
                                     skill = SKILL_AXE
                                end

                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
        if origin == ORIGIN_MELEE then
            creature:say("SKULLBASH!", TALKTYPE_MONSTER_SAY)
        elseif origin == ORIGIN_RANGED then
            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
        end
        return primaryDamage * 10, primaryType, secondaryDamage, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end


I can't test (I'm without any ot on my laptop, and I'm writing this on my phone now)
How it should work:the first if check if the attacker is a player.

The second if checks if you have an item equipped in the left hand slot, and then, reserves the type of weapon in a variable.

The other if/elseifs check if you are using sword, to count sword fighting, if you use club, to count club fighting, etc.. (haven't done for magic level, do some research and do yourself)

After that, I checked the skill lvl of the weapon type that the player is using (sword fighting to sword, club fighting to club, etc)

After that, I get a random number between 0 and 99 (including both) . The number generated is compared to the number of the player skill divided by 10 (so, skill 10 = 1%, it means that the random should be 0 to work, and skill 100 = 10%, so the random must be between 0 and 9 to work . Etc


And the rest is a copy of the script you found
 
Actually you put the code in the wrong place

Code:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
   
if (not attacker:isPlayer()) then return end

local weapontype = nil
                                local slotItem = attacker:getSlotItem(CONST_SLOT_LEFT)
                                if    slotItem then
                                    weaponType = slotItem:getType():getWeaponType()
                                end

                               local skill = SKILL_FIST
                                if weaponType == WEAPON_DISTANCE then
                                    skill = SKILL_DISTANCE
                                elseif weaponType == WEAPON_SWORD then
                                     skill = SKILL_SWORD
                                elseif weaponType == WEAPON_CLUB then
                                     skill = SKILL_CLUB
                                elseif weaponType == WEAPON_AXE then
                                     skill = SKILL_AXE
                                end

                                local skilllvl = attacker:getEffectiveSkillLevel(skill)

if math.random(0,99) < skillLvl/10 then
        if origin == ORIGIN_MELEE then
            creature:say("SKULLBASH!", TALKTYPE_MONSTER_SAY)
        elseif origin == ORIGIN_RANGED then
            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
        end
        return primaryDamage * 10, primaryType, secondaryDamage, secondaryType
    end

    return primaryDamage, primaryType, secondaryDamage, secondaryType
end


I can't test (I'm without any ot on my laptop, and I'm writing this on my phone now)
How it should work:the first if check if the attacker is a player.

The second if checks if you have an item equipped in the left hand slot, and then, reserves the type of weapon in a variable.

The other if/elseifs check if you are using sword, to count sword fighting, if you use club, to count club fighting, etc.. (haven't done for magic level, do some research and do yourself)

After that, I checked the skill lvl of the weapon type that the player is using (sword fighting to sword, club fighting to club, etc)

After that, I get a random number between 0 and 99 (including both) . The number generated is compared to the number of the player skill divided by 10 (so, skill 10 = 1%, it means that the random should be 0 to work, and skill 100 = 10%, so the random must be between 0 and 9 to work . Etc


And the rest is a copy of the script you found
It doesnt work.
 
Back
Top