• 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
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?
https://github.com/peonso/forgotten...29be80f5b949/docs/SCRIPTSYSTEM_HELP#L151-L152
 
I'm not sure what getPlayerSlotItem would return if slot is empty (I think it would return a table with uid=0), but try this:

Lua:
local leftItem = getPlayerSlotItem(cid, CONST_SLOT_LEFT).uid
local rightItem = getPlayerSlotItem(cid, CONST_SLOT_RIGHT).uid
if leftItem > 0 and rightItem > 0 and getItemWeaponType(leftItem) ~= WEAPON_NONE and getItemWeaponType(rightItem) ~= WEAPON_NONE then
   -- code here if he has correct weapons
end

You said you only want to verify so I didn't find them for you.

(If it works I think it might be tricked by putting ammo in hands, because I should be checking if they're not ammo as well)
 
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?
This is just a template.
Lua:
local items = {1234, 7890}

function onEquip(cid, item, slot)
    local slots = getPlayerSlotItem(cid, slot)
    if slots.itemid ~= item.itemid then
       -- this is to combat the 2nd call to onEquip
        return true
    end
   if isInArray(items, slots.itemid) then
       -- do something
   end
   return true
end

function onDeEquip(cid, item, slot)
   local slots = getPlayerSlotItem(cid, slot)
   if next(slots) then -- check (always good to check)
       if isInArray(items, slots.itemid) then
           -- do something else when they remove the item
       end
   end
    return true
end
 
Last edited:
I'm not sure what getPlayerSlotItem would return if slot is empty (I think it would return a table with uid=0), but try this:

Lua:
local leftItem = getPlayerSlotItem(cid, CONST_SLOT_LEFT).uid
local rightItem = getPlayerSlotItem(cid, CONST_SLOT_RIGHT).uid
if leftItem > 0 and rightItem > 0 and getItemWeaponType(leftItem) ~= WEAPON_NONE and getItemWeaponType(rightItem) ~= WEAPON_NONE then
   -- code here if he has correct weapons
end

You said you only want to verify so I didn't find them for you.

(If it works I think it might be tricked by putting ammo in hands, because I should be checking if they're not ammo as well)
This is just a template.
Lua:
local items = {1234, 7890}

function onEquip(cid, item, slot)
    local slots = getPlayerSlotItem(cid, slot)
    if slots.itemid ~= item.itemid then
       -- this is to combat the 2nd call to onEquip
        return true
    end
   if isInArray(items, slots.itemid) then
       -- do something
   end
   return true
end

function onDeEquip(cid, item, slot)
   local slots = getPlayerSlotItem(cid, slot)
   if next(slots) then -- check (always good to check)
       if isInArray(items, slots.itemid) then
           -- do something else when they remove the item
       end
   end
    return true
end

First of all, thank you both for the answers. I have expressed myself pretty bad. But I what I truly want is, for example: If the player has a Fire Sword or any other kind of SWORD equipped, Only then he will be able to cast a spell. So, the spell is kinda locked with the sword item type. If there is a function to it I suppose I can do the rest.

I tried doing the following:

Lua:
local leftItem = getPlayerSlotItem(cid, CONST_SLOT_LEFT).uid
local rightItem = getPlayerSlotItem(cid, CONST_SLOT_RIGHT).uid
if leftItem > 0 and rightItem > 0 and getItemWeaponType(leftItem) == 7 or getItemWeaponType(rightItem) == 7 then

But the error listed below appeared:

Lua:
[Error - Spell Interface]
data/spells/scripts/attack/ex
Description:
(luaGetThing) Thing not found

I'm very stupid with script.
 
So you only need to know the weapon type?

So I guess just:

Lua:
-- put this function in global.lua or something
function getPlayerWeaponType(cid)
    return getItemWeaponType(getPlayerWeapon(cid, true).uid)
end

-- use this in your code
if getPlayerWeaponType(cid) == WEAPON_SWORD then
   -- your code here
end

If it doesn't work tell me what errors you might receive.
 
This is just a template.
Lua:
local items = {1234, 7890}

function onEquip(cid, item, slot)
    local slots = getPlayerSlotItem(cid, slot)
    if slots.itemid ~= item.itemid then
       -- this is to combat the 2nd call to onEquip
        return true
    end
   if isInArray(items, slots.itemid) then
       -- do something
   end
   return true
end

function onDeEquip(cid, item, slot)
   local slots = getPlayerSlotItem(cid, slot)
   if next(slots) then -- check (always good to check)
       if isInArray(items, slots.itemid) then
           -- do something else when they remove the item
       end
   end
    return true
end
What if they equip the same item as they already have equipped?
https://otland.net/threads/fix-0-3-7-0-4-0-3-6-multiple-onequip-bug.249084/
 
What if they equip the same item as they already have equipped?
https://otland.net/threads/fix-0-3-7-0-4-0-3-6-multiple-onequip-bug.249084/
Well then they have a problem :p well as a developer this is something they would need to account for. The script I provided is just a template and contains no real code its essentially a starting point. :)

Also I'd like to point out that if the op were to use onEquip/DeEquip they would need to register the itemid with the server in movements in order for the script to effect the items that are equipped.

So it isn't like we write 1 script and it effects every weapon/equipment on the server without registering the id's.
 
Last edited:
So you only need to know the weapon type?

So I guess just:

Lua:
-- put this function in global.lua or something
function getPlayerWeaponType(cid)
    return getItemWeaponType(getPlayerWeapon(cid, true).uid)
end

-- use this in your code
if getPlayerWeaponType(cid) == WEAPON_SWORD then
   -- your code here
end

If it doesn't work tell me what errors you might receive.


Thats really helpful! What global lua is that?

WAIT
NEVERMIND
It worked! Thank you very much indeed! Finally one more step concluded here. Thank you brother!

WAIT
error at DISTRO:
I mean, its working, but when I have no weapon this error appears. Is there a workaround?

Lua:
Error - Spell Interface]
data/spells/scripts/attack/exevo flam hur.lua:onCastSpell
Description:
(luaGetThing) Thing not found
 
Last edited:
Thats really helpful! What global lua is that?

WAIT
NEVERMIND
It worked! Thank you very much indeed! Finally one more step concluded here. Thank you brother!

WAIT
error at DISTRO:
I mean, its working, but when I have no weapon this error appears. Is there a workaround?

Lua:
Error - Spell Interface]
data/spells/scripts/attack/exevo flam hur.lua:onCastSpell
Description:
(luaGetThing) Thing not found
Well I'm guessing it's the getPlayerWeapon function causing that error. You're not sending any specific error string so hard to tell. Either way if you can locate your getPlayerWeapon function in global.lua and paste it here (unless you could give me a more specific error string with file name and line number).
 
Last edited:
Well I'm guessing it's the getPlayerWeapon function causing that error. You're not sending any specific error string so hard to tell. Either way if you can locate your getPlayerWeapon function in config.lua and paste it here (unless you could give me a more specific error string with file name and line number).

Unfortunately, my config.lua does not have get Player Weapon and that error is the unique thing that appears.

Looks like the function searchs for a "weapolntype id" but cant find it, so it generates the error. That happens with empty hands and random items.
 
Wouldn't adding 'else' returning false when slotitem is not a weapon or nil do the trick? Or similar 'If' check on beginning of the script?
 
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
 
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

Thank you ma friend! Now I can do very interesting shit in my server. I hope this can help more people with their servers.
 
Last edited by a moderator:
I tried to use this function on a new TFS 1.2 and it didn't work.
And it spams "attempt to call global 'getPlayerWeapon' (a nil value)"

I know it's an old thread but someone can help?
 
I tried to use this function on a new TFS 1.2 and it didn't work.
And it spams "attempt to call global 'getPlayerWeapon' (a nil value)"

I know it's an old thread but someone can help?

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.
 
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.
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
 
Back
Top