Get a users primary group

Heres what ive tried:

local groups = GroupService:GetGroupsAsync(Player.UserId)
	for _, groupInfo in pairs(groups) do
		for key, value in pairs(groupInfo) do

			if key == "Id" then
				GroupId = value
			elseif key == "IsPrimary" and value == true then
				local group = game:GetService("GroupService"):GetGroupInfoAsync(GroupId)
				print(group.Name)
			end
		end
	end

The issue is it does get the primary group but it checks my primary group goes Oh its the commonwealth and then returns with tebrix interactive the group directly under my primary.
C3apture

Screenshot of the console output
image

I have looked into Groups Api but im not too sure on how id implement that.

You only need one loop.

local function get_primary_group(user_id)
	local groups = GroupService:GetGroupsAsync(user_id)
	
	for i, group in pairs(groups) do
		if group.IsPrimary then
			return group
		end
	end
end

local primary = get_primary_group(player.UserId)

print(primary.Name)
3 Likes