Trying to see how many groups a player owns with just a userid

I own about 7 groups but this code that I am trying is returning a 0/nil value, everytime I try to do it.

local function ownership(userId)
	local gs = game:GetService("GroupService")
	local groups = gs:GetGroupsAsync(userId)
	local groupsminrank = {}

	for _, v in pairs(groups) do
		local groupInfo = gs:GetGroupInfoAsync(v.Id)
		local roles = groupInfo.Roles

		if roles and roles[255] then
			local minRank = roles[255].Rank
			print("Group:", v.Name, "Player Rank:", v.Rank, "Min Rank:", minRank)

			if v.Rank >= minRank then
				table.insert(groupsminrank, v.Id)
			end
		end
	end

	print(#groupsminrank)
	return #groupsminrank
end

local ownership = ownership(player.UserId)
print(ownership)```

That isn’t necessary, GroupService:GetGroupsAsync already returns the GroupInfo


local function ownership(userId)
	local gs = game:GetService("GroupService")
	local groups = gs:GetGroupsAsync(userId)
	local groupsOwned = 0 -- just keep an incremental variable here

	for _, v in pairs(groups) do
		local rank = v.Rank -- GroupInfo.Rank is the rank the provided user is in, for the group
        if rank == 255 then -- 255 is owner
           groupsOwned += 1 -- increment the variable by 1
        end
	end

	return groupsOwned
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.