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

RevScripts check in various areas

alcapone

Member
Joined
Jan 13, 2021
Messages
246
Reaction score
19
Lua:
local config = {
    actionId = 21501,


    Area1 = {
        fromPos = Position(20444, 20255, 1), -- Upper left corner of the room
        toPos = Position(20541, 20309, 1), -- Lower right corner of the room
        entrancePos = Position(20450, 20293, 1),
        exitPosition = Position(20377, 20286, 3)
    },
    Area2 = {
        fromPos = Position(20444, 20255, 1), -- Upper left corner of the room
        toPos = Position(20541, 20309, 1), -- Lower right corner of the room
        entrancePos = Position(20450, 20293, 1),
        exitPosition = Position(20377, 20286, 3)
    },
    Area3 = {
        fromPos = Position(20444, 20255, 1), -- Upper left corner of the room
        toPos = Position(20541, 20309, 1), -- Lower right corner of the room
        entrancePos = Position(20450, 20293, 1),
        exitPosition = Position(20377, 20286, 3)
    },

    participantsPos = {
        Position(20380, 20282, 3)
    },
    attempts = {
        level = 100, -- Level required to enter
          seconds = 72000 -- 20 hours       
    },
    kickAfterSeconds = 60 * 1, -- 25 minutes

}

local function getSpectators()
    if not config.centerPosition then
        config.diffX = math.ceil((config.Area1.toPos.x - config.Area1.fromPos.x) / 2)
        config.diffY = math.ceil((config.Area1.toPos.y - config.Area1.fromPos.y) / 2)
        config.centerPosition = config.Area1.fromPos + Position(config.diffX, config.diffY, 0)
    end
    return Game.getSpectators(config.centerPosition, false, false, config.diffX, config.diffX, config.diffY, config.diffY)
end

local test = Action()

function test.onUse(player, item, fromPos, target, toPos, isHotkey)
 
 
    local participants = {}
    for index, pos in pairs(config.participantsPos) do
        local tile = Tile(pos)
      
      if not tile then error("[Warning - Tile not found]") end
      
      local participant = tile:getTopVisibleCreature(player)
      
      if participant and participant:isPlayer() then
            if index == 1 and participant ~= player then
                player:sendCancelMessage("Only the first participant can pull the lever.")
                return true
            end

      

            participants[#participants +1] = participant
        end
    end

    local spectators = getSpectators()
    for _, spectator in pairs(spectators) do
        if spectator:isPlayer() then
            player:sendCancelMessage("At this time the room is occupied, please try again later.")
            return true
        end
    end

 
    for _, spectator in pairs(spectators) do spectator:remove() end
    
        for index, participant in pairs(participants) do
        participant:teleportTo(config.Area1.entrancePos, false)
              
 
        end
    
        
    config.kickEventId = addEvent(function ()
        for _, spectator in pairs(getSpectators()) do
            if spectator:isPlayer() then
      
                spectator:teleportTo(config.Area1.exitPosition, false)
                config.Area1.exitPosition:sendMagicEffect(CONST_ME_TELEPORT)
                spectator:sendTextMessage(MESSAGE_EVENT_ADVANCE, "It's been a long time and you haven't managed to defeat the boss.")
            else
                spectator:remove()
            end
        end
    end, config.kickAfterSeconds * 1000)


 
    return true
end

test:aid(config.actionId)
test:register()

Good night, I'm trying to make that when the player pulls the lever he will be teleported to area1 if there is a player in area1 he will be teleported to area2.
with a time limit of 30 minutes
 
Have you written that code or are you trying to modify someone else's code? But I can tell you that it is possible to achieve what you are trying to do, and using spectators is one way to do it.

Here are some things I thought about when reading that code:

1. All your areas are the same area. Is this intentional?
2. On line 78 it seems to me that you are removing players using spectator:remove(). I have never tried to do this, and I don't really want to do this.. What is the purpose of this line?
3. On line 80-84, this is where you need to select which room to teleport to. You could have something like:
Lua:
local function getVacantRoom(rooms)
for _, room in pairs(rooms) do
   
    local spectators = getSpectators()
    if #spectators == 0 then
        return room
    end
end

In that case you would have to change your config setup slightly, so that all the rooms are within the same thing.
And change some more things, for instance, your Game.getSpectators() should have the parameters (centerPos, false, true), not (centerPos, false, false) (the second false set to true means that you are only looking for players) If you want to find both monsters and players (maybe to remove old monsters so that you can spawn new ones) then you would have to add a loop that itereates over every spectator and returns only if it doesn't find any players.

The thing is, making this code takes a bit of time and I'm sorry but I don't have it at the moment. But if you wrote the code in your post, hopefully you get what I'm on about and you can use that to figure it out! :) I might have time later this week for this. If so I'll reply then. Otherwise, good luck!

Oh, and one last note. You should probably make it so that the game does not try to teleport players out with an addEvent(). This is problematic, because it can cause server crashes in some situations, for instance if the player dies and logs out, so that the player is no longer online when the game tries to teleport it. It is better to trigger the teleport function to something immediate, like when a new player tries to enter. Otherwise you first need to check if that player is actually a player before trying to teleport it away, and you must never use Player() userdata, but only the cid. I'm pretty sure that Game.getSpectators() returns userdata values, and not only cid's, but I might be wrong.
 
Back
Top