How do I get the amount of groups a player is in from a Table?

I’m trying to get my script to work but it seems that i’m not very talented within Tables, so i’m here asking for your help.

I’m trying to fetch information on if the player is in a group that is located within the Table value, but my problem is that i’m not certain on how to achieve this. I’ve already started the basis of the script and I got trapped on the conclusion.

Here’s my script so far (Local Script)

local plr = game.Players.LocalPlayer

local DepartmentIDs = {
	AA = IDHERE, --There is actual IDs here, I just placed this for privacy
	ScD = ID2HERE,
	SD = ID3HERE,
	MD = ID4HERE
}
-- This part is where i'm trapped and can't figure out

https://developer.roblox.com/en-us/api-reference/class/GroupService
https://developer.roblox.com/en-us/api-reference/function/GroupService/GetGroupsAsync

GroupService:GetGroupsAsync() Will return the groups a Player is in as an Array.

1 Like

If you are trying to get the number of groups a player is in from the table as such.

local plr = game.Players.LocalPlayer
local DepartmentIDs = {
	AA = IDHERE, --There is actual IDs here, I just placed this for privacy
	ScD = ID2HERE,
	SD = ID3HERE,
	MD = ID4HERE
}

You will need to do something like this

local plr = game.Players.LocalPlayer
local DepartmentIDs = {
	AA = IDHERE, --There is actual IDs here, I just placed this for privacy
	ScD = ID2HERE,
	SD = ID3HERE,
	MD = ID4HERE
}

local inGroups = 0
for i,v in pairs(DepartmentIDs) do
     if plr:IsInGroup(v) then
          inGroups = inGroups + 1
     end
end
print((%s is in %s groups):format(plr.Name, inGroups)
local groups = game:GetService("GroupService")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local success, result = pcall(function()
		return groups:GetGroupsAsync(player.UserId)
	end)
	
	if success then
		if result then
			local numberOfGroups = #result
			print(numberOfGroups)
		end
	else
		warn(result)
	end
end)
local groups = game:GetService("GroupService")
local players = game:GetService("Players")

local whitelistedGroups = {1, 2, 3} --Put group IDs here.

players.PlayerAdded:Connect(function(player)
	local success, result = pcall(function()
		return groups:GetGroupsAsync(player.UserId)
	end)
	
	if success then
		if result then
			local numberOfGroups = 0
			for _, group in ipairs(result) do
				if table.find(whitelistedGroups, group.Id) then
					numberOfGroups += 1
				end
			end
			print(numberOfGroups)
		end
	else
		warn(result)
	end
end)
local groups = game:GetService("GroupService")
local players = game:GetService("Players")

local whitelistedGroups = {[123] = {}, [456] = {}, [789] = {}}

players.PlayerAdded:Connect(function(player)
	local success, result = pcall(function()
		return groups:GetGroupsAsync(player.UserId)
	end)
	
	if success then
		if result then
			for _, group in ipairs(result) do
				if whitelistedGroups[group.Id] then
					table.insert(whitelistedGroups[group.Id], player)
				end
			end
		end
	else
		warn(result)
	end
end)

https://developer.roblox.com/en-us/api-reference/function/GroupService/GetGroupsAsync

2 Likes

Or simply just:

local Table = {1,2,3,4}

print(#Table)

#Table will return amount of indexes in Table

Not what they want. They want to know the number of groups they are in , in the table.

It would be a table like that, but only that you MUST be in one of the groups within the table.

If you’re doing what I think you’re doing (Area 14, if that’s any clue) and need to specifically scope this table to your groups and not all the players’ groups, then you need two tables; one that points to all the associated groups and another that’s the player’s table.

-- Somewhere, define all your groups in a dictionary
local GROUPS = {
    ["ScD"] = 0000,
    ["SD"] = 0000,
    ["MTF"] = 0000,
}

-- Create a table for the player
local departments = {}

-- Get the groups they're in
local playerRobloxGroups = GroupService:GetGroupsAsync(player.UserId)

-- Iterate through their groups and add them where format GroupId = Rank
for _, groupInfo in ipairs(playerRobloxGroups) do
    departments[tostring(groupInfo.Id)] = groupInfo.Rank
end

-- Iterate through your tracked groups and add them as aliases
for shorthand, groupId in pairs(GROUPS) do
    groupId = tostring(groupId)

    departments[shorthand] = if departments[groupId] then departments[groupId] else 0
end

You can then index the player’s departments table either by a GroupId or a shorthand:

local isScientist = player.Departments["ScD"] > 0
local isSecurity = player.Departments[SD_GID] > 2

If you need to count them, check how many non-zeros there are:

local departmentCount = 0

for shorthand, _ in pairs(GROUPS) do
    if player.Departments[shorthand] > 0 then
        departmentCount += 1
    end
end

This in itself is particularly convoluted though since it’s accounting for a different system (setting up permissions via group caching). The main idea is that you want a dictionary where you have a key-value pair of group-rank number for the player and your own group ids in a table somewhere. You then check, of the GroupIds you track, how many appear in the player’s group table.

2 Likes

Thank you, this really helped out and gave more of an explanation on how it would work.