• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua function getSpectators(centerPos, rangex, rangey, multifloor, onlyPlayers)

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
Hello, I'm trying to learn TFS 1.1 scripting tryed alot of different ways to make this but none seems to work...

This should check for monsters in a 20 sqm range

if theres monsters then player cant pass
if theres no monsters then player can pass


Code:
local radiusX = 20
local radiusY = 20
local areapos = getCreaturePosition(cid)

if getSpectators(areapos, false, false, 0, radiusX, 0, radiusY) >= 1 then
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'Kill every monsters before pass...')
   return true
end

Error:
Code:
stack traceback:
        [C]: at 0x7ff76358bb90
        [C]: in function 'getSpectators'
        data/lib/compat/compat.lua:966: in function 'getSpectators'
 
This is because getSpectators returns a table, try this
Code:
local radiusX = 20
local radiusY = 20
local areapos = getCreaturePosition(cid)

if #getSpectators(areapos, false, false, 0, radiusX, 0, radiusY) >= 1 then
    player:sendTextMessage(MESSAGE_INFO_DESCR, 'Kill every monsters before pass...')
   return true
end

The # symbol when applied to a table, returns the size of it, you can also use the # on a string
Code:
print(#"this is a test")
-- prints 14
 
A warning: the global function getSpectators() is deprecated (it's defined in compat.lua which contains functions that are there mostly for compatibility reasons and might be removed in the future). You should use this instead:
Code:
Game.getSpectators(position[, multifloor = false[, onlyPlayer = false[, minRangeX = 0[, maxRangeX = 0[, minRangeY = 0[, maxRangeY = 0]]]]]])
 
Thanks @Damc I managed to get it working with ur code but I still got a problem...

Code:
#Game.getSpectators(areapos, false, false, 0, 15, 0, 15)

This counts players on screen and I want it to ignore players... any clues?
 
isMonster(cid)

the default values don't alow me to work with that :/

Code:
Game.getSpectators(centerPos, multifloor, onlyPlayers or false, rangex, rangex, rangey, rangey)

i need something like

Code:
Game.getSpectators(centerPos, multifloor, onlyMonsters or false, rangex, rangex, rangey, rangey)

but this must require source edit to make it work like that I guess?
 
Code:
   local spectators = Game.getSpectators(player:getPosition())
   local playerCount = 0
   for _, spectatorCreature in ipairs(spectators) do
     if spectatorCreature:isPlayer() then
       playerCount = playerCount + 1
     end
   end
   print(playerCount)
   print(#spectators)
 
Thanks @Damc I managed to get it working with ur code but I still got a problem...

Code:
#Game.getSpectators(areapos, false, false, 0, 15, 0, 15)

This counts players on screen and I want it to ignore players... any clues?
I think it will be easier with function in which you can use 'show player/monster/npc' as parameter.
Function for it:
PHP:
function getCustomSpectators(position, multifloor, showPlayers, showMonsters, showNPCs, minRangeX, maxRangeX, minRangeY, maxRangeY)
    //getSpectators(position[, multifloor = false[, onlyPlayer = false[, minRangeX = 0[, maxRangeX = 0[, minRangeY = 0[, maxRangeY = 0]]]]]])

    local spectators = Game.getSpectators(position, multifloor, false, minRangeX, maxRangeX, minRangeY, maxRangeY)

    customSpectatorsList = {}
    for _, spectatorCreature in ipairs(spectators) do
        if (showPlayers and spectatorCreature:isPlayer()) or
            (showMonsters and spectatorCreature:isMonster()) or
            (showNPCs and spectatorCreature:isNpc()) then
            table.insert(customSpectatorsList, spectatorCreature)
        end
    end

    return customSpectatorsList
end

Example of use:

Count monsters in range 5 tiles around 'player' on same floor:
PHP:
local numberOfMonstersAroundPlayerInRange_5 = #getCustomSpectators(player:getPosition(), false, false, true, false, 5, 5, 5, 5)
# - before function gives number of table elements, not elements

Send message to all players in range 6 tiles:
PHP:
local tmpSpectators = getCustomSpectators(player:getPosition(), false, true, false, false, 6, 6, 6, 6)
for _, playerInRange in pairs(tmpSpectators) do
    playerInRange:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You are noob.")
end

About min/max range in this function (Game.getSpectators in TFS):
0 = 11 (min/max 11) - so some of your codes with 0, 15 positions could be very wrong, as server translate it to 11!
minRangeX - tiles to left (over 0! not under!)
maxRangeX - tiles to right (over 0! not under!)
minRangeY - tiles to top (over 0! not under!)
maxRangeY - tiles to bottom (over 0! not under!)
Here image with example values and area it will scan for monsters:
kAbF8ZCkc.jpg
 
Last edited:
Back
Top