• 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 0.X Funcion getSpectators that only selects players

Mr.Caffeine

Active Member
Joined
Nov 4, 2018
Messages
79
Reaction score
43
Hello, there is a way to make this script select only the players from this area of getSpectators?
I need the getSpectators count ignores monsters and npcs.
TFS version: 0.4

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
local g = getCreaturePosition(cid)
local amountOfPlayersmin = 3
local amountOfPlayersmax = 4
local teleport = {x = 1942, y = 3056, z = 8}
local centerPos = {x = g.x, y = g.y, z = g.z, stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE}
local rangeX = 3
local rangeY = 3
local p = getSpectators(centerPos, rangeX, rangeY)
 
local players = #p
if players >= amountOfPlayersmin and players <= amountOfPlayersmax then
    for _, pid in pairs(p) do
        doTeleportThing(pid, teleport)
        doSendMagicEffect(getCreaturePosition(pid), CONST_ME_TELEPORT)
    end
else
    doPlayerSendTextMessage(cid, 25, "Is necessary to gather a group of 3 to 4 players in this room.")
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
    return true
end
end
 
Solution
Function to get only players. It also converts nil - which is returned when there is no creatures in area on 0.4 - into empty table:
Lua:
function getPlayerSpectators(centerPos, rangeX, rangeY, multifloor)
   multifloor = multifloor and true or false -- multifloor is optional, default 'false'
   local list = {}
   local spectators = getSpectators(centerPos, rangeX, rangeY, multifloor)
   if spectators then -- spectators may be 'nil' on TFS 0.4
      for _, cid in pairs(spectators) do
         if isPlayer(cid) then
            table.insert(list, cid)
         end
      end
   end

   return list
end
Hello, there is a way to make this script select only the players from this area of getSpectators?
I need the getSpectators count ignores monsters and npcs.
TFS version: 0.4

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
local g = getCreaturePosition(cid)
local amountOfPlayersmin = 3
local amountOfPlayersmax = 4
local teleport = {x = 1942, y = 3056, z = 8}
local centerPos = {x = g.x, y = g.y, z = g.z, stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE}
local rangeX = 3
local rangeY = 3
local p = getSpectators(centerPos, rangeX, rangeY)
 
local players = #p
if players >= amountOfPlayersmin and players <= amountOfPlayersmax then
    for _, pid in pairs(p) do
        doTeleportThing(pid, teleport)
        doSendMagicEffect(getCreaturePosition(pid), CONST_ME_TELEPORT)
    end
else
    doPlayerSendTextMessage(cid, 25, "Is necessary to gather a group of 3 to 4 players in this room.")
    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
    return true
end
end
Edit this line and replace with:
local p = getSpectators(centerPos, rangeX, rangeY, false, true)

(this function is set up as following in tfs 1+, not sure if it works in 0.4: function getSpectators(centerPos, rangex, rangey, multifloor, onlyPlayers)

If it worked, please mark this as solution.
 
Edit this line and replace with:
local p = getSpectators(centerPos, rangeX, rangeY, false, true)

(this function is set up as following in tfs 1+, not sure if it works in 0.4: function getSpectators(centerPos, rangex, rangey, multifloor, onlyPlayers)

If it worked, please mark this as solution.
Hello, Mjmackan unfortunately in tfs 0.4 don't have onlyplayers in function getSpectators
 
Function to get only players. It also converts nil - which is returned when there is no creatures in area on 0.4 - into empty table:
Lua:
function getPlayerSpectators(centerPos, rangeX, rangeY, multifloor)
   multifloor = multifloor and true or false -- multifloor is optional, default 'false'
   local list = {}
   local spectators = getSpectators(centerPos, rangeX, rangeY, multifloor)
   if spectators then -- spectators may be 'nil' on TFS 0.4
      for _, cid in pairs(spectators) do
         if isPlayer(cid) then
            table.insert(list, cid)
         end
      end
   end

   return list
end
 
Solution
Function to get only players. It also converts nil - which is returned when there is no creatures in area on 0.4 - into empty table:
Lua:
function getPlayerSpectators(centerPos, rangeX, rangeY, multifloor)
   multifloor = multifloor and true or false -- multifloor is optional, default 'false'
   local list = {}
   local spectators = getSpectators(centerPos, rangeX, rangeY, multifloor)
   if spectators then -- spectators may be 'nil' on TFS 0.4
      for _, cid in pairs(spectators) do
         if isPlayer(cid) then
            table.insert(list, cid)
         end
      end
   end

   return list
end

It worked perfectly, thank you gesior you are the best
 
Function to get only players. It also converts nil - which is returned when there is no creatures in area on 0.4 - into empty table:
Lua:
function getPlayerSpectators(centerPos, rangeX, rangeY, multifloor)
   multifloor = multifloor and true or false -- multifloor is optional, default 'false'
   local list = {}
   local spectators = getSpectators(centerPos, rangeX, rangeY, multifloor)
   if spectators then -- spectators may be 'nil' on TFS 0.4
      for _, cid in pairs(spectators) do
         if isPlayer(cid) then
            table.insert(list, cid)
         end
      end
   end

   return list
end

Hey, can u help me please?

I'm trying to code a spell that jump into each monster around the player, 1 by 1 doing 1 hit in each of them. In the end, the player are teleported to initial position when casted the spell.

Code:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_BLOCKARMOR, true)

function onGetFormulaValues(cid, level, skill, attack, factor)
    local skillTotal, levelTotal = skill + attack, level / 5
    return -(skillTotal / 3 + levelTotal), -(skillTotal + levelTotal)
end

setCombatCallback(combat, CALLBACK_PARAM_SKILLVALUE, "onGetFormulaValues")



function attack(cid, pos, toPos, fromPos, var)
    if #validTargets > 0 then
            if doCombat(cid, combat, var) == LUA_NO_ERROR then
                doTeleportThing(cid,validTargets[1])
                doSendMagicEffect(getPlayerPosition(cid), 61)
                table.remove(validTargets, validTargets[1])
                addEvent(attack, 200, cid)
            end
    else
        doTeleportThing(cid, playerfirstpos[1])
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Nenhum alvo valido encontrado.")
        for k in pairs(playerfirstpos) do
            playerfirstpos[k] = nil
        end
    end
return true
end

playerfirstpos = {}
validTargets = {}
function onCastSpell(cid, var)
local pos = getCreaturePosition(cid) 
local creatures = getSpectators(pos, 3, 3, false)
    if table.insert(playerfirstpos, pos) then
        if creatures then
            for _, target in ipairs(creatures) do
                if isCreature(target) and (isPlayer(target) or isMonster(target)) then
                    table.insert(validTargets, target)
                end
            end
            addEvent(attack, 1, cid)
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Nenhum alvo valido encontrado.")
        end
    end
return true
end

But not working ...
 
Back
Top