How do I get username information based off a player that holds a specific group rank id?

How do I get username information based off a player that holds a specific group rank id?

What I’m trying to do is create offices, and have the player’s username that holds a specific rolesetid to display on that office’s textlabel (To save the time of changing it when they’re removed from their position.) So if they get promoted/demoted, it will automatically remove them from that textlabel and replace it with vacant or a new person that holds the given rolesetid.

I’ve looked it up, and the current third party api i’ve been using is roproxy.com but it hasn’t been working. Does anyone know what I should do?

Here’s the script:

local Players = game:GetService("Players")

local groupId = 5879110
local targetRankId = 255
local textLabel = script.Parent

local function updateTextLabel()
    local url = string.format("https://groups.roproxy.com/v1/groups/%d/roles/%d/users", groupId, targetRankId)

    local success, response = pcall(HttpService.GetAsync, HttpService, url)
    if success then
        print("Status code:", response.StatusCode)
        print("Response body:", response.Body)

        local data = HttpService:JSONDecode(response.Body)
        if data and data.data then
            local usernames = {}
            for _, member in ipairs(data.data) do
                local username = Players:GetNameFromUserIdAsync(member.id)
                table.insert(usernames, username)
            end
            textLabel.Text = table.concat(usernames, ", ")
        else
            warn("Failed to decode group data.")
        end
    else
        warn("HTTP request failed:", response)
    end
end

updateTextLabel()```
2 Likes

Hi there,

You’re on the right track with this approach, but the reason why you’ve been running into issues is that you’re supplying the Groups API with your group’s RankID, while the API accepts a roleSetId. roleSetIds are NOT the same as the editable RankID that you can set in your group. They’re essentially completely unique identifiers assigned to each specific group rank. Think of them more like a UserId but for group ranks instead.

These roleSetIds can be rather tedious to find, and considering you’re already supplying a RankID in your API call, I’ve written down a neat little function for you that grabs a group rank’s specific roleSetId from a supplied groupId and rankId:

local function getRoleSetIdfromRankId(groupId, rankId)
	local succ, returned = pcall(function()
		return HttpService:GetAsync("https://groups.roproxy.com/v1/groups/"..groupId.."/roles/")
	end)
	
	if succ then
		local response = HttpService:JSONDecode(returned)
		
		for _, role in pairs(response["roles"]) do
			if role["rank"] == rankId then
				return role["id"]
			end
		end
	end
end

Incorporating this into your code and making a few minor adjustments to your response’s data structure, below should be your final and functional code:

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

local groupId = 5879110
local rankId = 255
local textLabel = script.Parent

local function getRoleSetIdfromRankId(groupId, rankId)
	local succ, returned = pcall(function()
		return HttpService:GetAsync("https://groups.roproxy.com/v1/groups/"..groupId.."/roles/")
	end)
	
	if succ then
		local response = HttpService:JSONDecode(returned)
		
		for _, role in pairs(response["roles"]) do
			if role["rank"] == rankId then
				return role["id"]
			end
		end
	end
end

local function updateTextLabel()
	local roleSetId = getRoleSetIdfromRankId(groupId, rankId)
	
	if roleSetId then
		local url = string.format("https://groups.roproxy.com/v1/groups/%d/roles/%d/users", groupId, roleSetId)

		local success, response = pcall(HttpService.GetAsync, HttpService, url)
		if success then
			--print("Status code:", response.StatusCode)
			-- GetAsync only returns the response body, not the status. If you wish to include this as well, look into RequestAsync()
			print("Response body:", response)

			local data = HttpService:JSONDecode(response)
			
			if data and data.data then
				local usernames = {}
				for _, member in ipairs(data.data) do
					local username = Players:GetNameFromUserIdAsync(member["userId"])
					table.insert(usernames, username)
				end
				
				textLabel.Text = table.concat(usernames, ", ")
			else
				warn("Failed to decode group data.")
			end
		else
			warn("HTTP request failed:", response)
		end
	end
end
updateTextLabel()

Let me know how it goes and feel free to report back if you run into any issues. Good luck!

2 Likes

It worked - thank you very much! :slight_smile:

1 Like

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