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

TFS 1.X+ Error during a check if have players on the positions in globalevents TFS 1.2 / 1.3

Yan18

Member
Joined
Jun 14, 2014
Messages
104
Solutions
3
Reaction score
17
Hello everyone!

I did a script to verify if there are players that aren't premium in specific positions in globalevents (just online players), but generates an error in the distro :

Lua Script Error: [GlobalEvent Interface] data/globalevents/scripts/Teste.lua:onThink data/globalevents/scripts/Teste.lua:16: attempt to index a nil value stack traceback: [C]: in function '__index' data/globalevents/scripts/Teste.lua:16: in function <data/globalevents/scripts/Teste.lua:8> [Error - GlobalEvents::think] Failed to execute event: Teste

my code:

Lua:
local position_areavip = {
from_pos = {x= 1, y = 1, z= 7},
to_pos = {x= 150, y = 150, z= 7},
}

local position_templo = Position(97, 130, 7)

function onThink(interval)
if #getOnlinePlayers() > 0 then
    for _, players in pairs (getOnlinePlayers()) do
    local player = Player(players)
       
        for i = position_areavip.from_pos.x, position_areavip.to_pos.x do
            for j = position_areavip.from_pos.y, position_areavip.to_pos.y do
                for k = position_areavip.from_pos.z, position_areavip.to_pos.z do
                    if Tile(Position(i, j, k)):getTopCreature():isPlayer() then
                        if player:getPremiumDays() <= 0 then
                            doTeleportThing(player, position_templo)
                        end  
                    end  
                end
            end
        end
       
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have been teleported to temple because you lost your premium.")
    end

else
  return true
end

return true
end

I tested with direct position and works, but with iterator, generates error.

I believe that error happened because when iterate in the first position and don't find a player, generates the error.

Where is the problem?
 
Last edited:
Solution
Test with this code!
Lua:
local config = {
    centerPosition = Position(75, 75, 7),
    range = 75,
    exitPosition = Position(200, 200, 7) -- or Town(townId):getTemplePosition()
}
function onThink(interval, lastExecution)
    local spectators = Game.getSpectators(config.centerPosition, false, true, config.range, config.range, config.range, config.range)
    for _, player in pairs(spectators) do
        if not player:isPremium() then
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
            player:teleportTo(config.exitPosition)
            config.exitPosition:sendMagicEffect(CONST_ME_TELEPORT)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have been teleported to temple because you lost your...
Test with this code!
Lua:
local config = {
    centerPosition = Position(75, 75, 7),
    range = 75,
    exitPosition = Position(200, 200, 7) -- or Town(townId):getTemplePosition()
}
function onThink(interval, lastExecution)
    local spectators = Game.getSpectators(config.centerPosition, false, true, config.range, config.range, config.range, config.range)
    for _, player in pairs(spectators) do
        if not player:isPremium() then
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
            player:teleportTo(config.exitPosition)
            config.exitPosition:sendMagicEffect(CONST_ME_TELEPORT)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have been teleported to temple because you lost your premium.")
        end
    end
    return true 
end
 
Solution
Test with this code!
Lua:
local config = {
    centerPosition = Position(75, 75, 7),
    range = 75,
    exitPosition = Position(200, 200, 7) -- or Town(townId):getTemplePosition()
}
function onThink(interval, lastExecution)
    local spectators = Game.getSpectators(config.centerPosition, false, true, config.range, config.range, config.range, config.range)
    for _, player in pairs(spectators) do
        if not player:isPremium() then
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
            player:teleportTo(config.exitPosition)
            config.exitPosition:sendMagicEffect(CONST_ME_TELEPORT)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have been teleported to temple because you lost your premium.")
        end
    end
    return true
end

Thanks to answer my friend! It works!

I migrated from TFS 0.x to 1.x recently haha, in TFS 0.x I needed to iterate with for loop to check the areas. getSpectator is very useful and functional! The only negative thing that I thought is you need to calculate the range, if you have a big area, it's a little boring haha. But thanks very much!
 
isn't it better (using less server resources) to use an onlogin script for this? something like this:
Lua:
    if player:getPremiumDays() == 0 then
        player:setTown(Town(1))
        player:teleportTo(player:getTown():getTemplePosition())
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Your premium time expired.")
    end
 
isn't it better (using less server resources) to use an onlogin script for this? something like this:
Lua:
    if player:getPremiumDays() == 0 then
        player:setTown(Town(1))
        player:teleportTo(player:getTown():getTemplePosition())
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "Your premium time expired.")
    end
I thought in create a script onLogin, but I need to check a specific area premium to teleport all players that aren't vip and they are inside it. And I thought Globalevents is better because a player can't lost your premium during the game, and if I had made in onLogin, the players just will teleported in your next login, and so they would be in the premium area even not being premium account.

But thanks for the sugestion my friend!
 
Back
Top