• 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 Add or Subtract Storage Value using items on the same StorageValue

alejandro762

Well-Known Member
Joined
Sep 6, 2021
Messages
225
Reaction score
63
Hello,

Im using this code as Add and Subtract Storage values from items,
For example, A Sword +500, Armor + 500 and legs +500.
Making a look i"ll get a +1500 a score.
I wish to know if is possible to do that using the same Storage Value , adding or subtract values from each one, if Equip or deEquip and not different storageValues like i do on this script, in order to use this addition of values to give access to a place / quest / door / mission etc.
Sorry I'm beginner,

I'm using TFS 1.3,

Here is the script,

Lua:
local gearStor = 150000
local gearStorSec = 150001
local gearStorThirst = 150002

local moveEvent = MoveEvent()
function moveEvent.onEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStor, 500)
    return true
end
moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()

local moveEvent = MoveEvent()
function moveEvent.onDeEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStor)
    return true
end
moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()
----------------------------------
local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStorSec, 500)
    return true
end
moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()

local moveEvent = MoveEvent()
function moveEvent.onDeEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStorSec)
    return true
end
moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()
----------------------------------
local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStorThirst, 500)
    return true
end
moveEvent:id(3983)
moveEvent:slot("legs")
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    player:setStorageValue(gearStorThirst)
    return true
end
moveEvent:id(3983)
moveEvent:slot("legs")
moveEvent:register()

Then, using a talkaction to call this value and see the value,

Code:
local gearTalk = TalkAction("!gearscore")
function gearTalk.onSay(player, words, param)

    local gearStor = player:getStorageValue(150000)
    local gearStorSec = player:getStorageValue(150001)
    local gearStorThirst = player:getStorageValue(150002)

    local gearScoreRank = gearStor + gearStorSec + gearStorThirst
    player:sendTextMessage(MESSAGE_INFO_DESCR, "Your Gear Score is +["..gearScoreRank.."].")
    return false
end

gearTalk:register()
 
Solution
Went on a discord call.

This was the solution we came up with.
Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [42088] = {type = "armor", value = 1},
    [3983] = {type = "legs", value = 1}
}

for itemId, _slot in pairs(config) do
    local stepInEvent = MoveEvent()
    stepInEvent:type("equip")
    
    function stepInEvent.onEquip(player, item, slot, isCheck)
        if not isCheck then
            local currentStorage = player:getStorageValue(gearStor)
            local newStorage = currentStorage + config[item:getId()].value
            player:setStorageValue(gearStor, newStorage)
        end
        return true
    end
    
    
    stepInEvent:id(itemId)
    stepInEvent:slot(_slot.type)...
Lua:
local currentStorage = player:getStorageValue(gearStor)
local newStorage = currentStorage + 500 -- onDeEquip use - instead of +
player:setStorageValue(gearStor, newStorage)
 
Lua:
local currentStorage = player:getStorageValue(gearStor)
local newStorage = currentStorage + 500 -- onDeEquip use - instead of +
player:setStorageValue(gearStor, newStorage)
Thanks for the reply,
I'm doing something wrong, i add it under the function cause i dont know how to register player outside,

Trying with only 1 item,
Is giving + 1000, then retrieve - 500,

Lua:
local gearStor = 150005


local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local newStorage = currentStorage + 500
    player:setStorageValue(gearStor, newStorage)
    return true
end

moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local normalStorage = currentStorage - 500
    player:setStorageValue(gearStor, normalStorage)
    return true
end

moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()

Is working taking care how many it adds and how many to subtract in order to get the right number.
In this case:
Code:
local newStorage = currentStorage + 5

local normalStage = currentStorage - 15

Adding +5 , it adds x3 in game = 15
Then subtract working - 15
= 0.
I dont know if intended it counts storage x3

Tried with 3 items + 500 add, then subtract - 1500, counting perfect:

Code:
10:14 Your Gear Score is +[0].
10:14 Your Gear Score is +[1500].
10:14 Your Gear Score is +[3000].
10:14 Your Gear Score is +[4500].
10:14 Your Gear Score is +[3000].
10:14 Your Gear Score is +[1500].
10:14 Your Gear Score is +[0].
 
Last edited:
Thanks for the reply,
I'm doing something wrong, i add it under the function cause i dont know how to register player outside,

Trying with only 1 item,
Is giving + 1000, then retrieve - 500,

Lua:
local gearStor = 150005


local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local newStorage = currentStorage + 500
    player:setStorageValue(gearStor, newStorage)
    return true
end

moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local normalStorage = currentStorage - 500
    player:setStorageValue(gearStor, normalStorage)
    return true
end

moveEvent:id(42088)
moveEvent:slot("armor")
moveEvent:register()

Is working taking care how many it adds and how many to subtract in order to get the right number.
In this case:
Code:
local newStorage = currentStorage + 5

local normalStage = currentStorage - 15

Adding +5 , it adds x3 in game = 15
Then subtract working - 15
= 0.
I dont know if intended it counts storage x3

Tried with 3 items + 500 add, then subtract - 1500, counting perfect:

Code:
10:14 Your Gear Score is +[0].
10:14 Your Gear Score is +[1500].
10:14 Your Gear Score is +[3000].
10:14 Your Gear Score is +[4500].
10:14 Your Gear Score is +[3000].
10:14 Your Gear Score is +[1500].
10:14 Your Gear Score is +[0].
You need to use isCheck. onEquip is triggered multiple times, because of how the source was coded.

Lua:
function moveEvent.onEquip(player, item, slot, isCheck)
    print("onEquip triggered.")
    if isCheck then
    -- if not isCheck then -- you might need to do it like this instead. I can't remember if it returns true/false on final trigger
        print("Storage Adjusted.")
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + 500
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end
 
I check without modifing i see like i post above when log out and coming back it counts x2 lol.
You need to use isCheck. onEquip is triggered multiple times, because of how the source was coded.

Lua:
function moveEvent.onEquip(player, item, slot, isCheck)
    print("onEquip triggered.")
    if isCheck then
    -- if not isCheck then -- you might need to do it like this instead. I can't remember if it returns true/false on final trigger
        print("Storage Adjusted.")
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + 500
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end

Ok tried with if isCheck then, onEquip.
On deEquip with if isCheck is not working, but adding else after if isCheck then, it works perfectly.
Now it seems to be x2 instead of x3 the storage.

local newStorage = currentStorage + 1, it will add 2
then i will retreive -2 on deEquip.
Is not giving more storage when logout, like the code before, so for me it's perfect!
Now it looks like:

deEquip:
Lua:
You see yourself. You are god.
Gear Score: +[0]

onEquip ( with armor, legs and sword ) +1 (on script ) onLook and storage +2 /each:
Code:
13:57 You see yourself. You are god.
Gear Score: +[6]

Thanks you!

The code so looks like:

Code:
local gearStor = 150000


local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + 1
        player:setStorageValue(gearStor, newStorage)
        return true
    end
end

moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    if isCheck then
    else
        local currentStorage = player:getStorageValue(gearStor)
        local subtractStorage = currentStorage - 2
        player:setStorageValue(gearStor, subtractStorage)
        return true
    end
end

moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()
 
I check without modifing i see like i post above when log out and coming back it counts x2 lol.


Ok tried with if isCheck then, onEquip.
On deEquip with if isCheck is not working, but adding else after if isCheck then, it works perfectly.
Now it seems to be x2 instead of x3 the storage.

local newStorage = currentStorage + 1, it will add 2
then i will retreive -2 on deEquip.
Is not giving more storage when logout, like the code before, so for me it's perfect!
Now it looks like:

deEquip:
Lua:
You see yourself. You are god.
Gear Score: +[0]

onEquip ( with armor, legs and sword ) +1 (on script ) onLook and storage +2 /each:
Code:
13:57 You see yourself. You are god.
Gear Score: +[6]

Thanks you!

The code so looks like:

Code:
local gearStor = 150000


local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + 1
        player:setStorageValue(gearStor, newStorage)
        return true
    end
end

moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    if isCheck then
    else
        local currentStorage = player:getStorageValue(gearStor)
        local subtractStorage = currentStorage - 2
        player:setStorageValue(gearStor, subtractStorage)
        return true
    end
end

moveEvent:id(42036)
moveEvent:slot("hand")
moveEvent:register()

Right, it's doing x2 cuz you're using the wrong version of isCheck.
I literally put green text explaining that.. and put prints into the code so you could test yourself which one worked correctly.

Anyway, here's the correct isCheck, and made with a config table, so you can add all your different items to it.
Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [11111] = {type = "hand", value = 1},
    [22222] = {type = "hand", value = 1}
}

local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if not isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + config[item:getId()].value
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local subtractStorage = currentStorage - config[item:getId()].value
    player:setStorageValue(gearStor, subtractStorage)
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()
 
Last edited:
Right, it's doing x2 cuz you're using the wrong version of isCheck.
I literally put green text explaining that.. and put prints into the code so you could test yourself which one worked correctly.

Anyway, here's the correct isCheck, and made with a config table, so you can add all your different items to it.
Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [11111] = {type = "hand", value = 1},
    [22222] = {type = "hand", value = 1}
}

local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if not isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + config[item:getId()].value
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local subtractStorage = currentStorage - 2
    player:setStorageValue(gearStor, subtractStorage)
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()
Uf, to complex master for me.
I didn't think you could make a table with movement.
Also this part..
Lua:
for itemId, slot in pairs(config) do

    moveEvent:id(itemId)

    moveEvent:slot(slot.type)

end

moveEvent:register()

about for Is it , for itemId of the local config and the slot ("in pairs", I don't understand) so do the movement:id(itemId)?

However, it only takes into account 1 single object, among the 3 and not the 3 at the same time.
 
Uf, to complex master for me.
I didn't think you could make a table with movement.
Also this part..
Lua:
for itemId, slot in pairs(config) do

    moveEvent:id(itemId)

    moveEvent:slot(slot.type)

end

moveEvent:register()

about for Is it , for itemId of the local config and the slot ("in pairs", I don't understand) so do the movement:id(itemId)?

However, it only takes into account 1 single object, among the 3 and not the 3 at the same time.
Edited my above post.
There was a missing part I forgot to change in onDeEquip

What do you mean it only takes a single object?

Test again with the updated script.
Maybe the confusion is because of my error.
 
Edited my above post.
There was a missing part I forgot to change in onDeEquip

What do you mean it only takes a single object?

Test again with the updated script.
Maybe the confusion is because of my error.
Ok, ill check that when i finish the work.
 
Edited my above post.
There was a missing part I forgot to change in onDeEquip

What do you mean it only takes a single object?

Test again with the updated script.
Maybe the confusion is because of my error.

Tried like this,

Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [42088] = {type = "armor", value = 1},
    [3983] = {type = "legs", value = 1}
}

local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if not isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + config[item:getId()].value
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local subtractStorage = currentStorage - config[item:getId()].value
    player:setStorageValue(gearStor, subtractStorage)
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

Tested , only 42088 it will add +1 then subtract -1. The 2 Others will not work.
 
Tried like this,

Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [42088] = {type = "armor", value = 1},
    [3983] = {type = "legs", value = 1}
}

local moveEvent = MoveEvent()

function moveEvent.onEquip(player, item, slot, isCheck)
    if not isCheck then
        local currentStorage = player:getStorageValue(gearStor)
        local newStorage = currentStorage + config[item:getId()].value
        player:setStorageValue(gearStor, newStorage)
    end
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

local moveEvent = MoveEvent()

function moveEvent.onDeEquip(player, item, slot, isCheck)
    local currentStorage = player:getStorageValue(gearStor)
    local subtractStorage = currentStorage - config[item:getId()].value
    player:setStorageValue(gearStor, subtractStorage)
    return true
end

for itemId, slot in pairs(config) do
    moveEvent:id(itemId)
    moveEvent:slot(slot.type)
end
moveEvent:register()

Tested , only 42088 it will add +1 then subtract -1. The 2 Others will not work.
Are the others already registered in movements.xml?

-- edit
Maybe I'm doing something dumb.
Let me check my old scripts.

-- edit 2
No, pretty sure it should be working as intended.

I'd definitely check and make sure these items aren't already registered in movements.xml
Or rather, just look at your console and look for something like "duplicate registered itemid in movements"
 
Last edited:
Are the others already registered in movements.xml?

-- edit
Maybe I'm doing something dumb.
Let me check my old scripts.

-- edit 2
No, pretty sure it should be working as intended.

I'd definitely check and make sure these items aren't already registered in movements.xml

Nope they aren't registered,
42088 spiritthorn armor
1661978415934.png
3983 bast skirt
1661978461753.png
 
Went on a discord call.

This was the solution we came up with.
Lua:
local gearStor = 150000
local config = {
  --[itemid]
    [42036] = {type = "hand", value = 1},
    [42088] = {type = "armor", value = 1},
    [3983] = {type = "legs", value = 1}
}

for itemId, _slot in pairs(config) do
    local stepInEvent = MoveEvent()
    stepInEvent:type("equip")
    
    function stepInEvent.onEquip(player, item, slot, isCheck)
        if not isCheck then
            local currentStorage = player:getStorageValue(gearStor)
            local newStorage = currentStorage + config[item:getId()].value
            player:setStorageValue(gearStor, newStorage)
        end
        return true
    end
    
    
    stepInEvent:id(itemId)
    stepInEvent:slot(_slot.type)
    stepInEvent:register()
    
    local stepOutEvent = MoveEvent()
    stepOutEvent:type("deequip")
    
    function stepOutEvent.onDeEquip(player, item, slot, isCheck)
        local currentStorage = player:getStorageValue(gearStor)
        local subtractStorage = currentStorage - config[item:getId()].value
        player:setStorageValue(gearStor, subtractStorage)
        return true
    end
    
    stepOutEvent:id(itemId)
    stepOutEvent:slot(_slot.type)
    stepOutEvent:register()
end


local creatureevent = CreatureEvent("whateveryouwannanamethis")

function creatureevent.onLogin(player)
    local total = 0
    for i = 1, 10 do
        local item = player:getSlotItem(i)
        if item then
            local index = config[item:getId()]
            if index then
                total = total + index.value
            end
        end
    end
    player:setStorageValue(gearStor, total)
    return true
end

creatureevent:register()
 
Solution
Back
Top