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

[1.2] Help with getSlotItem function returning nil

Siegh

Thronar Developer
Joined
Mar 12, 2011
Messages
1,186
Solutions
1
Reaction score
510
Location
Brazil
So, I have this in one of my weapons scripts which is supposed to check whether you have a certain necklace equipped and, if yes, you gain 1 soul. It works fine, but it returns an error on my console if I don't have any necklace equipped.

Code:
    if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 2131 then
        player:addSoul(1)
    end

1682850382335.png

I have looked up previous questions and I found this fix here, which works in some contexts, but I'm not being able to modify it to still allow the script to work even if you don't have enything equipped in the slot.

Code:
if not player:getSlotItem(CONST_SLOT_NECKLACE) then
return false

I'm pretty sure there is a very simple solution to this which I'm oversighting, help please!
 
So, I have this in one of my weapons scripts which is supposed to check whether you have a certain necklace equipped and, if yes, you gain 1 soul. It works fine, but it returns an error on my console if I don't have any necklace equipped.

Code:
    if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 2131 then
        player:addSoul(1)
    end

View attachment 75237

I have looked up previous questions and I found this fix here, which works in some contexts, but I'm not being able to modify it to still allow the script to work even if you don't have enything equipped in the slot.

Code:
if not player:getSlotItem(CONST_SLOT_NECKLACE) then
return false

I'm pretty sure there is a very simple solution to this which I'm oversighting, help please!
Could you post the full script?
 
Code:
function onUseWeapon(player, variant)
    function onGetFormulaValues(player, skill, attack, factor)
        local min = (((player:getLevel() * 4) + (attack * (skill*0.1)))/3)
        local max = ((player:getLevel() * 4) + (attack * (skill*0.1)))
        return -min, -max
    end
    combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
    combat:execute(player, variant)
    
    if player:getSlotItem(CONST_SLOT_LEFT) then 
        if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 2131 then
            player:addSoul(1)
        end
    end
return true
end
 
Not full script + a different script now :D

Edit: this should work
Lua:
function onGetFormulaValues(player, skill, attack, factor)
    local min = (((player:getLevel() * 4) + (attack * (skill*0.1)))/3)
    local max = ((player:getLevel() * 4) + (attack * (skill*0.1)))
    return -min, -max
 end
    
combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")

function onUseWeapon(player, variant)
    local amuletId = 2131
    local amulet = player:getSlotItem(CONST_SLOT_NECKLACE)
    if amulet and amulet.itemid == amuletId then
        player:addSoul(1)
    end
    combat:execute(player, variant)
    return true
end
 
Last edited:
Not full script + a different script now :DD
The situation is literally the same, I don't need to post it completely cause it's a big one and it's still under work. I just need assistance with the returning nil error on the console part.

Edit: it had a wrong line with CONST_SLOT_LEFT by mistake, it was supposed to be necklace only.
 
Code:
function onUseWeapon(player, variant)
    function onGetFormulaValues(player, skill, attack, factor)
        local min = (((player:getLevel() * 4) + (attack * (skill*0.1)))/3)
        local max = ((player:getLevel() * 4) + (attack * (skill*0.1)))
        return -min, -max
    end
    combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
    combat:execute(player, variant)
   
    if player:getSlotItem(CONST_SLOT_LEFT) then
        if player:getSlotItem(CONST_SLOT_NECKLACE).itemid == 2131 then
            player:addSoul(1)
        end
    end
return true
end
Lua:
function onUseWeapon(player, variant)
local necklaceItemID = 2131
function onGetFormulaValues(player, skill, attack, factor)
    local min = (((player:getLevel() * 4) + (attack * (skill*0.1)))/3)
    local max = ((player:getLevel() * 4) + (attack * (skill*0.1)))
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")
combat:execute(player, variant)

if player:getSlotItem(CONST_SLOT_LEFT) then
    if player:getSlotItem(CONST_SLOT_NECKLACE) and player:getSlotItem(CONST_SLOT_NECKLACE).itemid == necklaceItemID then
        player:addSoul(1)
    end
end

return true
end
 
Thanks Shalaby that work, that's the check I needed. Just wanted to mention the CONST_SLOT_LEFT check wasn't necessary, it was there by mistake. Cheers.
 
Back
Top