Would it be possible to make a list of people who are in two groups without doing it manually?

Example:

Group A:
Person 1
Person 2
Person 3
Person 4
Person 5

Group B:
Person 2
Person 4
Person 6
Person 7
Person 8

What I want it to show in the list:

Person 2
Person 4

Basically a filter.

Sorry if I am not clear enough.

1 Like

Something like this would work, looping through both arrays and comparing the users.

for keyA, valueA in pairs(groupA) do
    for keyB, valueB in pairs(groupB) do
         if valueA.UserId == valueB.UserId then table.insert(groupC, valueA)
    end
end

Another thing you could do is if you made a dictionary instead with the UserId’s as keys then you could simply do something like this:

for keyA, valueA in pairs(groupA) do
     if groupB[valueA.UserId] ~= nil then table.insert(groupC, valueA)
end
1 Like

Adding onto this you can use this Player | Documentation - Roblox Creator Hub To see if a users is in a group.

1 Like

I am talking about roblox groups.

What would the use-case here be? Maybe there’s a better way to do things instead of checking if the user is in the 2 groups?

It wouldnt be for one user it would be to check for all users in one group and see which users are in both group a and group b.

Yes, but what is the use-case?
Why do you need to check if an user is in two groups?
If we know this maybe we can get a better fit solution for you.

hm, try this:

local groupid1 = groupidhere
local groupid2 = groupidhere
local group1 = {}
local group2 = {}

for i,v in pairs(game.Players:GetPlayers()) do
if i:IsInGroup(groupid1) then
table.insert(group1, v)
elseif i:IsInGroup(groupid2) then
table.insert(group2, v)
end
end
  1. First things first elseif doesn’t make sense here, change that to
if v:IsInGroup(groupid1) then
    table.insert(group1, v)
end
if v:IsInGroup(groupid2) then
    table.insert(group2, v)
end
  1. v:IsInGroup(), not i:IsInGroup() because i is the index in the iteration, v is the player, so you can’t use IsInGroup on something that is not a player.

  2. this is inefficient and I suggest the OP to tell us what he is trying to achieve.

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

Players.PlayerAdded:Connect(function(player)
	local groupInfo = GroupService:GetGroupsAsync(player.UserId)
	if table.find(groupInfo, groupId1) and table.find(groupInfo, groupId2) then
		--do stuff
	end
end)
1 Like

Here’s the script to do something like this:

-- Named it PlayersService to avoid any confusion
local PlayersService = game:GetService("Players")
local GroupA = ... -- put group a id here
local GroupB = ... -- put group b id here

local function GetPlayersInBothGroups()
	local PlayersInBothGroups = {}
	local Players = PlayersService:GetPlayers()

	for value = 1, #Players do
		local Player = Players[value]

		local IsInGroupA = Player:IsInGroup(GroupA)
		local IsInGroupB = Player:IsInGroup(GroupB)

		if IsInGroupA and IsInGroupB then
			PlayersInBothGroups[#PlayersInBothGroups + 1] = Player
		end
	end

	return PlayersInBothGroups
end

local PlayersInBothGroups = GetPlayersInBothGroups()

This would achieve what you are looking for.

I suggest caching the information whether a player is in a group or not, because if he want’s to perform that filtering multiple times this will be very inefficient.

Are you talking about the Player:IsInGroup(GroupA) call?
Roblox caches the results of that call.

I didn’t know that it caches, thank you for the information.

I feel like there’s a lot of, well, extraneous work floating around in these code samples. Provided you cache the results of a GetGroupsAsync call from GroupService (as that does not cache), you can fetch all the groups a user is in. You can then write a simple function that gleans over these groups and returns false if a user isn’t in one of the needed groups, or true if the loop ran without being stopped.

local GroupService = game:GetService("GroupService")

-- Caching the results of the call
local playerGroups = {}

local success, result = pcall(GroupService.GetGroupsAsync, GroupService, player.UserId)
if success and result then
    for _, group in ipairs(result) do
        playerGroups[group.Id] = group.Rank
    end
end

-- Working with the cached results
local function playerIsInAllGroups(requiredGroups, groupCache)
    for _, groupId in ipairs(requiredGroups) do
        if not groupCache[groupId] then
            return false
        end
    end
    return true
end

-- Applying this function
local needsMembershipIn = {1, 2, 3}
local inAllGroups = playerIsInAllGroups(needsMembershipIn, playerGroups)
1 Like