We use cookies on this site to enhance your user experience

Group Ally/Enemy Checker

Group Ally/Enemy Checker

Sep 12 2018, 2:40 AM PST

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