Player:GetRankInGroup isn't working

I’m trying to have a function to retrieve a player’s rank number in a group with a function and then use that data to create a PlayerData folder with a bunch of essential data for a custom leaderboard Gui.
This is the function:

The issue is that the script says people who are in the group and have been in the group long before they joined, are not in the group. Also, if the people who are affected rejoin the game, the issue still happens to them.

I’ve had multiple friends of mine who are very good scripters look over the entire module and they couldn’t figure out the issue.

EDIT
This is where the function is called:

What values are you inputting for the GetRankInGroup parameters?

Are you checking for any rank from the any primary group, because generally I have seen the GetRankInGroup used like this

Player:GetRankInGroup(GroupId) == GroupLevel

GroupId and GroupLevel being your said ids/variables

Have you tried the function without the protected call or tried to retrieve the error and printing it? It should give some insight as to why the function isn’t working.

Try adding error handler

function getRankInGroup(player, group)
    local groupRank
    local success, err = pcall(function()
        groupRank = player:GetRankInGroup(group)
    end)

    if not success then
        print(err)
    end

    return groupRank
end

and see if there are any errors

I pressed send reply by accident, don’t care about (post withdrawn by author, will be automatically deleted in 1 hour unless flagged)

It would be useful if you could also show us where you call the function. It is entirely possible that you could be calling it using the PlayerId, or name, instead of the Player instance.

Example on how the function should be called
local player = game.Players.Someone
local group = 12345678
local rank = GetRankInGroup(player, group)

And as said above, try adding an error handler, as it will allow you to figure out the cause.

It just prints the player’s rank in the group, I was using it to determine if the error was coming from there.

The group IDs for TCAR and all of TCAR’s divisions.

I’m certain it would have errored consistently if I had done that instead of only erroring for some people.