• 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 TFS 1.3 Storage on use!

SorketROrk

Well-Known Member
Joined
Oct 14, 2020
Messages
152
Solutions
1
Reaction score
69
Location
Sweden
Hey! :)

I was wondering if someone could help me out with a simple script, I want to spread out a couple of statues around the map, and when player "use" they should get a storage value and a message "You found 1 out of 10 statues".

I've been having difficulties with it since I do not know how I could make a check to see if player already clicked on a statue or if its a new to be added as 2/10 and so on.
My idea here is to avoid linear style where you find statue 1 and then statue 2 in order to get statue 3.. Player should be able to click on statue 1 and then maybe the next one he finds is statue 10, it should not matter which statue it is, it should give +1 to storage on any of them while perventing multi usage of the same statue.

My Request:
*Player can use statue once, getting storage value
*If player already has clicked then send message "You already found this statue"
*If player finds a new statue it should know how many statues you previously had to send correct message with "You have found x out of x statues"

Hopefully ive explained good enough, I appreciate all help! :D


Yours, SRO
 
Solution
Lua:
local canCreateBossAroundPos = true -- Can we create around the stone position?
local forceBossCreating      = true -- If position is not walkable, should we force the boss creating?

local configs = -- Stone configs
{
  -- [uniqueId] = { config }
  [12610]        = { storageId = 12610, bosses = { 'Demon', 'Demon', 'Demon', 'Demon',' Orshabaal'}, allBosses = true, bossPos = Position(1310, 897, 9)},
  [12611]        = { storageId = 12611, bosses = { 'Juggernaut', 'Juggernaut', 'Juggernaut', 'Ghazbaran'}, allBosses = true, bossPos = Position(1316, 895, 9)},
  [12612]        = { storageId = 12612, bosses = { 'Orshabaal', 'Orshabaal', 'Demon', 'Demon'}, allBosses = true, bossPos = Position(1310, 891, 9)},
  [12613]        = { storageId = 12613, bosses = {...
Lua:
local canCreateBossAroundPos = true -- Can we create around the stone position?
local forceBossCreating      = true -- If position is not walkable, should we force the boss creating?

local configs = -- Stone configs
{
  -- [uniqueId] = { config }
  [12610]        = { storageId = 12610, bosses = { 'Demon', 'Demon', 'Demon', 'Demon',' Orshabaal'}, allBosses = true, bossPos = Position(1310, 897, 9)},
  [12611]        = { storageId = 12611, bosses = { 'Juggernaut', 'Juggernaut', 'Juggernaut', 'Ghazbaran'}, allBosses = true, bossPos = Position(1316, 895, 9)},
  [12612]        = { storageId = 12612, bosses = { 'Orshabaal', 'Orshabaal', 'Demon', 'Demon'}, allBosses = true, bossPos = Position(1310, 891, 9)},
  [12613]        = { storageId = 12613, bosses = { 'Ghazbaran', 'Morgaroth', 'Orshabaal'}, allBosses = true, bossPos = Position(1316, 891, 9)},

  [12614]        = { checkAllStoneStorages = true, teleportPos = Position(1317, 860, 9) } -- Reward config (room if got all stones above)
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  local config = configs[item.uid] -- Our stone config is registered at the 'config' variable (not 'configs'!)

  -- Stones configs check
  -- Config not found
  if not config then
    return false -- False to send the "Sorry, not possible." message.
  end
  local checkAllStoneStorages = config.checkAllStoneStorages
  -- Should have either checkAllStoneStorages as true or a storageId setted at the stone config
  if not checkAllStoneStorages and not config.storageId then
    return false -- False to send the "Sorry, not possible." message.
  end

  -- If has storageId to check
  if config.storageId then
    -- Check it meaning that the stone has been already taken
    if player:getStorageValue(config.storageId) > -1 then
     player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You've already taken this, there is %d of %d stones left.", stonesTaken, totalStones))
      return true -- True to not send the "Sorry, not possible." message.

    -- Player has not the stone storage
    else
      -- Enable the storageId, meaning that the player took this stone
      player:setStorageValue(config.storageId, 1)
    end
  end

  -- Stones storage/count check
  local stonesTaken = 0
  local totalStones = 0
  -- ipairs goes from key 1 to N, pairs goes through all table values
  -- "_" is the uid, but call as "_" because we are not using this value (it's just nomenclature)
  for _, cfg in pairs(configs) do
    -- If has storageId to check
    if cfg.storageId then
      -- It's a stone to check, so increate the total stones
      totalStones = totalStones + 1

      -- Player has not the stone storage
      if player:getStorageValue(cfg.storageId) == -1 then
        -- Player does not have 1 of all storages, so we cannot continue
        if checkAllStoneStorages then -- Reward config
          return false -- False to send the "Sorry, not possible." message.
        end

      -- Player has the stone storage
      else
        -- Increase the stonesTaken meaning the player took this stone (just for count how many stones has been taken)
        stonesTaken = stonesTaken + 1
      end
    end
  end

  -- If has boss to create
  if config.bosses then
    -- Create all bosses at list
    if config.allBosses then
      for _, bossName in ipairs(config.bosses) do -- ipairs, and mentioned, goes from key 1 to N
        Game.createMonster(bossName, config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
      end

    -- Create only 1 boss
    else
      Game.createMonster(config.bosses[math.random(#config.bosses)], config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
    end
  end

  -- If has teleportPos
  if config.teleportPos then
    -- Teleport player
    player:teleportTo(config.teleportPos, false) -- False to: if the teleportPos is around player, then don't do the walking movement
  end

  -- Message to player
  if checkAllStoneStorages then -- Reward config
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations! You got all stones!")
  else
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You got %d of %d stones.", stonesTaken, totalStones))
  end
  return true
end
Just delete spawning monsters from config and add how many statues suits you.
 
Last edited:
Lua:
local canCreateBossAroundPos = true -- Can we create around the stone position?
local forceBossCreating      = true -- If position is not walkable, should we force the boss creating?

local configs = -- Stone configs
{
  -- [uniqueId] = { config }
  [12610]        = { storageId = 12610, bosses = { 'Demon', 'Demon', 'Demon', 'Demon',' Orshabaal'}, allBosses = true, bossPos = Position(1310, 897, 9)},
  [12611]        = { storageId = 12611, bosses = { 'Juggernaut', 'Juggernaut', 'Juggernaut', 'Ghazbaran'}, allBosses = true, bossPos = Position(1316, 895, 9)},
  [12612]        = { storageId = 12612, bosses = { 'Orshabaal', 'Orshabaal', 'Demon', 'Demon'}, allBosses = true, bossPos = Position(1310, 891, 9)},
  [12613]        = { storageId = 12613, bosses = { 'Ghazbaran', 'Morgaroth', 'Orshabaal'}, allBosses = true, bossPos = Position(1316, 891, 9)},

  [12614]        = { checkAllStoneStorages = true, teleportPos = Position(1317, 860, 9) } -- Reward config (room if got all stones above)
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  local config = configs[item.uid] -- Our stone config is registered at the 'config' variable (not 'configs'!)

  -- Stones configs check
  -- Config not found
  if not config then
    return false -- False to send the "Sorry, not possible." message.
  end
  local checkAllStoneStorages = config.checkAllStoneStorages
  -- Should have either checkAllStoneStorages as true or a storageId setted at the stone config
  if not checkAllStoneStorages and not config.storageId then
    return false -- False to send the "Sorry, not possible." message.
  end

  -- If has storageId to check
  if config.storageId then
    -- Check it meaning that the stone has been already taken
    if player:getStorageValue(config.storageId) > -1 then
     player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You've already taken this, there is %d of %d stones left.", stonesTaken, totalStones))
      return true -- True to not send the "Sorry, not possible." message.

    -- Player has not the stone storage
    else
      -- Enable the storageId, meaning that the player took this stone
      player:setStorageValue(config.storageId, 1)
    end
  end

  -- Stones storage/count check
  local stonesTaken = 0
  local totalStones = 0
  -- ipairs goes from key 1 to N, pairs goes through all table values
  -- "_" is the uid, but call as "_" because we are not using this value (it's just nomenclature)
  for _, cfg in pairs(configs) do
    -- If has storageId to check
    if cfg.storageId then
      -- It's a stone to check, so increate the total stones
      totalStones = totalStones + 1

      -- Player has not the stone storage
      if player:getStorageValue(cfg.storageId) == -1 then
        -- Player does not have 1 of all storages, so we cannot continue
        if checkAllStoneStorages then -- Reward config
          return false -- False to send the "Sorry, not possible." message.
        end

      -- Player has the stone storage
      else
        -- Increase the stonesTaken meaning the player took this stone (just for count how many stones has been taken)
        stonesTaken = stonesTaken + 1
      end
    end
  end

  -- If has boss to create
  if config.bosses then
    -- Create all bosses at list
    if config.allBosses then
      for _, bossName in ipairs(config.bosses) do -- ipairs, and mentioned, goes from key 1 to N
        Game.createMonster(bossName, config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
      end

    -- Create only 1 boss
    else
      Game.createMonster(config.bosses[math.random(#config.bosses)], config.bossPos or fromPosition, canCreateBossAroundPos, forceBossCreating)
    end
  end

  -- If has teleportPos
  if config.teleportPos then
    -- Teleport player
    player:teleportTo(config.teleportPos, false) -- False to: if the teleportPos is around player, then don't do the walking movement
  end

  -- Message to player
  if checkAllStoneStorages then -- Reward config
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Congratulations! You got all stones!")
  else
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, string.format("You got %d of %d stones.", stonesTaken, totalStones))
  end
  return true
end
Just delete spawning monsters from config and add how many statues suits you.

Its prob confusing him in that case here we go

dont forget action.xml

actions/scripts/YourFileName

Lua:
local config = {
    [5000] = { storage = 5000 },
    [5001] = { storage = 5001 },
    [5002] = { storage = 5002 },
    [5003] = { storage = 5003 },
    [5004] = { storage = 5004 },
    [5005] = { storage = 5005 },
    [5006] = { storage = 5006 },
    [5007] = { storage = 5007 },
    [5008] = { storage = 5008 },
    [5009] = { storage = 5009 },
    [5010] = { storage = 5010 }
}
   

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local statue = config[item.actionid]
           
    if player:getStorageValue(statue.storage) == 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You alredy used this statue')
        return true
    end
   
    if statue then
        player:setStorageValue(10000, player:getStorageValue(10000) + 1)
        player:setStorageValue(statue.storage, 1)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'you have found '..player:getStorageValue(10000).." statues")
    end
    return true
end
 
Solution
Its prob confusing him in that case here we go

dont forget action.xml

actions/scripts/YourFileName

Lua:
local config = {
    [5000] = { storage = 5000 },
    [5001] = { storage = 5001 },
    [5002] = { storage = 5002 },
    [5003] = { storage = 5003 },
    [5004] = { storage = 5004 },
    [5005] = { storage = 5005 },
    [5006] = { storage = 5006 },
    [5007] = { storage = 5007 },
    [5008] = { storage = 5008 },
    [5009] = { storage = 5009 },
    [5010] = { storage = 5010 }
}
  

function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    local statue = config[item.actionid]
          
    if player:getStorageValue(statue.storage) == 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You alredy used this statue')
        return true
    end
  
    if statue then
        player:setStorageValue(10000, player:getStorageValue(10000) + 1)
        player:setStorageValue(statue.storage, 1)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'you have found '..player:getStorageValue(10000).." statues")
    end
    return true
end
Hey, thanks for making it a bit easier, im getting another result than expected tho.

15:31 you have found 0 statues
15:31 you have found 1 statues
15:31 you have found 2 statues

How come it starts from 0 when using the first statue?
Here is the code:

Lua:
local config = {
    [45111] = { storage = 16125 },
    [45112] = { storage = 16126 },
    [45113] = { storage = 16127 }
}
  


function onUse(player, item, fromPosition, target, toPosition, isHotkey)


    local statue = config[item.actionid]
          
    if player:getStorageValue(statue.storage) == 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You alredy used this statue')
        return true
    end
  
    if statue then
        player:setStorageValue(16128, player:getStorageValue(16128) + 1)
        player:setStorageValue(statue.storage, 1)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'you have found '..player:getStorageValue(16128).." statues")
    end
    return true
end
 
I think its bcus every storage starts on -1

add in login lua

if player:getStorageValue(16128) == -1 then
player:setStorageValue(16128, 1)
end
Player:setStorageValue(16128, 0)* so when the message is sent its the storage value after being changed to +1 so it sends 1 statue, you might need a string format to make it statue for 1 and statues for 2++
 
Well it seems to be working now, I did it like this:

Lua:
local config = {
    [45111] = { storage = 16125 },
    [45112] = { storage = 16126 },
    [45113] = { storage = 16127 }
}
  


function onUse(player, item, fromPosition, target, toPosition, isHotkey)


    local statue = config[item.actionid]
          
    if player:getStorageValue(statue.storage) == 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You alredy used this statue')
        return true
    end
  
   if player:getStorageValue(16128) == -1 then
        player:setStorageValue(16128, 0)
   end
  
    if statue then
        
        player:setStorageValue(16128, player:getStorageValue(16128) + 1)
        player:setStorageValue(statue.storage, 1)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'you have found '..player:getStorageValue(16128).." statues")
    end
    return true
end

15:50 you have found 1 statues
15:50 you have found 2 statues
15:50 you have found 3 statues

Is this a good way to do it?

Thanks for the help guys :)
 
A few things:
  • check whether statue is not nil earlier. You use it at line 14th but ensure that it exists at line 23rd.
  • try to assign storage value to some local variable instead of getting it all across the script multiple times (in this small script you execute getter of the same storage value like 3 times the and setter twice). You can get it once at the beginning and set it once at the very end of script (using calculeted value from local variable).
  • I suggest you to use first storage value number to store assumed amount of collected statues instead of the last one (16125 - assumed value, 16126 - first statue, 16127 - second statue, ...). If you want to add another statue in future, in this case you'll keep continuity in collected statues storage numbers. (Just to keep it clean, it won't change code behaviour - example below).

this order:
Code:
16125 - assumed value
16126 - first statue
16127 - second statue
-- after some time when you extend the script
16128 - third statue

instead of this:
Code:
16125 - first statue 
16126 - second statue
16127 - assumed value
-- after some time when you extend the script
16128 - third statue
 
Well it seems to be working now, I did it like this:

Lua:
local config = {
    [45111] = { storage = 16125 },
    [45112] = { storage = 16126 },
    [45113] = { storage = 16127 }
}
 


function onUse(player, item, fromPosition, target, toPosition, isHotkey)


    local statue = config[item.actionid]
         
    if player:getStorageValue(statue.storage) == 1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You alredy used this statue')
        return true
    end
 
   if player:getStorageValue(16128) == -1 then
        player:setStorageValue(16128, 0)
   end
 
    if statue then
       
        player:setStorageValue(16128, player:getStorageValue(16128) + 1)
        player:setStorageValue(statue.storage, 1)
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'you have found '..player:getStorageValue(16128).." statues")
    end
    return true
end

15:50 you have found 1 statues
15:50 you have found 2 statues
15:50 you have found 3 statues

Is this a good way to do it?

Thanks for the help guys :)
Lua:
If player:getStorageValue(16128) == 1 then
Player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, “you have found 1 statue”) 
else
<<old message here>>
end
 
Back
Top