Specific Team Ranks

Im trying to achieve a leaderboard that displays a specific rank when you are on that team so like when youre on the Sheriffs Office team it will display your rank from this specific group id, when youre a citizen it will display it from this team. I found out how to add a leaderstat and a specific group rank but I do not know how to make it so if you change teams you get the specific group rank

local GroupID = 6339762		 
local CharHide = 0			 
							 

game:GetService('Players').PlayerAdded:Connect(function(p)
	local ls = Instance.new('IntValue', p)
	ls.Name = 'leaderstats'
	local rank = Instance.new('StringValue', ls)
	rank.Name = 'Rank'
	rank.Value = 'Guest'
	if p:IsInGroup(GroupID) then
		local xrole = p:GetRoleInGroup(GroupID)
		if string.len(xrole) <= CharHide then
			warn('[GroupLeaderboard] Attempted to hide more characters than role has; using default name')
			rank.Value = xrole
		else
			local role = string.sub(xrole, CharHide+1)
			rank.Value = role
		end
	end
end)
5 Likes

Maybe something like this

local GroupID = 6339762
local CharHide = 0

game:GetService('Players').PlayerAdded:Connect(function(player)
    local function updateRank()
        local rank = 'Guest' -- Default rank
        
        if player:IsInGroup(GroupID) then
            local xrole = player:GetRoleInGroup(GroupID)
            if string.len(xrole) <= CharHide then
                warn('[GroupLeaderboard] Attempted to hide more characters than role has; using default name')
                rank = xrole
            else
                local role = string.sub(xrole, CharHide+1)
                rank = role
            end
        end
        
        player.leaderstats.Rank.Value = rank
    end

    -- Setup the leaderstats for the player
    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local rankValue = Instance.new('StringValue')
    rankValue.Name = 'Rank'
    rankValue.Value = 'Guest'
    rankValue.Parent = leaderstats

    -- Call updateRank when player joins and when team changes
    player:GetPropertyChangedSignal("Team"):Connect(function()(updateRank)
    updateRank() 
end)

I added a function called updateRank that calculates and updates the player’s rank based on their current team. This function is then called both when the player joins the game and when their team changes.

3 Likes

p:GetRoleInGroup caches the result. You’d want to use the GroupService to prevent this issue.
GroupService | Documentation - Roblox Creator Hub

I am confused on how you are trying to accomplish though. By changing their in-game team, their role in-game changes as well for that group? Do the different teams correlate with different groups?

After thinking about it, I think I might have a better idea on what you’re trying to accomplish. You could get the group’s information via GroupService:GetGroupInfoAsync, get the different roles and correlate them to the teams. You could also use GroupService:GetGroupsAsync to get the player’s groups that they’re a member of.
GroupService (GetGroupInfoAsync) | Documentation - Roblox Creator Hub
GroupService (GetGroupsAsync) | Documentation - Roblox Creator Hub

1 Like

Theres a problem on this line

-- Call updateRank when player joins and when team changes
    player:GetPropertyChangedSignal("Team"):Connect(function()(updateRank)
    updateRank() 
end)
``
That the (updateRank is just a scribble, how would I fix that?

https://gyazo.com/0f71c275616415282eabda75d4f345fd (Gyazo shows the problem, but its not attaching for some reason)
1 Like

My apologies,

Delete the underlined empty parenthesis.

image

That fixed the top parathesis error but it still has an error in the code, it could just be on my end of error but I am not that strong in code fixing.

https://gyazo.com/0bb6c90ac8433a71544a11c15073541b

If you hover over the line it should tell you what type of error your encountering… It looks like your missing a end)