• 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 item:getWeaponType() not working

Tubeshop

Member
Joined
Nov 23, 2023
Messages
173
Reaction score
19
local itemType = ItemType(player:getSlotItem(CONST_SLOT_LEFT))
if itemType then
print(itemType:getWeaponType())
if itemType:getWeaponType() == WEAPON_AXE then
print("axe")
else
print("empty")
end
end
its allways print "0" nad "empty". Why lua dont see weapon type? this is a creaturescript fragment.
 
ItemType doesn't take an Item userdata, which is what you receive when you call getSlotItem (or nil), instead, check if the item in the slot exists, then use ItemType(item:getId())
 
ItemType doesn't take an Item userdata, which is what you receive when you call getSlotItem (or nil), instead, check if the item in the slot exists, then use ItemType(item:getId())
item is in left hand but:
local itemType = ItemType(player:getSlotItem(CONST_SLOT_LEFT))
if itemType then
print(ItemType(item:getId()))
if itemType:getWeaponType() == WEAPON_AXE then
print("axe")
else
print("empty")
end
end
not work for me. its print "empty"
 
and this:

local itemType = ItemType(player:getSlotItem(CONST_SLOT_LEFT))
if itemType then
print(ItemType(item:getId()))
if itemType:getWeaponType() == WEAPON_AXE then
print("axe")
else
print("empty")
end
end
return:

[C]: in function '__index'
data/creaturescripts/scripts/idle/idleexp.lua:60: in function 'DodawanieSkill'
data/creaturescripts/scripts/idle/idleexp.lua:25: in function <data/creaturescripts/scripts/idle/idleexp.lua:1>

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/idle/idleexp.lua:eek:nThink
data/creaturescripts/scripts/idle/idleexp.lua:60: attempt to index global 'item' (a nil value)
stack traceback:
[C]: in function '__index'
data/creaturescripts/scripts/idle/idleexp.lua:60: in function 'DodawanieSkill'
data/creaturescripts/scripts/idle/idleexp.lua:25: in function <data/creaturescripts/scripts/idle/idleexp.lua:1>
Post automatically merged:

local itemType = ItemType(player:getSlotItem(CONST_SLOT_LEFT))
print(ItemType(itemType:getId()))
if itemType then

if itemType:getWeaponType() == WEAPON_AXE then
print("axe")
else
print("empty")
end
end
return:

empty
userdata: 0x4107fa38
 
I don't see either of those blocks of code changed, regardless, you need to check if the item exists in the left hand before you attempt to get the id of it, the same way you check if the item type exists before you use it.
 
try this

Lua:
    local weaponId = getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid
    local weaponItem = ItemType(weaponId)
    local weaponType = weaponItem:getWeaponType()
    
    if weaponItem then
        print(weaponId)
        if weaponType == WEAPON_AXE then
            print("axe")
        else
            print("empty")
        end
    end
)
 
Last edited:
Lua:
local item = player:getSlotItem(CONST_SLOT_LEFT)
if not item then
    print("no slot item")
    return true
end

local itemType = item:getType()
--or
--local itemType = ItemType(item:getId())
if not itemType then
    print("failed to get itemtype")
    return true
end

print(itemType:getWeaponType() or "no weapon type")
if itemType:getWeaponType() == WEAPON_AXE then
    print("axe")
else
    print("not axe")
end
 
try this

Lua:
    local weapon = getPlayerSlotItem(cid, CONST_SLOT_LEFT).itemid
    local it = ItemType(weapon)
    if it then
        print(weapon)
        if it == WEAPON_AXE then
            print("axe")
        else
            print("empty")
        end
    end
)
This will throw errors if there is no slot item...

Always handle properties or methods singular, to make sure that the first execution actually exists, otherwise you will be checking the itemid property of nil....and it will throw an error
 
This will throw errors if there is no slot item...

Always handle properties or methods singular, to make sure that the first execution actually exists, otherwise you will be checking the itemid property of nil....and it will throw an error
Thanks, I fixed it to work correctly
 
Back
Top