Leaderstat Rank Changes to Group Rank while on team

Hello, I am trying to make it so that when you are on a certain team, your group rank will show under the rank leaderstat for that group whilst on that team. When you get off that team, it resets back to the default group rank. There is 6 teams that will have this and any other team will be the default groups group rank.

Basically, if I’m on the SWAT team, it will show my rank and when I switch to another team, it switches to that groups rank. If you are on the Civilian, Prisoners, Department of Transportation it shows the default group rank.

1 Like
local TeamsService = game:GetService("Teams")
local GroupId = 000 --your group id here
local SWATRank = 0 --insert rank number for swat role in group (SHOULD BE A NUMBER, DON'T MISTAKE WITH ROLE, let me know if you want to use role name instead.)


for _, team in pairs(TeamsService:GetTeams()) do --Gets every team in TeamService
    team.PlayerAdded:connect(function(player) --Event that runs when a player is added to each team, and returns the player when "player" is called.
        if player:GetRankInGroup(GroupID) ~= SWATRank then --if player's rank in group is not equal to SWATRank (a variable) then script will continue to next line.
             player.Leaderstat.Value = player:GetRankInGroup(GroupID) -- Sets the player leaderstat value to the player's rank in the group
        end
    end)
end

This should work, you just have to make it compatible with your in-game hierachy and edit the variables, let me know if you need help. :slightly_smiling_face:

Edit: I added notes to make the script simpler to understand.

Let me know if I misunderstood your question, I feel it was a bit vague.