• 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 check if player is in current town

johnsamir

Advanced OT User
Joined
Oct 13, 2009
Messages
847
Solutions
6
Reaction score
151
Location
Nowhere
Hi

Im using a change map global event in my server tfs 1.4 taken from here:

the change map work partially good, the problem is that it always teleport player to teleport when log in, does not matter if player is in current town already

so i have been looking how to fix this but with no solution, this part of the script in charge to check or do this goes at creaturescripts/login.lua:
Lua:
  local currentTown = Game.getStorageValue(100) or 1
    if currentTown > 0 and player:getTown() ~= currentTown then
        player:teleportTo(Town(currentTown):getTemplePosition())
    end

have tried with
Code:
if currentTown > 0 and player:getPosition() ~= currentTown or if player:getStorageValue(43214) then
        player:teleportTo(Town(currentTown):getTemplePosition())
end

Code:
if  not currentTown  then
        player:teleportTo(Town(currentTown):getTemplePosition())
end
and

Code:
          if not currentTown and player:getTown() ~= currentTown then -- code original
        player:teleportTo(Town(currentTown):getTemplePosition())
        return true
   --aregado war
elseif player:getPosition(Town(currentTown)) == currentTown then
    doPlayerSendTextMessage(cid, 20, "This is the current town")
    return true
end
    return true
end

all editions or does not work or works partially like fordoes not check if player is not in current town to teleport when log in and have to wait until the next map change or it does simply teleport the player to the current town, temple no matter if player is in current town or not

can somebody give me a hand with this?


thank
 
Solution
Did a call over discord and decided on this solution

add into a login script
Lua:
local config = {
    townIds = {
        [1] = {topLeftCorner = Position(32382, 32244, 0), bottomRightCorner = Position(32519, 32362, 15)}, -- "Thais"
        [2] = {topLeftCorner = Position(32406, 32144, 0), bottomRightCorner = Position(32462, 32196, 15)}, -- "Edron"
        [3] = {topLeftCorner = Position(733, 963, 0), bottomRightCorner = Position(870, 1079, 15)}, -- "Carlin"
        [4] = {topLeftCorner = Position(1006, 984, 0), bottomRightCorner = Position(1181, 1144, 15)}, -- "Venore"
        [5] = {topLeftCorner = Position(759, 195, 0), bottomRightCorner = Position(947, 328, 15)} -- "Werida"
        --[6] = {topLeftCorner = Position(1000, 1000, 7)...
player:getTown() get's a town's userdata
player:getTown():getId() get's the town number, which can be used to compare against, like you initially tried to do.

Lua:
local currentTown = Game.getStorageValue(100) or 1
if currentTown > 0 and player:getTown():getId() ~= currentTown then
    player:teleportTo(Town(currentTown):getTemplePosition())
end
 
player:getTown() get's a town's userdata
player:getTown():getId() get's the town number, which can be used to compare against, like you initially tried to do.

Lua:
local currentTown = Game.getStorageValue(100) or 1
if currentTown > 0 and player:getTown():getId() ~= currentTown then
    player:teleportTo(Town(currentTown):getTemplePosition())
end
with that is checking supposedly cheking if player get or is in current town id but does not works
i was trying something like this with the solution you gave me but does not work or gives console error either if im not in current town for example change map has rotate to town 5 and player log in but it was in town 4 it stands there till the next change map
Code:
       local currentTown = Game.getStorageValue(43214) or 1
        local playerPosition = player:getPosition() -- added
          if not currentTown and player:getTown():getId() ~= currentTown then -- code original
        player:teleportTo(Town(currentTown):getTemplePosition())
        return true
   --aregado war
elseif player:getPosition(Town(currentTown)) == currentTown then
    doPlayerSendTextMessage(cid, 20, "This is the current town")
    return true
end
    return true
end

and tried this, but it keeps teleporting the player always does not matters if it's already in the current map
Code:
          if currentTown > 0 and player:getTown():getId() ~= currentTown then
        player:teleportTo(Town(currentTown):getTemplePosition())
        return true
   --aregado war
elseif player:getPosition(Town(currentTown)) == currentTown then
    doPlayerSendTextMessage(cid, 20, "This is the current town")
    return true
end
    return true
end

the same with this attempt, the player is being teleport to temple even if it's already in the current town
Code:
          if currentTown > 0 and player:getTown():getId() ~= currentTown then
        player:teleportTo(Town(currentTown):getTemplePosition())
        return true
   --aregado war
elseif player:getTown():getId() == currentTown then--player:getPosition(Town(currentTown)) == currentTown then
    doPlayerSendTextMessage(cid, 20, "This is the current town")
    return true
end
    return true
end
 
Figure out what's wrong by printing values.

Lua:
local currentTown = Game.getStorageValue(100) or 1
print("currentTown = " .. currentTown)
print("player:getTown():getId() = " .. player:getTown():getId())
if currentTown > 0 and player:getTown():getId() ~= currentTown then
    print("player teleported to town")
    player:teleportTo(Town(currentTown):getTemplePosition())
else
    print("player was not teleported as town was the same as current")
end
 
tried this same errors
Lua:
if currentTown and player:getTown():getId() ~= currentTown then
    player:teleportTo(Town(currentTown):getTemplePosition())
end
    return true
end

this were the results with your script, this is what i get in login and in change map
Code:
 ---when changed a player from current map and log in again ( player wasn't being teleported to current town on login , only when change map function is executed)
 GOD Of War has logged in.
currentTown = 1
player:getTown():getId() = 2
player teleported to town
Qweqw has logged in.
currentTown = 1
player:getTown():getId() = 2
player teleported to town
Pastyx has logged in.
currentTown = 1
player:getTown():getId() = 2
player teleported to town
 shop!
The map has been changed to Venore Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to Werida Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to sanislan Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to Thais Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to Edron Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to Carlin Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
The map has been changed to Venore Next map change will be in 30 minutes! Type !shop list to view the items available in shop!
----when changed a player from current map and log in again ( player wasn't being teleported to current town on login , only when change map function is executed)
Jhoony has logged out.
Jhoony has logged in.
currentTown = 4
player:getTown():getId() = 4
player was not teleported as town was the same as current
Jhoony has logged out.
Jhoony has logged in.
currentTown = 4
player:getTown():getId() = 4
player was not teleported as town was the same as current
Jhoony has logged out.
Jhoony has logged in.
currentTown = 4
player:getTown():getId() = 4
player was not teleported as town was the same as current
Jhoony has logged out.
Jhoony has logged in.
currentTown = 4
player:getTown():getId() = 4
player was not teleported as town was the same as current
Jhoony has logged out.
Jhoony has logged in.
currentTown = 4
player:getTown():getId() = 4
player was not teleported as town was the same as current
@Xikini
 
It.. appears to be working as intended?

Player logins.
-> player is not in correct town, get's teleported to current town.
-> player is in current town, does not get teleported.

Is this not what you want to happen?
 
It.. appears to be working as intended?

Player logins.
-> player is not in correct town, get's teleported to current town.
-> player is in current town, does not get teleported.

Is this not what you want to happen?
it was teleported by the change map function but not on login that's what's missing
 
Did a call over discord and decided on this solution

add into a login script
Lua:
local config = {
    townIds = {
        [1] = {topLeftCorner = Position(32382, 32244, 0), bottomRightCorner = Position(32519, 32362, 15)}, -- "Thais"
        [2] = {topLeftCorner = Position(32406, 32144, 0), bottomRightCorner = Position(32462, 32196, 15)}, -- "Edron"
        [3] = {topLeftCorner = Position(733, 963, 0), bottomRightCorner = Position(870, 1079, 15)}, -- "Carlin"
        [4] = {topLeftCorner = Position(1006, 984, 0), bottomRightCorner = Position(1181, 1144, 15)}, -- "Venore"
        [5] = {topLeftCorner = Position(759, 195, 0), bottomRightCorner = Position(947, 328, 15)} -- "Werida"
        --[6] = {topLeftCorner = Position(1000, 1000, 7), bottomRightCorner = Position(1000, 1000, 7)}  -- "sanislan"
    },
    globalStorage = 43214
}

-- inside your login function
    -- teleport player to current map town, if they are not inside it.
    local currentTown = Game.getStorageValue(config.globalStorage)
    if not currentTown then
        currentTown = 1
    end
   
    if not player:getPosition():isInRange(config.townIds[currentTown].topLeftCorner, config.townIds[currentTown].bottomRightCorner)
        print("Teleporting player, as they are out of bounds.")
        player:setTown(currentTown)
        player:teleportTo(Town(currentTown):getTemplePosition())
    end

and change map
Lua:
local config = {
    townIds = {
        [1] = "Thais",
        [2] = "Edron",
        [3] = "Carlin",
        [4] = "Venore",
        [5] = "Werida",
        [6] = "sanislan"
    },
    globalStorage = 43214
}

function onThink(interval, lastExecution)
    local players = Game.getPlayers()
    if #players == 0 then
        return true
    end
   
    local newTown = Game.getStorageValue(config.globalStorage)
    if not newTown or newTown == #config.townIds then
        newTown = 1
    else
        newTown = newTown + 1
    end
    Game.setStorageValue(config.globalStorage, newTown)
   
    local position = Town(newTown):getTemplePosition()
   
    for i = 1, #players do
        local player = players[i]
        player:setTown(newTown) -- want to setTown regardless, in case they die, they will spawn in correct town
        if not Tile(player:getPosition()):hasFlag(TILESTATE_PVPZONE) then -- if player is not in town.. in an event.. don't teleport?
            player:teleportTo(position)
            player:addHealth(player:getMaxHealth())
            player:addMana(player:getMaxMana())
        end
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "The map has been changed to ".. config.townIds[newTown] .." Next map change will be in 30 minutes! Type !shop list to view the items available in shop!")
    end
    position:sendMagicEffect(CONST_ME_TELEPORT)
    print("The map has been changed to ".. config.townIds[newTown] .." Next map change will be in 30 minutes! Type !shop list to view the items available in shop!")
    return true
end
 
Last edited:
Solution
Back
Top