• 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 [Solved]Help, i can't teleport players from a town

Kaivan

New Member
Joined
Apr 29, 2021
Messages
13
Reaction score
1
Hello,
I'm racking my brain trying to do this, but it should actually be simple:
I created a Global event that spawns an NPC that teleports you, and set your Town to 3, and in that same event a Timer for the NPC to "go away".
Lua:
local npc_name = "Captain Barry" -- npc name
function onThink(interval)
    Game.createNpc(npc_name, Position(297, 270, 6), false, true)
    addEvent(ByeNPC, 60000)
    return true
end

So far so good, but I would like that after the timer in which the NPC is removed, all online players would be teleported to a position outside the event, this within the function created in "addEvent", so i trying like this:
Lua:
function ByeCaptain()
    broadcastMessage("[SHIPMENT] Captain Barry come out!")
    doRemoveCreature(npc_name)
    for index, creature in ipairs(getPlayersOnline()) do
            doTeleportThing(creature, 297, 272, 6)
    end
    return true
end

Despite the ByeCaptain event running, I can't get all players to teleport, so I need some help with that. Ty
 
Solution
There is no TFS version but i assumed you are using TFS 1.x

getPlayersOnline() returns a list with players name instead the userdata as you can see here lib/compat/compat.lua
Lua:
function getOnlinePlayers()
    local result = {}
    for _, player in ipairs(Game.getPlayers()) do
        result[#result + 1] = player:getName()
    end
    return result
end

That means you need to create userdata again
Lua:
for _, player in ipairs(getPlayersOnline()) do
    player = Player(player)
    if player then
       player:teleportTo(Position(297,272,6))
    end
end

Or you could simply use Game.getPlayers() instead which returns the userdata
Lua:
for _, player in ipairs(Game.getPlayers()) do...
There is no TFS version but i assumed you are using TFS 1.x

getPlayersOnline() returns a list with players name instead the userdata as you can see here lib/compat/compat.lua
Lua:
function getOnlinePlayers()
    local result = {}
    for _, player in ipairs(Game.getPlayers()) do
        result[#result + 1] = player:getName()
    end
    return result
end

That means you need to create userdata again
Lua:
for _, player in ipairs(getPlayersOnline()) do
    player = Player(player)
    if player then
       player:teleportTo(Position(297,272,6))
    end
end

Or you could simply use Game.getPlayers() instead which returns the userdata
Lua:
for _, player in ipairs(Game.getPlayers()) do
    player:teleportTo(Position(297,272,6))
end

In addition, you could do a check on player residence to only teleport those players with Town ID 3
Lua:
if player:getTown():getId() == 3 then
    -- code
end
 
Last edited:
Solution
Back
Top