The Object.ForEach... functions call your callback for each object that meets the condition. General rules:

  • The callback receives 1 parameter, the object index (aIndex).
  • The list is captured before iterating, so adding / removing objects during iteration does not break the loop.
  • If the callback returns false (strictly a boolean), the loop stops; any other return value (or no return value) continues.
  • If the callback raises an error, the loop stops and the error is reported to the script that called the iteration function (GameServer is not affected).
  • If the callback parameter is not a function, or the filter object (map, party, guild) does not exist, the function does nothing.
  • These functions have no return value.

Object.ForEachPlayer

Iterates over every character that is in the game (PLAYING state).

Object.ForEachPlayer(callback)
ParameterTypeDescription
callbackfunction(aIndex)Function called for each player
Object.ForEachPlayer(function(aIndex)
    Message.Send(aIndex, 1, "Hello everyone!")
end)

Object.ForEach

Iterates over every existing character, monster and NPC (empty slots are not included).

Object.ForEach(callback)
ParameterTypeDescription
callbackfunction(aIndex)Function called for each object
local count = 0
Object.ForEach(function(aIndex) count = count + 1 end)

Object.ForEachPlayerOnMap

Iterates over the players in the game on a map.

Object.ForEachPlayerOnMap(map, callback)
ParameterTypeDescription
mapintegerMap number (must exist on this GameServer)
callbackfunction(aIndex)Function called for each player
Object.ForEachPlayerOnMap(MAP_LORENCIA, function(aIndex)
    Message.Send(aIndex, 0, "The event has started!")
end)

Object.ForEachMonster

Iterates over every living, active monster on the whole server.

Object.ForEachMonster(callback)
ParameterTypeDescription
callbackfunction(aIndex)Function called for each monster
Object.ForEachMonster(function(aIndex) Log.Add(Object.GetName(aIndex)) end)

Object.ForEachMonsterOnMap

Iterates over the living monsters on a map.

Object.ForEachMonsterOnMap(map, callback)
ParameterTypeDescription
mapintegerMap number (0 or higher; for a negative number the function does nothing)
callbackfunction(aIndex)Function called for each monster
Object.ForEachMonsterOnMap(MAP_DEVIAS, function(aIndex) Object.Kill(aIndex) end)

Object.ForEachMonsterByClass

Iterates over the living monsters of a given monster code, on every map.

Object.ForEachMonsterByClass(class, callback)
ParameterTypeDescription
classintegerMonster code (0 or higher; for a negative number the function does nothing)
callbackfunction(aIndex)Function called for each monster
Object.ForEachMonsterByClass(44, function(aIndex) Log.Add("Found monster 44: " .. aIndex) end)

Object.ForEachPartyMember

Iterates over the party members who are online on this GameServer.

Object.ForEachPartyMember(partyNumber, callback)
ParameterTypeDescription
partyNumberintegerParty number, obtained from Player.GetPartyNumber
callbackfunction(aIndex)Function called for each member
local party = Player.GetPartyNumber(aIndex)
if party >= 0 then
    Object.ForEachPartyMember(party, function(member) Message.Send(member, 1, "The whole party receives a gift!") end)
end

Object.ForEachGuildMember

Iterates over the guild members who are online on this GameServer.

Object.ForEachGuildMember(guild, callback)
ParameterTypeDescription
guildinteger or stringGuild number (greater than 0, obtained from Player.GetGuildNumber) or guild name
callbackfunction(aIndex)Function called for each member
Object.ForEachGuildMember(Player.GetGuildName(aIndex), function(member)
    Message.Send(member, 2, "Guild announcement")
end)

Object.ForEachNearby

Iterates over the active characters and monsters, on the same map and in the same instance, within a radius around an object. The center object itself is not included.

Object.ForEachNearby(aIndex, range, callback)
ParameterTypeDescription
aIndexintegerIndex of the center object
rangeintegerRadius in cells (clamped to 0-255)
callbackfunction(aIndex)Function called for each object within the radius (only living objects are included)
Object.ForEachNearby(aIndex, 5, function(other)
    if Object.GetType(other) == OBJECT_MONSTER then Object.Kill(other) end
end)

Object.CountPlayersOnMap

Counts the players in the game on a map.

Object.CountPlayersOnMap(map) -> integer
ParameterTypeDescription
mapintegerMap number

Returns: the number of players; 0 if the map does not exist on this GameServer.

if Object.CountPlayersOnMap(MAP_ARENA) >= 10 then ... end

Object.CountMonstersOnMap

Counts the living monsters on a map.

Object.CountMonstersOnMap(map [, class]) -> integer
ParameterTypeDescription
mapintegerMap number
classinteger (optional)Monster code; omitted = all monster types

Returns: the number of monsters counted.

local boss = Object.CountMonstersOnMap(MAP_KALIMA1, 100)