PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetAlliesAsync

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

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 allied group.

For enemies, use GroupService/GetEnemiesAsync.

Parameters

Name Type Default Description

The group’s ID.

Returns

Return Type Summary

A StandardPages object of the group’s allies.


Code Samples


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 k, v in pairs(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 pairs(allies) do
    			local groupId = groupInfo.Id
    			if groupInfo then
    				if player:IsInGroup(groupId) then 
    					isAlly = true
    					break
    				end
    			end
    		end
    
    		-- check to see if the player is in any enemy groups
    		for _, groupInfo in pairs(enemies) do
    			local groupId = groupInfo.Id
    			if groupInfo then
    				if player:IsInGroup(groupId) then 
    					isEnemy = true
    					break
    				end
    			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 pairs(Players:GetPlayers()) do
    	playerAdded(player)
    end

GroupService:GetAlliesAsync

local Players = Game:GetService("Players")

--creates a table of all of the allies of a given group
local allies = {}
local groupID = 57

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

function OnPlayerAdded(player)
   for _,group in pairs(allies) do
      if player:IsInGroup(group.Id) then
         print("Player is an ally!")
      end
   end
end

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