• 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 addevent debbug

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
What is happening is that if they pull the switch and people leave the waiting room it just crashes the server, does anyone know a solution for this?

Lua:
local t = {
    Position(299, 219, 10), -- Alavanca
}

local function backLever()
    local tile = t[1]:getTile() 
  
    local alavanca = tile:getItemById(9826)

    if alavanca then
        alavanca:transform(9825)
    end
end

local function startArena(creatures)
    for _, pid in ipairs(creatures) do
        pid:sendTextMessage(MESSAGE_INFO_DESCR, 'Você foi teleportado para a arena, sobreviva até o fim para receber a sua recompensa !!!')
      
        _CHAMPARENA_LH.addPlayer(pid)
      
        if Game.getStorageValue(_CHAMPARENA_LH.global_storage) == -1 then
            Game.setStorageValue(_CHAMPARENA_LH.global_aux, -1)             
            _CHAMPARENA_LH.open()
        end 
    end 
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 9825 then
      
        local count, creatures = 0, Game.getSpectators(Position(305, 218, 10), false, false, 10, 10, 10, 10)
        if creatures ~= nil then
            for _, pid in ipairs(creatures) do
                if isPlayer(pid) then
                    count = count + 1
                end
            end
        end             
      
        if count >= 1 then
            for _, pid in ipairs(creatures) do
                pid:sendTextMessage(MESSAGE_INFO_DESCR, 'Aguarde 2 minutos para serem teleportados para Arena !!!')
            end
          
            addEvent(startArena, 2 * 6 * 1000, creatures)
            addEvent(backLever, 3 * 6 * 1000) 
            item:transform(item.itemid == 9826 and 9825 or 9826)
        end
    end
  
    return true 
end


will that solve the problem?
Lua:
local function startArena(creatures)
    if creatures then
        for _, pid in ipairs(creatures) do
            pid:sendTextMessage(MESSAGE_INFO_DESCR, 'Você foi teleportado para a arena, sobreviva até o fim para receber a sua recompensa !!!')
            
            _CHAMPARENA_LH.addPlayer(pid)
            
            if Game.getStorageValue(_CHAMPARENA_LH.global_storage) == -1 then
                Game.setStorageValue(_CHAMPARENA_LH.global_aux, -1)               
                _CHAMPARENA_LH.open()
            end   
        end   
    end
end
 
Solution
@darkmu
What TFS version are you using? Game.getSpectators returns table of Creature objects, which should not be passed as parameter to addEvent.
Why it crash:

There are also other problems.
  • Game.getSpectators(Position(305, 218, 10), false, false, 10, 10, 10, 10) - second false means returnOnlyPlayer = false and then you filter list by isPlayer
  • in TFS 1.2+ Game.getSpectators always returns list [can be empty], so this IF is always true if creatures ~= nil then
  • list of creatures is indexed by numbers 1,2,3.. so you don't need to...
I dont see a problem here it could be in other parts of the system.
LuaEnvironment::executeTimerEvent
it = {<No data fields>}

and in addevent, precisely because creatures I think are passing null for all left the room and I think addEvent cannot pass a null parameter
 
You are right. (I used to play Darkmu btw).

In your add event you have to check if the creatures exist.

Lua:
local function startArena(creatures)
    for _, pid in ipairs(creatures) do
        local player = Player(pid)
        if player then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'Você foi teleportado para a arena, sobreviva até o fim para receber a sua recompensa !!!')
            _CHAMPARENA_LH.addPlayer(player)
        end
    end
    
    Game.setStorageValue(_CHAMPARENA_LH.global_aux, -1)               
    _CHAMPARENA_LH.open()
end

Something like that.
 
@darkmu
What TFS version are you using? Game.getSpectators returns table of Creature objects, which should not be passed as parameter to addEvent.
Why it crash:

There are also other problems.
  • Game.getSpectators(Position(305, 218, 10), false, false, 10, 10, 10, 10) - second false means returnOnlyPlayer = false and then you filter list by isPlayer
  • in TFS 1.2+ Game.getSpectators always returns list [can be empty], so this IF is always true if creatures ~= nil then
  • list of creatures is indexed by numbers 1,2,3.. so you don't need to use for to count elements, you can use #creatures to get maxium index of table (in this case number of elements)

I'm not sure about that part:
Lua:
            if Game.getStorageValue(_CHAMPARENA_LH.global_storage) == -1 then
                Game.setStorageValue(_CHAMPARENA_LH.global_aux, -1)
                _CHAMPARENA_LH.open()
            end
It gets some key (_CHAMPARENA_LH.global_storage), but sets different (_CHAMPARENA_LH.global_aux).
It executes inside for loop. If there will be no players alive, it won't execute. I don't know if it's fine with this system logic.

Fixed script [except part with global storage values]:
Lua:
local t = {
    Position(299, 219, 10), -- Alavanca
}

local function backLever()
    local tile = t[1]:getTile()
    local alavanca = tile:getItemById(9826)

    if alavanca then
        alavanca:transform(9825)
    end
end

local function startArena(creatureIdsList)
    for _, pid in ipairs(creatureIdsList) do
        -- change IDs of creatures into Player objects
        local player = Player(pid)
        if player then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'Você foi teleportado para a arena, sobreviva até o fim para receber a sua recompensa !!!')

            _CHAMPARENA_LH.addPlayer(player)

            if Game.getStorageValue(_CHAMPARENA_LH.global_storage) == -1 then
                Game.setStorageValue(_CHAMPARENA_LH.global_aux, -1)
                _CHAMPARENA_LH.open()
            end
        end
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 9825 then
        -- get 'only players'
        local creatures = Game.getSpectators(Position(305, 218, 10), false, true, 10, 10, 10, 10)
        local count = #creatures

        if count >= 1 then
            for _, pid in ipairs(creatures) do
                pid:sendTextMessage(MESSAGE_INFO_DESCR, 'Aguarde 2 minutos para serem teleportados para Arena !!!')
            end

            -- change Player objects into IDs of creatures
            local creatureIdsList = {}
            for _, creature in pairs(creatures) do
                table.insert(creatureIdsList, creature:getId())
            end
            addEvent(startArena, 2 * 6 * 1000, creatureIdsList)
            addEvent(backLever, 3 * 6 * 1000)

            -- we checked above in IF that itemid is 9825, so we can change it to 9826 without checking itemid
            item:transform(9826)
        end
    end

    return true
end

Edit:
Forgot to mention that `addEvent`s are set to 12 (2 * 6 * 1000) and 18 (2 * 6 * 1000) seconds. In message is something about 2 minutes. It's probably change only for tests, but don't forget to replace 6 with 60 before you put it on production server.
 
Last edited:
Solution
Back
Top