PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

GetEnemiesAsync

Returns a StandardPages object including information on all of the specified group’s enemies.

This pages does not include a list of group IDs but instead a list of group information tables, mirroring the format of those returned by GroupService/GetGroupInfoAsync. See below for the structure of these tables.

group = {
    Name = "Knights of the Seventh Sanctum",
    Id = 377251,
    Owner = {
        Name = "Vilicus",
        Id = 23415609
    },
    EmblemUrl = "http://www.roblox.com/asset/?id=60428602",
    Description = "We fight alongside the balance to make sure no one becomes to powerful",
    Roles = {
        [1] = {
            Name = "Apprentice",
            Rank = 1
        },
        [2] = {
            Name = "Warrior",
            Rank = 2
        },
        [3] = {
            Name = "Earth Walker",
            Rank = 255
        }
    }
}

Note, as this function returns a StandardPages object rather than an array, developers may wish to convert it to an array for ease of use (see examples).

This function has a number of useful applications, including detecting if a player is a member of an enemy group.

For allies, use GroupService/GetAlliesAsync.

Parameters

Name Type Default Description

The group’s ID.

Returns

Return Type Summary

A StandardPages object of the group’s enemies.


Code Samples


GroupService:GetEnemiesAsync

-- creates a list of all of the enemies of a given group
local enemies = {}
local groupId = 57

local pages = game:GetService("GroupService"):GetEnemiesAsync(groupId)
while true do
   for _, group in pairs(pages:GetCurrentPage()) do
      table.insert(enemies, group)
   end
   if pages.IsFinished then
      break
   end
   pages:AdvanceToNextPageAsync()
end

function onPlayerAdded(player)
   for _, enemyGroup in pairs(enemies) do
      if player:IsInGroup(enemyGroup.Id) then
          print("Player is an enemy!")
          break
       end
    end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
-- handle players who joined while the enemies list was still loading
for _,player in pairs(game.Players:GetPlayers()) do
   onPlayerAdded(player)
end

Group Ally/Enemy Checker

This code sample demonstrates how GroupService and Player/IsInGroup can be used to determine whether a player is a member of a group, or any of its allies or enemies.

Note as GroupService/GetAlliesAsync and GroupService/GetEnemiesAsync use StandardPages objects a utility function is used to convert them to allies.

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")

-- define group id here
local GROUP_ID = 271454

-- utility function for dealing with pages
local function pagesToArray(pages)
    local array = {}
    while true do
        for _, v in ipairs(pages:GetCurrentPage()) do
            table.insert(array, v)
        end
        if pages.IsFinished then
            break
        end
        pages:AdvanceToNextPageAsync()
    end
    return array
end

-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)

-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)

local function playerAdded(player)
    -- check to see if the player is in the group
    if player:IsInGroup(GROUP_ID) then
        print(player.Name .. " is a member!")
    else
        local isAlly, isEnemy = false, false

        -- check to see if the player is in any ally groups
        for _, groupInfo in ipairs(allies) do
            local groupId = groupInfo.Id
            if player:IsInGroup(groupId) then
                isAlly = true
                break
            end
        end

        -- check to see if the player is in any enemy groups
        for _, groupInfo in ipairs(enemies) do
            local groupId = groupInfo.Id
            if player:IsInGroup(groupId) then
                isEnemy = true
                break
            end
        end

        if isAlly and not isEnemy then
            print(player.Name .. " is an ally!")
        elseif isEnemy and not isAlly then
            print(player.Name .. " is an enemy!")
        elseif isEnemy and isAlly then
            print(player.Name .. " is both an ally and an enemy!")
        else
            print(player.Name .. " is neither an ally or an enemy!")
        end
    end
end

-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)

-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end