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

Clear Area if player is off the screen

Count Dracula

New Member
Joined
Jul 30, 2013
Messages
36
Solutions
1
Reaction score
1
Hello guys

As the title says, I'm trying to remove two kinds of monsters in the screen if there is no player, but with addEvent...

But after a lot of try and error, nothing worked..

My tfs version is 1.2

Thanks!
 
Solution
It seems to me it would still remove the monsters even if player is there.
you're right, in the case of a monster being in the spec list before a player
this should fix it
Lua:
local function clearArena(pos, monsters)
    local playerInside = #Game.getSpectators(pos, false, true, 15, 15, 15, 15) > 0
    if playerInside then
        return
    end
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})
What have you tried?

Code:
local function clearArena()
    local spectators = Game.getSpectators(Position(1, 2, 3), false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isPlayer() then
        return true
        end
        if spectator:getName():lower() == 'monster1' or spectator:getName():lower() == 'monster2'  then
            spectator:remove()
        end
    end
end

This for example, doesn't work.
 
Could you be more descriptive than "doesn't work". Does it give you error? What does it do and what was it expected to do?
 
Lua:
local function clearArena()
    local spectators = Game.getSpectators(Position(1, 2, 3), false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isPlayer() then
        return true
        end
        if spectator:getName():lower() == 'monster1' or spectator:getName():lower() == 'monster2'  then
            spectator:remove()
        end
    end
end

This for example, doesn't work.
Lua:
local function clearArena(pos, monsters)
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isMonster() and isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})
 
Last edited by a moderator:
Could you be more descriptive than "doesn't work". Does it give you error? What does it do and what was it expected to do?

It doesn't give any errors @Colandus , simply doesn't work. It keeps removing the creatures even if has players on the screen..

Removing the monsters is easy, but only remove them if the players are off the screen it's the tricky part.

Lua:

Lua:
local function clearArena(pos, monsters)
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isMonster() and isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})
]

This only make remove the monsters on the screen @Xeraphus but I want it to work only if there is no players on it.
 
It doesn't give any errors @Colandus , simply doesn't work. It keeps removing the creatures even if has players on the screen..

Removing the monsters is easy, but only remove them if the players are off the screen it's the tricky part.



This only make remove the monsters on the screen @Xeraphus but I want it to work only if there is no players on it.
oh i forgot
Lua:
local function clearArena(pos, monsters)
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isPlayer() then
            return
        end
        if isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})
 
oh i forgot
Lua:
local function clearArena(pos, monsters)
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if spectator:isPlayer() then
            return
        end
        if isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})

Thank you so much @Xeraphus
I only changed it to work as an event, and it worked flawless.
 
It seems to me it would still remove the monsters even if player is there.
you're right, in the case of a monster being in the spec list before a player
this should fix it
Lua:
local function clearArena(pos, monsters)
    local playerInside = #Game.getSpectators(pos, false, true, 15, 15, 15, 15) > 0
    if playerInside then
        return
    end
    local spectators = Game.getSpectators(pos, false, false, 15, 15, 15, 15)
    for i = 1, #spectators do
        local spectator = spectators[i]
        if isInArray(monsters, spectator:getName():lower()) then
            spectator:remove()
        end
    end
end

clearArena(Position(x, y, z), {"monster 1", "monster 2"})
 
Last edited by a moderator:
Solution
In my case, the monster only appears if I step in a certain tile inside the room, so it's impossible the creature is there before the player..

But I didn't really understand what changes you made.. can you please explain me?
 
In my case, the monster only appears if I step in a certain tile inside the room, so it's impossible the creature is there before the player..

But I didn't really understand what changes you made.. can you please explain me?
Now it always checks if there is a player there first. Then if it's found it will continue to remove the monsters.

If you don't mind trying the last code and tell us if it works so it could be marked as best answer considering it's more accurate (for other scenarios as well).
 
Now it always checks if there is a player there first. Then if it's found it will continue to remove the monsters.

If you don't mind trying the last code and tell us if it works so it could be marked as best answer considering it's more accurate (for other scenarios as well).

I tried and also works for me.
 
In my case, the monster only appears if I step in a certain tile inside the room, so it's impossible the creature is there before the player..

But I didn't really understand what changes you made.. can you please explain me?
i used the "playerOnly" argument in the getSpectators function, and compared the size of the table it returns with 0
if it's greater than 0, there's at least 1 player in there, otherwise the statement wont pass and it will go through and delete monsters instead.
 
Back
Top