• 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.X+ Storage on items

liqeen

Active Member
Joined
Nov 26, 2014
Messages
149
Solutions
1
Reaction score
28
Location
Poland
Hello, I'm trying to make a script that will set a certain value of storage to item from table when equipped, it "KINDA" works but not so far, by that I mean it do set the storage but every time a player gets hit it adds the storage once more and so on, also it does not remove the storage upon dequiping.

How it should work:
1. Player has already a certain value of x storage.
2. Player equips an item which gives value of "5" for example.
3. Player should have value of the storage equal to x + 5.
4. Player dequip a piece of equipment and his storage value should go back to x.


Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)   

    if attacker then
        local base = creature:getStorageValue(27452)
        local storage = 0
    
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local slot = creature:getSlotItem(i)
            if slot then
                local v = gear[slot:getId()]
                if v then
                    if i == v.slot then

                    storage = v.amount
                    end
                end
            end
        end
      
      creature:setStorageValue(27452, base + storage) end
    return   
end

It is probably messed up and should be done in movements but I can't handle it, please help :D
Thanks for your time
 
Solution
And we are back again to "cannot dress that object" thing xD
Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(player, item, slot, equipType)
    local itemId = item:getId()
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number    
            player:setStorageValue(27452, newValue)
        end
    end
    return
end

function onEquip(player, item, slot...
Hello, I'm trying to make a script that will set a certain value of storage to item from table when equipped, it "KINDA" works but not so far, by that I mean it do set the storage but every time a player gets hit it adds the storage once more and so on, also it does not remove the storage upon dequiping.

How it should work:
1. Player has already a certain value of x storage.
2. Player equips an item which gives value of "5" for example.
3. Player should have value of the storage equal to x + 5.
4. Player dequip a piece of equipment and his storage value should go back to x.


Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

function onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)  

    if attacker then
        local base = creature:getStorageValue(27452)
        local storage = 0
   
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local slot = creature:getSlotItem(i)
            if slot then
                local v = gear[slot:getId()]
                if v then
                    if i == v.slot then

                    storage = v.amount
                    end
                end
            end
        end
     
      creature:setStorageValue(27452, base + storage) end
    return  
end

It is probably messed up and should be done in movements but I can't handle it, please help :D
Thanks for your time
Just need to change this line
Lua:
storage = v.amount
to
Lua:
storage = storage + v.amount

And it will add all of the current equipped armor instead of only 1 equipped.

But, as you said.. probably better to do this inside of onEquip / onDeEquip instead, since it triggers less often then onHealthChange
 
Just need to change this line
Lua:
storage = v.amount
to
Lua:
storage = storage + v.amount

And it will add all of the current equipped armor instead of only 1 equipped.

But, as you said.. probably better to do this inside of onEquip / onDeEquip instead, since it triggers less often then onHealthChange
Okay I've changed it to movements, the equip part works as it should but dequip ain't removing the storage dunno tho how to make it as negative value, it can be stacked infinitly. Can you please take a look?

Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = -500}
}

function onDeEquip(cid, item, slot)
      local player = Player(cid)
      if not player then return false end
        local base = player:getStorageValue(27452)
        local storage = 0
        local slots = getPlayerSlotItem(cid, slot )
       
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local slot = player:getSlotItem(i)
            if slot then
                local v = gear[slot:getId()]
                if v then
                    if i == v.slot then

                 storage = v.amount
                    end
                end
            end
      player:setStorageValue(27452, base - storage) end
    return  
end
 
Last edited:
Okay I've changed it to movements, the equip part works as it should but dequip ain't removing the storage dunno tho how to make it as negative value, it can be stacked infinitly. Can you please take a look?

Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = -500}
}

function onDeEquip(cid, item, slot)
      local player = Player(cid)
      if not player then return false end
        local base = player:getStorageValue(27452)
        local storage = 0
        local slots = getPlayerSlotItem(cid, slot )
  
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local slot = player:getSlotItem(i)
            if slot then
                local v = gear[slot:getId()]
                if v then
                    if i == v.slot then

                 storage = v.amount
                    end
                end
            end
      player:setStorageValue(27452, base - storage) end
    return
end
Previously, you had to calculate every armor on your character, since you had no idea which armor was possibly changed.

However inside of movements, we know which slot is being changed, and what item is being changed, so we no longer require to loop over all the armors, and can freely change the value because of that.

Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(itemId, slot, equipType)
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number      
            player:setStorageValue(27452, newValue)
        end
    end
    return  
end

function onEquip(cid, item, slot)  
    local itemId = item:getId()
    updateEquipmentStorageValue(itemId, slot, 1) -- 1 signifies 'on', aka Equip
    return
end

function onDeEquip(cid, item, slot)  
    local itemId = item:getId()
    updateEquipmentStorageValue(itemId, slot, 0) -- 0 signifies 'off', aka deEquip
    return
end
 
Previously, you had to calculate every armor on your character, since you had no idea which armor was possibly changed.

However inside of movements, we know which slot is being changed, and what item is being changed, so we no longer require to loop over all the armors, and can freely change the value because of that.

Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(itemId, slot, equipType)
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number     
            player:setStorageValue(27452, newValue)
        end
    end
    return 
end

function onEquip(cid, item, slot) 
    local itemId = item:getId()
    updateEquipmentStorageValue(itemId, slot, 1) -- 1 signifies 'on', aka Equip
    return
end

function onDeEquip(cid, item, slot) 
    local itemId = item:getId()
    updateEquipmentStorageValue(itemId, slot, 0) -- 0 signifies 'off', aka deEquip
    return
end
Okay the loop part seems pretty logical now but I encounter a new problem.
I've added the code below because it could not find a player when I tried to equip an item.
Lua:
 local player = Player(cid)
 if not player then return false end
But now for some reason when I try to equip an item it says in tibia that "You cannot dress that object there." Despite that slot type is correct and item is registered in movements as a necklace.
 
Okay the loop part seems pretty logical now but I encounter a new problem.
I've added the code below because it could not find a player when I tried to equip an item.
Lua:
 local player = Player(cid)
 if not player then return false end
But now for some reason when I try to equip an item it says in tibia that "You cannot dress that object there." Despite that slot type is correct and item is registered in movements as a necklace.
Ah oops, I forgot to add that lol

This should work.
Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(player, itemId, slot, equipType)
    local itemId = item:getId()
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number     
            player:setStorageValue(27452, newValue)
        end
    end
    return 
end

function onEquip(player, item, slot)
    updateEquipmentStorageValue(player, item, slot, 1) -- 1 signifies 'on', aka Equip
    return
end

function onDeEquip(cid, item, slot) 
    updateEquipmentStorageValue(player, item, slot, 0) -- 0 signifies 'off', aka deEquip
    return
end
 
Ah oops, I forgot to add that lol

This should work.
Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(player, itemId, slot, equipType)
    local itemId = item:getId()
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number    
            player:setStorageValue(27452, newValue)
        end
    end
    return
end

function onEquip(player, item, slot)
    updateEquipmentStorageValue(player, item, slot, 1) -- 1 signifies 'on', aka Equip
    return
end

function onDeEquip(cid, item, slot)
    updateEquipmentStorageValue(player, item, slot, 0) -- 0 signifies 'off', aka deEquip
    return
end
So close yet so far xD
Well now it shouts in console:
Lua:
Lua Script Error: [MoveEvents Interface]
data/movements/scripts/testitem.lua:onEquip
data/movements/scripts/testitem.lua:6: attempt to index global 'item' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/movements/scripts/testitem.lua:6: in function 'updateEquipmentStorageValue'
        data/movements/scripts/testitem.lua:17: in function <data/movements/scripts/testitem.lua:16>
 
So close yet so far xD
Well now it shouts in console:
Lua:
Lua Script Error: [MoveEvents Interface]
data/movements/scripts/testitem.lua:onEquip
data/movements/scripts/testitem.lua:6: attempt to index global 'item' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/movements/scripts/testitem.lua:6: in function 'updateEquipmentStorageValue'
        data/movements/scripts/testitem.lua:17: in function <data/movements/scripts/testitem.lua:16>
change
Lua:
local function updateEquipmentStorageValue(player, itemId, slot, equipType)
to
Lua:
local function updateEquipmentStorageValue(player, item, slot, equipType)
xD
 
And we are back again to "cannot dress that object" thing xD
Lua:
local gear = {
    [21691] = {slot = CONST_SLOT_NECKLACE, amount = 500}
}

local function updateEquipmentStorageValue(player, item, slot, equipType)
    local itemId = item:getId()
    if gear[itemId] then
        if gear[itemId].slot == slot then -- note, you may need to change the tables slot value to match what the onEquip/onDeEquip slot value is producing
            local newValue = player:getStorageValue(27452) + (equipType == 1 and gear[itemId].amount or gear[itemId].amount * -1) -- if deEquip we multiply the value by -1, to turn it into a negative number    
            player:setStorageValue(27452, newValue)
        end
    end
    return
end

function onEquip(player, item, slot, isCheck)
    if isCheck == false then
        updateEquipmentStorageValue(player, item, slot, 1) -- 1 signifies 'on', aka Equip
    end
    return true
end

function onDeEquip(player, item, slot, isCheck)
    if isCheck == false then
        updateEquipmentStorageValue(player, item, slot, 0) -- 0 signifies 'off', aka deEquip
    end
    return true
end
 
Solution
Back
Top