How to check the list of groups a player is on

  1. What do you want to achieve?
    How can I check the list of groups in which the player is located and on the servers he owns it has taken visits from all games

  2. What solutions have you tried so far?
    Roblox studio, youtube, itd…

3 Likes

To get the groups a user is in you should be able to use Group Service like this:

local gs = game:GetService("GroupService")
local groups = gs:GetGroupsAsync(plr.UserId) -- Get all groups as a table (Must have the player)

im a bit confused on what you mean there could you elaborate?

3 Likes

The idea is to check if the player is the owner of the group and if so, it checks the list of games on the group and takes the visits from them and adds them up

2 Likes

Well you should be able to loop through the groups table then use the roblox api with a proxy to communicate with it since roblox doesn’t let you directly communicate with it.

i threw this script together quickly but it seems to work

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

local function calculateTotalPlaceVisits(gamesData)
	local totalPlaceVisits = 0
	for _, game in ipairs(gamesData) do
		totalPlaceVisits = totalPlaceVisits + game.placeVisits
	end
	return totalPlaceVisits
end

Players.PlayerAdded:Connect(function(player) -- Or any event where player can be used
	local success, groups = pcall(function()
		return GroupService:GetGroupsAsync(player.UserId)
	end)

	if not success then
		warn("Failed to fetch groups for player: " .. player.Name)
		return
	end

	for _, group in ipairs(groups) do
		if group.Rank == 255 then
			print("Player owns group: " .. group.Name .. " (ID: " .. group.Id .. ")")

			local endpoint = `https://games.roproxy.com/v2/groups/{group.Id}/gamesV2?accessFilter=2&limit=100`
			local response = HttpService:JSONDecode(HttpService:GetAsync(endpoint))
			local totalVisits = calculateTotalPlaceVisits(response.data)

			print("Total Place Visits: " .. totalVisits)
		end
	end
end)


for example thats what would be printed if its looking at me

2 Likes

[Error:]
ServerScriptService.GameLauncher:32: attempt to call a nil value - Server

1 Like

That’s probably an issue with how you’re accessing the function, unless it errors if you tried print(response.data)?

1 Like

It’s the function, if it was response.data it’d throw an Index error.

Attempt to index nil with <PROPR>
2 Likes

response data gives me nil back

2 Likes

You have HTTP access in Studio enabled right?

1 Like

Yes it is obvious ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

1 Like

Try using pcalls when you’re trying to fetch the data from the proxy.

local response, data;
pcall(function(): ()
    response = HttpService:GetAsync(endpoint);
end);

--// Use response if it is not `nil`
if (response) then
    --// Decode the JSON from the response
    data = HttpService:JSONDecode(response);
else
    print("Couldn't fetch player data.")
end;
2 Likes

[Error:]

1 Like

Like I said before, use pcalls (see prev reply)
The comment --// ... (line 34) indicates that you’re supposed to move your response logic there.

1 Like

Also make sure that GameCore.calculateTotalGroupPlaceVisits is a valid function.

1 Like

Error:

Module:

It is a private function (not visible/usable for anyone who requires the module). To make it a global function just add it to the module using .

--// Before
function calculateTotalGroupPlaceVisits(): ()
end;

--// After
function module.calculateTotalGroupPlaceVisits(): ()
end;

ERRATA:

  • Fixed typo
1 Like
  • You aren’t decoding the response which was fetched from the proxy.

You are doing response.data, which would be nil as data is a property of the decoded json.

if (response) then
    --// Decode the JSON from the response
    data = HttpService:JSONDecode(response);
else
    print("Couldn't fetch player data.")
end;

^^ this code was already there in my reply, I have no clue as to why you didn’t use it :person_shrugging:

1 Like

I used everything you wrote

Error:

You’re still using response.data, use data.data.

I have corrected and it throws the same error but loads visits normally

Error: