• 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 Function for verifying weapon type equipped?

FilipeJF

New Member
Joined
Jan 9, 2012
Messages
124
Reaction score
4
Is there any function in Crying Damson 0.3.6 that can verify the kind of weapon the player is currently equipped with? I did a research around the internet and I've found some interesting ones:

Lua:
((getPlayerSlotItem(cid, 6).itemid == ID de todos os itens do tipo) or (getPlayerSlotItem(cid, 5).itemid == ID de todos os itens do tipo))

getItemWeaponType(uid)

getItemWeaponType(getPlayerWeapon(cid, true).uid)


Is there a way to group those functions to verify if the player is equipped with a shield, a sword, an axe, a club or even a wand?
 
Solution
Hmm okay try this:
Lua:
function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end

-- use this in your code
if getPlayerWeaponType(cid) == WEAPON_SWORD then
   -- your code here
end
nice bro, but you forgot to check if there is a weapon

Code:
function Player:getWeaponType()
    local weapon = self:getSlotItem(CONST_SLOT_LEFT)
    if (weapon) then
        return ItemType(weapon:getId()):getWeaponType()
    end
    return WEAPON_NONE
end
his will work just fine, the ItemType constructor will return nil if it's given nil as an argument.
he checks if itemType then to make sure the ItemType constructor successfully worked, if the player didn't have an item in that slot it would be given a nil parameter, which results in itemType being nil.
also in your code you could just do weapon:getType():getWeaponType() instead of using the constructor and itemid
 
Because 0.2 / 1.x never used getPlayerWeapon, so there is no compat function for it.
Lua:
local itemType = ItemType(player:getSlotItem(CONST_SLOT_LEFT))
if itemType then
    if itemType:getWeaponType() == WEAPON_SWORD then
        -- your code here
    end
end

Give that a try insted.


ok guys @Joe Rod and @WibbenZ i'm very new to this and idk where to put those functions, i'm using a clean datapack and just one source edit ( True Dual Wielding )
and this script ( Random Item Stats ). I'm trying to change tibia to use it as a D&D like game for me and my friends to play over skype. So it can be more dynamic

i'm using it inside a onHealthChange event to calculate "critical strikes" based on the weapon/skill that the player has. (That is the ideia)
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  if(Monster(attacker) ~= nil) then --if attacker is monster, then don't crit
  return primaryDamage, primaryType, secondaryDamage, secondaryType
   end
   if attacker == nil then --if attacker is dead, conditions apply after
  return primaryDamage, primaryType, secondaryDamage, secondaryType
   end


   if Player:getWeaponType() == SWORD then
         creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
         return primaryDamage, primaryType, secondaryDamage, secondaryType
   else
   creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
   return primaryDamage*100, primaryType, secondaryDamage, secondaryType
end
end

and global.lua
Code:
dofile('data/lib/lib.lua')
for dir in io.popen([[dir "data\lib\" /b /aa]]):lines() do
  dofile('data/lib/'..dir..'')
end

STORAGEVALUE_PROMOTION = 30018

ropeSpots = {384, 418, 8278, 8592, 13189, 14435, 14436, 15635, 19518}

doors = {[1209] = 1211, [1210] = 1211, [1212] = 1214, [1213] = 1214, [1219] = 1220, [1221] = 1222, [1231] = 1233, [1232] = 1233, [1234] = 1236, [1235] = 1236, [1237] = 1238, [1239] = 1240, [1249] = 1251, [1250] = 1251, [1252] = 1254, [1253] = 1254, [1539] = 1540, [1541] = 1542, [3535] = 3537, [3536] = 3537, [3538] = 3539, [3544] = 3546, [3545] = 3546, [3547] = 3548, [4913] = 4915, [4914] = 4915, [4916] = 4918, [4917] = 4918, [5082] = 5083, [5084] = 5085, [5098] = 5100, [5099] = 5100, [5101] = 5102, [5107] = 5109, [5108] = 5109, [5110] = 5111, [5116] = 5118, [5117] = 5118, [5119] = 5120, [5125] = 5127, [5126] = 5127, [5128] = 5129, [5134] = 5136, [5135] = 5136, [5137] = 5139, [5138] = 5139, [5140] = 5142, [5141] = 5142, [5143] = 5145, [5144] = 5145, [5278] = 5280, [5279] = 5280, [5281] = 5283, [5282] = 5283, [5284] = 5285, [5286] = 5287, [5515] = 5516, [5517] = 5518, [5732] = 5734, [5733] = 5734, [5735] = 5737, [5736] = 5737, [6192] = 6194, [6193] = 6194, [6195] = 6197, [6196] = 6197, [6198] = 6199, [6200] = 6201, [6249] = 6251, [6250] = 6251, [6252] = 6254, [6253] = 6254, [6255] = 6256, [6257] = 6258, [6795] = 6796, [6797] = 6798, [6799] = 6800, [6801] = 6802, [6891] = 6893, [6892] = 6893, [6894] = 6895, [6900] = 6902, [6901] = 6902, [6903] = 6904, [7033] = 7035, [7034] = 7035, [7036] = 7037, [7042] = 7044, [7043] = 7044, [7045] = 7046, [7054] = 7055, [7056] = 7057, [8541] = 8543, [8542] = 8543, [8544] = 8546, [8545] = 8546, [8547] = 8548, [8549] = 8550, [9165] = 9167, [9166] = 9167, [9168] = 9170, [9169] = 9170, [9171] = 9172, [9173] = 9174, [9267] = 9269, [9268] = 9269, [9270] = 9272, [9271] = 9272, [9273] = 9274, [9275] = 9276, [10276] = 10277, [10274] = 10275, [10268] = 10270, [10269] = 10270, [10271] = 10273, [10272] = 10273, [10471] = 10472, [10480] = 10481, [10477] = 10479, [10478] = 10479, [10468] = 10470, [10469] = 10470, [10775] = 10777, [10776] = 10777, [12092] = 12094, [12093] = 12094, [12188] = 12190, [12189] = 12190, [19840] = 19842, [19841] = 19842, [19843] = 19844, [19980] = 19982, [19981] = 19982, [19983] = 19984, [20273] = 20275, [20274] = 20275, [20276] = 20277, [17235] = 17236, [18208] = 18209, [13022] = 13023, [10784] = 10786, [10785] = 10786, [12099] = 12101, [12100] = 12101, [12197] = 12199, [12198] = 12199, [19849] = 19851, [19850] = 19851, [19852] = 19853, [19989] = 19991, [19990] = 19991, [19992] = 19993, [20282] = 20284, [20283] = 20284, [20285] = 20286, [17237] = 17238, [13020] = 13021, [10780] = 10781, [12095] = 12096, [12195] = 12196, [19845] = 19846, [19985] = 19986, [20278] = 20279, [10789] = 10790, [12102] = 12103, [12204] = 12205, [19854] = 19855, [19994] = 19995, [20287] = 20288, [10782] = 10783, [12097] = 12098, [12193] = 12194, [19847] = 19848, [19987] = 19988, [20280] = 20281, [10791] = 10792, [12104] = 12105, [12202] = 12203, [19856] = 19857, [19996] = 19997, [20289] = 20290}
verticalOpenDoors = {1211, 1220, 1224, 1228, 1233, 1238, 1242, 1246, 1251, 1256, 1260, 1540, 3546, 3548, 3550, 3552, 4915, 5083, 5109, 5111, 5113, 5115, 5127, 5129, 5131, 5133, 5142, 5145, 5283, 5285, 5289, 5293, 5516, 5737, 5749, 6194, 6199, 6203, 6207, 6251, 6256, 6260, 6264, 6798, 6802, 6902, 6904, 6906, 6908, 7044, 7046, 7048, 7050, 7055, 8543, 8548, 8552, 8556, 9167, 9172, 9269, 9274, 9274, 9269, 9278, 9282, 10270, 10275, 10279, 10283, 10479, 10481, 10485, 10483, 10786, 12101, 12199, 19851, 19853, 19991, 19993, 20284, 20286, 17238, 13021, 10790, 12103, 12205, 19855, 19995, 20288, 10792, 12105, 12203, 19857, 19997, 20290}
horizontalOpenDoors = {1214, 1222, 1226, 1230, 1236, 1240, 1244, 1248, 1254, 1258, 1262, 1542, 3537, 3539, 3541, 3543, 4918, 5085, 5100, 5102, 5104, 5106, 5118, 5120, 5122, 5124, 5136, 5139, 5280, 5287, 5291, 5295, 5518, 5734, 5746, 6197, 6201, 6205, 6209, 6254, 6258, 6262, 6266, 6796, 6800, 6893, 6895, 6897, 6899, 7035, 7037, 7039, 7041, 7057, 8546, 8550, 8554, 8558, 9170, 9174, 9272, 9276, 9280, 9284, 10273, 10277, 10281, 10285, 10470, 10472, 10476, 10474, 10777, 12094, 12190, 19842, 19844, 19982, 19984, 20275, 20277, 17236, 18209, 13023, 10781, 12096, 12196, 19846, 19986, 20279, 10783, 12098, 12194, 19848, 19988, 20281}
openSpecialDoors = {1224, 1226, 1228, 1230, 1242, 1244, 1246, 1248, 1256, 1258, 1260, 1262, 3541, 3543, 3550, 3552, 5104, 5106, 5113, 5115, 5122, 5124, 5131, 5133, 5289, 5291, 5293, 5295, 6203, 6205, 6207, 6209, 6260, 6262, 6264, 6266, 6897, 6899, 6906, 6908, 7039, 7041, 7048, 7050, 8552, 8554, 8556, 8558, 9176, 9178, 9180, 9182, 9278, 9280, 9282, 9284, 10279, 10281, 10283, 10285, 10474, 10476, 10483, 10485, 10781, 12096, 12196, 19846, 19986, 20279, 10783, 12098, 12194, 19848, 19988, 20281, 10790, 12103, 12205, 19855, 19995, 20288, 10792, 12105, 12203, 19857, 19997, 20290}
questDoors = {1223, 1225, 1241, 1243, 1255, 1257, 3542, 3551, 5105, 5114, 5123, 5132, 5288, 5290, 5745, 5748, 6202, 6204, 6259, 6261, 6898, 6907, 7040, 7049, 8551, 8553, 9175, 9177, 9277, 9279, 10278, 10280, 10475, 10484, 10782, 10791, 12097, 12104, 12193, 12202, 19847, 19856, 19987, 19996, 20280, 20289}
levelDoors = {1227, 1229, 1245, 1247, 1259, 1261, 3540, 3549, 5103, 5112, 5121, 5130, 5292, 5294, 6206, 6208, 6263, 6265, 6896, 6905, 7038, 7047, 8555, 8557, 9179, 9181, 9281, 9283, 10282, 10284, 10473, 10482, 10780, 10789, 10780, 12095, 12102, 12204, 12195, 19845, 19854, 19985, 19994, 20278, 20287}
keys = {2086, 2087, 2088, 2089, 2090, 2091, 2092, 10032}

function Player:getWeaponType()
    local weapon = self:getSlotItem(CONST_SLOT_LEFT)
    if (weapon) then
        return ItemType(weapon:getId()):getWeaponType()
    end
    return WEAPON_NONE
end

function getDistanceBetween(firstPosition, secondPosition)
    local xDif = math.abs(firstPosition.x - secondPosition.x)
    local yDif = math.abs(firstPosition.y - secondPosition.y)
    local posDif = math.max(xDif, yDif)
    if firstPosition.z ~= secondPosition.z then
        posDif = posDif + 15
    end
    return posDif
end

function getFormattedWorldTime()
    local worldTime = getWorldTime()
    local hours = math.floor(worldTime / 60)

    local minutes = worldTime % 60
    if minutes < 10 then
        minutes = '0' .. minutes
    end
    return hours .. ':' .. minutes
end

table.contains = function(array, value)
    for _, targetColumn in pairs(array) do
        if targetColumn == value then
            return true
        end
    end
    return false
end

string.split = function(str, sep)
    local res = {}
    for v in str:gmatch("([^" .. sep .. "]+)") do
        res[#res + 1] = v
    end
    return res
end

string.trim = function(str)
    return str:match'^()%s*$' and '' or str:match'^%s*(.*%S)'
end

if not nextUseStaminaTime then
    nextUseStaminaTime = {}
end
There is a better way of doing this? I just want to check the players weapon to calculate a % of crit based on the skill lvl of the weapon he is using, btw no errors just the "NOT A SWORD" test popping
 
Last edited:
ok guys @Joe Rod and @WibbenZ i'm very new to this and idk where to put those functions, i'm using a clean datapack and just one source edit ( True Dual Wielding )
and this script ( Random Item Stats ). I'm trying to change tibia to use it as a D&D like game for me and my friends to play over skype. So it can be more dynamic

i'm using it inside a onHealthChange event to calculate "critical strikes" based on the weapon/skill that the player has. (That is the ideia)
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
  if(Monster(attacker) ~= nil) then --if attacker is monster, then don't crit
  return primaryDamage, primaryType, secondaryDamage, secondaryType
   end
   if attacker == nil then --if attacker is dead, conditions apply after
  return primaryDamage, primaryType, secondaryDamage, secondaryType
   end


   if Player:getWeaponType() == SWORD then
         creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
         return primaryDamage, primaryType, secondaryDamage, secondaryType
   else
   creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
   return primaryDamage*100, primaryType, secondaryDamage, secondaryType
end
end

and global.lua
Code:
dofile('data/lib/lib.lua')
for dir in io.popen([[dir "data\lib\" /b /aa]]):lines() do
  dofile('data/lib/'..dir..'')
end

STORAGEVALUE_PROMOTION = 30018

ropeSpots = {384, 418, 8278, 8592, 13189, 14435, 14436, 15635, 19518}

doors = {[1209] = 1211, [1210] = 1211, [1212] = 1214, [1213] = 1214, [1219] = 1220, [1221] = 1222, [1231] = 1233, [1232] = 1233, [1234] = 1236, [1235] = 1236, [1237] = 1238, [1239] = 1240, [1249] = 1251, [1250] = 1251, [1252] = 1254, [1253] = 1254, [1539] = 1540, [1541] = 1542, [3535] = 3537, [3536] = 3537, [3538] = 3539, [3544] = 3546, [3545] = 3546, [3547] = 3548, [4913] = 4915, [4914] = 4915, [4916] = 4918, [4917] = 4918, [5082] = 5083, [5084] = 5085, [5098] = 5100, [5099] = 5100, [5101] = 5102, [5107] = 5109, [5108] = 5109, [5110] = 5111, [5116] = 5118, [5117] = 5118, [5119] = 5120, [5125] = 5127, [5126] = 5127, [5128] = 5129, [5134] = 5136, [5135] = 5136, [5137] = 5139, [5138] = 5139, [5140] = 5142, [5141] = 5142, [5143] = 5145, [5144] = 5145, [5278] = 5280, [5279] = 5280, [5281] = 5283, [5282] = 5283, [5284] = 5285, [5286] = 5287, [5515] = 5516, [5517] = 5518, [5732] = 5734, [5733] = 5734, [5735] = 5737, [5736] = 5737, [6192] = 6194, [6193] = 6194, [6195] = 6197, [6196] = 6197, [6198] = 6199, [6200] = 6201, [6249] = 6251, [6250] = 6251, [6252] = 6254, [6253] = 6254, [6255] = 6256, [6257] = 6258, [6795] = 6796, [6797] = 6798, [6799] = 6800, [6801] = 6802, [6891] = 6893, [6892] = 6893, [6894] = 6895, [6900] = 6902, [6901] = 6902, [6903] = 6904, [7033] = 7035, [7034] = 7035, [7036] = 7037, [7042] = 7044, [7043] = 7044, [7045] = 7046, [7054] = 7055, [7056] = 7057, [8541] = 8543, [8542] = 8543, [8544] = 8546, [8545] = 8546, [8547] = 8548, [8549] = 8550, [9165] = 9167, [9166] = 9167, [9168] = 9170, [9169] = 9170, [9171] = 9172, [9173] = 9174, [9267] = 9269, [9268] = 9269, [9270] = 9272, [9271] = 9272, [9273] = 9274, [9275] = 9276, [10276] = 10277, [10274] = 10275, [10268] = 10270, [10269] = 10270, [10271] = 10273, [10272] = 10273, [10471] = 10472, [10480] = 10481, [10477] = 10479, [10478] = 10479, [10468] = 10470, [10469] = 10470, [10775] = 10777, [10776] = 10777, [12092] = 12094, [12093] = 12094, [12188] = 12190, [12189] = 12190, [19840] = 19842, [19841] = 19842, [19843] = 19844, [19980] = 19982, [19981] = 19982, [19983] = 19984, [20273] = 20275, [20274] = 20275, [20276] = 20277, [17235] = 17236, [18208] = 18209, [13022] = 13023, [10784] = 10786, [10785] = 10786, [12099] = 12101, [12100] = 12101, [12197] = 12199, [12198] = 12199, [19849] = 19851, [19850] = 19851, [19852] = 19853, [19989] = 19991, [19990] = 19991, [19992] = 19993, [20282] = 20284, [20283] = 20284, [20285] = 20286, [17237] = 17238, [13020] = 13021, [10780] = 10781, [12095] = 12096, [12195] = 12196, [19845] = 19846, [19985] = 19986, [20278] = 20279, [10789] = 10790, [12102] = 12103, [12204] = 12205, [19854] = 19855, [19994] = 19995, [20287] = 20288, [10782] = 10783, [12097] = 12098, [12193] = 12194, [19847] = 19848, [19987] = 19988, [20280] = 20281, [10791] = 10792, [12104] = 12105, [12202] = 12203, [19856] = 19857, [19996] = 19997, [20289] = 20290}
verticalOpenDoors = {1211, 1220, 1224, 1228, 1233, 1238, 1242, 1246, 1251, 1256, 1260, 1540, 3546, 3548, 3550, 3552, 4915, 5083, 5109, 5111, 5113, 5115, 5127, 5129, 5131, 5133, 5142, 5145, 5283, 5285, 5289, 5293, 5516, 5737, 5749, 6194, 6199, 6203, 6207, 6251, 6256, 6260, 6264, 6798, 6802, 6902, 6904, 6906, 6908, 7044, 7046, 7048, 7050, 7055, 8543, 8548, 8552, 8556, 9167, 9172, 9269, 9274, 9274, 9269, 9278, 9282, 10270, 10275, 10279, 10283, 10479, 10481, 10485, 10483, 10786, 12101, 12199, 19851, 19853, 19991, 19993, 20284, 20286, 17238, 13021, 10790, 12103, 12205, 19855, 19995, 20288, 10792, 12105, 12203, 19857, 19997, 20290}
horizontalOpenDoors = {1214, 1222, 1226, 1230, 1236, 1240, 1244, 1248, 1254, 1258, 1262, 1542, 3537, 3539, 3541, 3543, 4918, 5085, 5100, 5102, 5104, 5106, 5118, 5120, 5122, 5124, 5136, 5139, 5280, 5287, 5291, 5295, 5518, 5734, 5746, 6197, 6201, 6205, 6209, 6254, 6258, 6262, 6266, 6796, 6800, 6893, 6895, 6897, 6899, 7035, 7037, 7039, 7041, 7057, 8546, 8550, 8554, 8558, 9170, 9174, 9272, 9276, 9280, 9284, 10273, 10277, 10281, 10285, 10470, 10472, 10476, 10474, 10777, 12094, 12190, 19842, 19844, 19982, 19984, 20275, 20277, 17236, 18209, 13023, 10781, 12096, 12196, 19846, 19986, 20279, 10783, 12098, 12194, 19848, 19988, 20281}
openSpecialDoors = {1224, 1226, 1228, 1230, 1242, 1244, 1246, 1248, 1256, 1258, 1260, 1262, 3541, 3543, 3550, 3552, 5104, 5106, 5113, 5115, 5122, 5124, 5131, 5133, 5289, 5291, 5293, 5295, 6203, 6205, 6207, 6209, 6260, 6262, 6264, 6266, 6897, 6899, 6906, 6908, 7039, 7041, 7048, 7050, 8552, 8554, 8556, 8558, 9176, 9178, 9180, 9182, 9278, 9280, 9282, 9284, 10279, 10281, 10283, 10285, 10474, 10476, 10483, 10485, 10781, 12096, 12196, 19846, 19986, 20279, 10783, 12098, 12194, 19848, 19988, 20281, 10790, 12103, 12205, 19855, 19995, 20288, 10792, 12105, 12203, 19857, 19997, 20290}
questDoors = {1223, 1225, 1241, 1243, 1255, 1257, 3542, 3551, 5105, 5114, 5123, 5132, 5288, 5290, 5745, 5748, 6202, 6204, 6259, 6261, 6898, 6907, 7040, 7049, 8551, 8553, 9175, 9177, 9277, 9279, 10278, 10280, 10475, 10484, 10782, 10791, 12097, 12104, 12193, 12202, 19847, 19856, 19987, 19996, 20280, 20289}
levelDoors = {1227, 1229, 1245, 1247, 1259, 1261, 3540, 3549, 5103, 5112, 5121, 5130, 5292, 5294, 6206, 6208, 6263, 6265, 6896, 6905, 7038, 7047, 8555, 8557, 9179, 9181, 9281, 9283, 10282, 10284, 10473, 10482, 10780, 10789, 10780, 12095, 12102, 12204, 12195, 19845, 19854, 19985, 19994, 20278, 20287}
keys = {2086, 2087, 2088, 2089, 2090, 2091, 2092, 10032}

function Player:getWeaponType()
    local weapon = self:getSlotItem(CONST_SLOT_LEFT)
    if (weapon) then
        return ItemType(weapon:getId()):getWeaponType()
    end
    return WEAPON_NONE
end

function getDistanceBetween(firstPosition, secondPosition)
    local xDif = math.abs(firstPosition.x - secondPosition.x)
    local yDif = math.abs(firstPosition.y - secondPosition.y)
    local posDif = math.max(xDif, yDif)
    if firstPosition.z ~= secondPosition.z then
        posDif = posDif + 15
    end
    return posDif
end

function getFormattedWorldTime()
    local worldTime = getWorldTime()
    local hours = math.floor(worldTime / 60)

    local minutes = worldTime % 60
    if minutes < 10 then
        minutes = '0' .. minutes
    end
    return hours .. ':' .. minutes
end

table.contains = function(array, value)
    for _, targetColumn in pairs(array) do
        if targetColumn == value then
            return true
        end
    end
    return false
end

string.split = function(str, sep)
    local res = {}
    for v in str:gmatch("([^" .. sep .. "]+)") do
        res[#res + 1] = v
    end
    return res
end

string.trim = function(str)
    return str:match'^()%s*$' and '' or str:match'^%s*(.*%S)'
end

if not nextUseStaminaTime then
    nextUseStaminaTime = {}
end
There is a better way of doing this? I just want to check the players weapon to calculate a % of crit based on the skill lvl of the weapon he is using, btw no errors just the "NOT A SWORD" test popping

Rule 1 of coding, always keep your code clean.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not attacker or attacker:isMonster() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if creature:isPlayer() and creature:getWeaponType() == WEAPON_SWORD then
        creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    else
        creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage * 100, primaryType, secondaryDamage, secondaryType
    end
end

Moved the second if statment to the first, if the attacker dosn't exist we shouldn't call Monster or in this case isMonster()
Changed SWORD to the correct variable WEAPON_SWORD ref: forgottenserver/const.h at e70f7153128596ce870bb79cf5f1ae4159262291 · otland/forgottenserver · GitHub
 
Last edited:
Rule 1 of coding, always keep your code clean.
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not attacker then -- if attacker is dead, conditions apply after
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if attacker:isMonster() then -- if attacker is monster, then don't crit
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    if creature:isPlayer() and creature:getWeaponType() == WEAPON_SWORD then
        creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    else
        creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage * 100, primaryType, secondaryDamage, secondaryType
    end
end

Moved the second if statment to the first, if the attacker dosn't exist we shouldn't call Monster or in this case isMonster()
Changed SWORD to the correct variable WEAPON_SWORD ref: forgottenserver/const.h at e70f7153128596ce870bb79cf5f1ae4159262291 · otland/forgottenserver · GitHub
if not attacker or attacker:isMonster() then
 
Oh didn't notice they returned the same values haha, ill edit the post
No luck, still getting the test message "NOT A SWORD!" maybe it's the source edit that i mentioned before? I tested to see if the skills were leveling up as normal, and yes everything is working.
If someone wants i could upload my files somewhere
 
No luck, still getting the test message "NOT A SWORD!" maybe it's the source edit that i mentioned before? I tested to see if the skills were leveling up as normal, and yes everything is working.
If someone wants i could upload my files somewhere

Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not attacker or attacker:isMonster() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    print(creature:getWeaponType())
    if creature:isPlayer() and creature:getWeaponType() == WEAPON_SWORD then
        creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    else
        creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage * 100, primaryType, secondaryDamage, secondaryType
    end
end

Give that a try, then take an SS of your console (the error)
 
Lua:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
    if not attacker or attacker:isMonster() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end

    print(creature:getWeaponType())
    if creature:isPlayer() and creature:getWeaponType() == WEAPON_SWORD then
        creature:say("SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    else
        creature:say("NOT A SWORD!", TALKTYPE_MONSTER_SAY)
        return primaryDamage * 100, primaryType, secondaryDamage, secondaryType
    end
end

Give that a try, then take an SS of your console (the error)

Ok here
xAzG3D3.jpg
 
put this in lib/core/player.lua and do /reload global
Lua:
local nonWeapons = {WEAPON_NONE, WEAPON_SHIELD, WEAPON_AMMO}

function Player.getWeaponType(self)
    local left = self:getSlotItem(CONST_SLOT_LEFT)
    if left then
        local weaponType = left:getType():getWeaponType()
        if not isInArray(nonWeapons, weaponType) then
            return weaponType
        end
    end
    local right = self:getSlotItem(CONST_SLOT_RIGHT)
    if right then
        local weaponType = right:getType():getWeaponType()
        if not isInArray(nonWeapons, weaponType) then
            return weaponType
        end
    end
end
 
put this in lib/core/player.lua and do /reload global
Lua:
local nonWeapons = {WEAPON_NONE, WEAPON_SHIELD, WEAPON_AMMO}

function Player.getWeaponType(self)
    local left = self:getSlotItem(CONST_SLOT_LEFT)
    if left then
        local weaponType = left:getType():getWeaponType()
        if not isInArray(nonWeapons, weaponType) then
            return weaponType
        end
    end
    local right = self:getSlotItem(CONST_SLOT_RIGHT)
    if right then
        local weaponType = right:getType():getWeaponType()
        if not isInArray(nonWeapons, weaponType) then
            return weaponType
        end
    end
end

Ok i put it and got the same error, (i have the global.lua and the core/player.lua at the same time its ok? or i need to delete one of them)

But i just changed the print(creature:getWeaponType()) to print(attacker:getWeaponType()) and now im getting some numbers on the console 1 for sword / 2 for club / 3 for axe / 0 for fist (nothing) / 5 for distance it is working right?
 
Ok i put it and got the same error, (i have the global.lua and the core/player.lua at the same time its ok? or i need to delete one of them)

But i just changed the print(creature:getWeaponType()) to print(attacker:getWeaponType()) and now im getting some numbers on the console 1 for sword / 2 for club / 3 for axe / 0 for fist (nothing) / 5 for distance it is working right?

Yes in that case it's working.
The reason why the function is missing is because your source code is oudated; forgottenserver/luascript.cpp at 1.2 · otland/forgottenserver · GitHub
Are you sure you are using the latest 1.2? Or are you using 1.1 or the earliest 1.2?
 
Really thanks to @Static_ @WibbenZ @Joe Rod and this thread I could achieve what i wanted. If someone wants to see/use.
I just use it to simulate a more "complex" critical system for my home server/(D&D wannabe), i tried alot to make the imbuiment system work but i couldn't, so i came up with this ideia.
I know that the code is really amateur and messy. Thx again for the help! If someone wants to improve or optimize the script feel free.
I'm really liking this community

Code:
function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
local percDam = 0

    if not attacker or attacker:isMonster() then
        return primaryDamage, primaryType, secondaryDamage, secondaryType
    end
    --print(attacker:getWeaponType())
    --print(attacker:getMagicLevel(SKILL_MAGLEVEL))

    if attacker:isPlayer() then
        if attacker:getWeaponType() == WEAPON_SWORD and math.random(100) <= attacker:getSkillLevel(SKILL_SWORD) * 1.10 then
            --attacker:say("SWORD", TALKTYPE_MONSTER_SAY)
            percDam = (attacker:getSkillLevel(SKILL_SWORD) / 75) * primaryDamage       
            elseif attacker:getWeaponType() == WEAPON_AXE and math.random(100) <= attacker:getSkillLevel(SKILL_AXE) * 0.9 then
            --attacker:say("AXE", TALKTYPE_MONSTER_SAY)
            percDam = (attacker:getSkillLevel(SKILL_AXE) / 75) * primaryDamage
            elseif attacker:getWeaponType() == WEAPON_CLUB and math.random(100) <= attacker:getSkillLevel(SKILL_CLUB) * 0.75 then
            --attacker:say("CLUB", TALKTYPE_MONSTER_SAY)
            percDam = (attacker:getSkillLevel(SKILL_CLUB) / 75) * primaryDamage
            elseif attacker:getWeaponType() == WEAPON_DISTANCE and math.random(100) <= attacker:getSkillLevel(SKILL_DISTANCE) * 0.75 then
            --attacker:say("DISTANCE", TALKTYPE_MONSTER_SAY)
            percDam = (attacker:getSkillLevel(SKILL_DISTANCE) / 75) * primaryDamage
            elseif attacker:getWeaponType() == WEAPON_WAND and math.random(100) <= attacker:getMagicLevel(SKILL_MAGLEVEL) * 1 then
            --attacker:say("WAND", TALKTYPE_MONSTER_SAY)
            percDam = (attacker:getMagicLevel(SKILL_MAGLEVEL) / 75) * primaryDamage           
        else
        --attacker:say("NO CRIT", TALKTYPE_MONSTER_SAY)
        return primaryDamage, primaryType, secondaryDamage, secondaryType   
        end
    end

    if percDam ~= 0 then
        if origin == ORIGIN_RANGED then
            creature:say("HEADSHOT!", TALKTYPE_MONSTER_SAY)
            creature:getPosition():sendMagicEffect(CONST_ME_EXPLOSIONAREA)       
        elseif origin == ORIGIN_MELEE then
            attacker:say("CRITICAL!", TALKTYPE_MONSTER_SAY)
            creature:getPosition():sendMagicEffect(CONST_ME_HITAREA)
        elseif origin == ORIGIN_SPELL then
            if primaryType == 2 then
                    attacker:say("ELECTROCUTE!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 512 then
                    attacker:say("FREEZE!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 8 then
                    attacker:say("BURN!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 2048 then
                    attacker:say("EMBRACE!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 1024 then
                    attacker:say("PURIFY!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 128 then
                    attacker:say("REVIVE!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 1 then
                    attacker:say("CRITICAL!", TALKTYPE_MONSTER_SAY)
                elseif primaryType == 4 then
                    attacker:say("THORN!", TALKTYPE_MONSTER_SAY)
            end
        end
    end

    attacker:sendTextMessage((MESSAGE_DAMAGE_DEALT), "Critical Hit: +".. percDam .." DMG")
    return primaryDamage + percDam, primaryType, secondaryDamage, secondaryType
   
end

--[[
VIS     2
FRIGO     512
FLAM     8
MORT     2048
HOLY     1024
HEAL    128
PHYS    1
TERA    4
]]--
 
Back
Top