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

[Help] Position on storage

enemyy

Member
Joined
Aug 11, 2017
Messages
28
Reaction score
5
hello dear friends,

how can i save the player position in a storage?

and after this, get this position back in another script...

ps:

i need this to work in a "waypoint back" action script
i'm using TFS 1.3, to 10.98 version

I've tried these lines of code:
in first action item:
Lua:
local storage = 15636  --storage for example

  setPlayerStorageValue(cid, storage, player:getPosition())  -- to save player position

in second action item:
Lua:
   player:teleportTo(cid, getPlayerStorageValue(cid, 15636))  -- to get position back

but I had this error:
Lua Script Error: [Action Interface]
data/actions/scripts/other/summon.lua:eek:nUse
attempt to index a nil value
stack traceback:
[C]: at 0x7ff6a2e092d0
[C]: in function 'teleportTo'
data/actions/scripts/other/homeward:35: in function <data/actions/scripts/other/homeward.lua:14>
 
Solution
Lua:
function Player.savePosition(self, storage)
    self:setStorageValue(storage, self:getPosition().x)
    self:setStorageValue(storage+1, self:getPosition().y)
    self:setStorageValue(storage+2, self:getPosition().z)
end

function Player.loadPosition(self, storage)
    return Position(self:getStorageValue(storage), self:getStorageValue(storage+1), self:getStorageValue(storage+2))
end

-- 15636 = x, 15637 = y,  15638 = z
local start = 15636
player:teleportTo(player:loadPosition(start))
Lua:
function Player.savePosition(self, storage)
    self:setStorageValue(storage, self:getPosition().x)
    self:setStorageValue(storage+1, self:getPosition().y)
    self:setStorageValue(storage+2, self:getPosition().z)
end

function Player.loadPosition(self, storage)
    return Position(self:getStorageValue(storage), self:getStorageValue(storage+1), self:getStorageValue(storage+2))
end

-- 15636 = x, 15637 = y,  15638 = z
local start = 15636
player:teleportTo(player:loadPosition(start))
 
Solution
Lua:
function Player.savePosition(self, storage)
    self:setStorageValue(storage, self:getPosition().x)
    self:setStorageValue(storage+1, self:getPosition().y)
    self:setStorageValue(storage+2, self:getPosition().z)
end

function Player.loadPosition(self, storage)
    return Position(self:getStorageValue(storage), self:getStorageValue(storage+1), self:getStorageValue(storage+2))
end

-- 15636 = x, 15637 = y,  15638 = z
local start = 15636
player:teleportTo(player:loadPosition(start))
Glad you are still around and doing well mate! How are things going? :)
 
Glad you are still around and doing well mate! How are things going? :)
By this way u're using 3 storages. I think it's better to use .gsub and transform positions table to string, and then save it to storage + add function to convert string to table again.
 
@Evil Hero

like this ?

Lua:
local WINDOW_ID = 4203
local BUTTON_ACCEPT = 0
local BUTTON_CLOSE = 1
local storage = 15636 -- < --- storage

local WAYPOINTS_STORAGE = 41875
local WAYPOINTS = {
  [1] = {
    name = "Rhyves Town",
    position = Position(32984, 31284, 6)
  },
  [2] = {
    name = "Deep",
    position = Position(32858, 31291, 8)
  },
  [3] = {
    name = "Firelink Shrine",
    position = Position(32858, 31316, 6)
  },
}

function Player.savePosition(self, storage) -- < -- save function
    self:setStorageValue(storage, self:getPosition().x)
    self:setStorageValue(storage+1, self:getPosition().y)
    self:setStorageValue(storage+2, self:getPosition().z)
end


function onUse(player, item, position, fromPosition)

local itenx = item:getPosition()
  if player:isPlayer() then
  Player.savePosition(self, storage) -- < --- command to save position
   
    for i = 1, #WAYPOINTS do
      local waypoint = WAYPOINTS[i]
      if item:getPosition() == waypoint.position and player:getStorageValue(WAYPOINTS_STORAGE + i) ~= 1 then
        player:setStorageValue(WAYPOINTS_STORAGE + i, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "New waypoint unlocked!\n-- " .. waypoint.name .. " --")
        item:sendMagicEffect(CONST_ME_POFF)
        return true
      end
    end

    local empty = true
    for i = 1, #WAYPOINTS do
      local waypoint = WAYPOINTS[i]
      if item:getPosition() == waypoint.position and player:getStorageValue(WAYPOINTS_STORAGE + i) == 1 then
        empty = false
        break
      end
    end

    if not empty then
      player:registerEvent("WaypointsModal")
     
      local title = "Bonfire Warp"
      local message = "Choose your destination."
     
      local window = ModalWindow(WINDOW_ID, title, message)

      window:addButton(BUTTON_ACCEPT, "Teleport")
      window:addButton(BUTTON_CLOSE, "Close")

      for i = 1, #WAYPOINTS do
        local waypoint = WAYPOINTS[i]
        if player:getStorageValue(WAYPOINTS_STORAGE + i) == 1 then
          window:addChoice(i, waypoint.name)
        end
      end
      window:setDefaultEnterButton(BUTTON_ACCEPT)
      window:setDefaultEscapeButton(BUTTON_CLOSE)

      window:sendToPlayer(player)
    end
  end
    return true
end

function onWaypointsModal(player, modalWindowId, buttonId, choiceId)
  player:unregisterEvent("WaypointsModal")


  if modalWindowId == WINDOW_ID then
      if buttonId == BUTTON_ACCEPT then
      WAYPOINTS[choiceId].position.y = WAYPOINTS[choiceId].position.y + 1
        if player:getStorageValue(WAYPOINTS_STORAGE + choiceId) == 1 then
          player:teleportTo(WAYPOINTS[choiceId].position)
          setPlayerStorageValue(cid, 15636,WAYPOINTS[choiceId].position)
          WAYPOINTS[choiceId].position:sendMagicEffect(CONST_ME_FIREAREA)
          WAYPOINTS[choiceId].position.y = WAYPOINTS[choiceId].position.y - 1
        end
      end
  end
end

i had this error back:

Lua Script Error: [Action Interface]
data/actions/scripts/other/waypoints.lua:eek:nUse
data/actions/scripts/other/waypoints.lua:23: attempt to index local 'self' (a nil value)
stack traceback:
[C]: in function '__index'
data/actions/scripts/other/waypoints.lua:23: in function 'savePosition'
data/actions/scripts/other/waypoints.lua:33: in function <data/actions/scripts/other/waypoints.lua:29>
 
@Evil Hero

like this ?

Lua:
local WINDOW_ID = 4203
local BUTTON_ACCEPT = 0
local BUTTON_CLOSE = 1
local storage = 15636 -- < --- storage

local WAYPOINTS_STORAGE = 41875
local WAYPOINTS = {
  [1] = {
    name = "Rhyves Town",
    position = Position(32984, 31284, 6)
  },
  [2] = {
    name = "Deep",
    position = Position(32858, 31291, 8)
  },
  [3] = {
    name = "Firelink Shrine",
    position = Position(32858, 31316, 6)
  },
}

function Player.savePosition(self, storage) -- < -- save function
    self:setStorageValue(storage, self:getPosition().x)
    self:setStorageValue(storage+1, self:getPosition().y)
    self:setStorageValue(storage+2, self:getPosition().z)
end


function onUse(player, item, position, fromPosition)

local itenx = item:getPosition()
  if player:isPlayer() then
  Player.savePosition(self, storage) -- < --- command to save position
 
    for i = 1, #WAYPOINTS do
      local waypoint = WAYPOINTS[i]
      if item:getPosition() == waypoint.position and player:getStorageValue(WAYPOINTS_STORAGE + i) ~= 1 then
        player:setStorageValue(WAYPOINTS_STORAGE + i, 1)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "New waypoint unlocked!\n-- " .. waypoint.name .. " --")
        item:sendMagicEffect(CONST_ME_POFF)
        return true
      end
    end

    local empty = true
    for i = 1, #WAYPOINTS do
      local waypoint = WAYPOINTS[i]
      if item:getPosition() == waypoint.position and player:getStorageValue(WAYPOINTS_STORAGE + i) == 1 then
        empty = false
        break
      end
    end

    if not empty then
      player:registerEvent("WaypointsModal")
   
      local title = "Bonfire Warp"
      local message = "Choose your destination."
   
      local window = ModalWindow(WINDOW_ID, title, message)

      window:addButton(BUTTON_ACCEPT, "Teleport")
      window:addButton(BUTTON_CLOSE, "Close")

      for i = 1, #WAYPOINTS do
        local waypoint = WAYPOINTS[i]
        if player:getStorageValue(WAYPOINTS_STORAGE + i) == 1 then
          window:addChoice(i, waypoint.name)
        end
      end
      window:setDefaultEnterButton(BUTTON_ACCEPT)
      window:setDefaultEscapeButton(BUTTON_CLOSE)

      window:sendToPlayer(player)
    end
  end
    return true
end

function onWaypointsModal(player, modalWindowId, buttonId, choiceId)
  player:unregisterEvent("WaypointsModal")


  if modalWindowId == WINDOW_ID then
      if buttonId == BUTTON_ACCEPT then
      WAYPOINTS[choiceId].position.y = WAYPOINTS[choiceId].position.y + 1
        if player:getStorageValue(WAYPOINTS_STORAGE + choiceId) == 1 then
          player:teleportTo(WAYPOINTS[choiceId].position)
          setPlayerStorageValue(cid, 15636,WAYPOINTS[choiceId].position)
          WAYPOINTS[choiceId].position:sendMagicEffect(CONST_ME_FIREAREA)
          WAYPOINTS[choiceId].position.y = WAYPOINTS[choiceId].position.y - 1
        end
      end
  end
end

i had this error back:

Lua Script Error: [Action Interface]
data/actions/scripts/other/waypoints.lua:eek:nUse
data/actions/scripts/other/waypoints.lua:23: attempt to index local 'self' (a nil value)
stack traceback:
[C]: in function '__index'
data/actions/scripts/other/waypoints.lua:23: in function 'savePosition'
data/actions/scripts/other/waypoints.lua:33: in function <data/actions/scripts/other/waypoints.lua:29>
at line 33 change
Lua:
Player.savePosition(self, storage) -- < --- command to save position
to
Lua:
player:savePosition(storage) -- < --- command to save position

atleast thats how i understand it by the example code how to load the position
 
By this way u're using 3 storages. I think it's better to use .gsub and transform positions table to string, and then save it to storage + add function to convert string to table again.
TFS 1.3 does not support storageValues of string type, hence we have to stick to 3 different storageValues
 
Back
Top