• 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 Game.getSpectators question. TFS [1.2]

Moj mistrz

Monster Creator
Joined
Feb 1, 2008
Messages
932
Solutions
10
Reaction score
295
Location
Poland
Hello there, I've got one question regarding Game.getSpectators. Namely, is it possible to print lowest level spectator? If yes, then how? I'd like to know that :).

Thanks in advance.
 
something like this should do the trick, you just need to change the "(...)" params
Code:
Game.getLowestLevelSpectator(...)
local specPlayers = {}
   for k, creature in pairs(Game.getSpectators(...)) do
     if creature:isPlayer() then
       specPlayers[k] = creature
     end
   end

   if specPlayers ~= {} then
     for k, player in pairs(specPlayers) do
     local lowest = nil
       if lowest == nil then
         lowest = {k, player:getLevel()}
       end
       if lowest[2] <= player then
         lowest = {k, player:getLevel()}
       end
     end
     return specPlayers[k]
   end
   return nil
end
 
Code:
function getLowestLevelSpectator(...)
   local specPlayers = {}
   for _, creature in pairs(Game.getSpectators(...)) do
     if creature:isPlayer() then
       table.insert(specPlayers, creature)
     end
   end

   if (#specPlayers > 2) then
     table.sort(specPlayers, function(a, b) return (a:getLevel() < b:getLevel()) end)
   end
   return specPlayers[1]
end
 
not tested

Code:
function getLowestLevelSpectator(...)
  local spectators = Game.getSpectators(...)
  for i = #spectators, 2, -1 do
    if not spectators[i]:isPlayer() or spectators[i]:getLevel() < spectators[i - 1]:getLevel() then
      spectators[i] = nil
    end
  end
  return spectators[#spectators > 1 and 2 or 1]
end
 
Back
Top