Why don't "IsInGroup" and "GetRankInGroup" return group ID? How to get group ID from the IsInGroup function?

  1. I want to get the group ID the user is part of.

  2. IsInGroup and GetRankInGroup don’t return group ID’s. This is a big problem because I have a list of groups that are listed in a dictionary and the fastest way to check if a user is in one of them is to run a for do function that tries every group id, but it doesn’t return the group id afterwards, which I actually also need.

module script

local groups = {[123456] = "Stacking Numbers", [22222] = "Only Twos"}
function module:GetGroupsID(id) -- the only working approach so far, but it still doesn't return the matching id
for id,_ in pairs(groups) do
return id
end    

server script

if plr:IsInGroup(module:GetGroupsID()) then
local groupid = plr:IsInGroup(Database:GetGroupsID()) -- returns true but doesn't return id, mildly infuriating
  1. I’ve tried using several different approaches with functions, but none of them worked. Chances are if you propose an idea it’s probably been done (unless there’s some innovative secret way that I don’t know of).

Is there actually a way to make this work without hacky workarounds, or will IsInGroup return ID’s any time soon?

1 Like

Why would they return an ID that’s passed through as an argument already? Just save the ID before calling IsInGroup with it.

local id=module:GetGroupsID()
if plr:IsInGroup(id) then
--do stuff with the id
3 Likes

I think GroupService:GetGroupsAsync is what you want.

All you do is pass the player’s id as the argument and it returns a table of information of the groups they are currently in.

Or if you only want to check if they are in specific group and you have the id of that group, you can check if player:GetRankInGroup returns 0

1 Like
local groups = {[123456] = "Stacking Numbers", [22222] = "Only Twos"}
function module:GetGroupsID(id) -- the only working approach so far, but it still doesn't return the matching id
for id,_ in pairs(groups) do
return id
end  

Your code is being cut off short - the loop only runs once, then terminates because you are returning under any condition. This is part of the problem. Also refer to the reply by @ChipioIndustries.

2 Likes

Ight, so the function player:IsInGroup() doesn’t return an ID, as the argument (that’s what goes in these parenthesis) is the group ID, which it will return true if the player is in the given group. As @FifthInfinity stated, you’ll need to use GroupService:GetGroupsASync() to find all of the players groups, and then just perform a check there.

Otherwise, you can use a loop that cycles through all of the values in the groups table, and if the player is in one of the groups in the table it breaks the loop and returns the group id that the player is in

e.g:

local groups = {["Stacking Numbers"] = 123456, ["Only Twos"] = 22222 }
function module:GetGroupsID(player) --this is the loop function]
    local groupID = false
    for i, v in pairs(groups) do --the loopy bit
        if player:IsInGroup(v[1]) then --as a precaution, we state the position of the value
            groupID = v[1]
            break
        end
    end
    return groupID
end

And then for the server:

local groupID = GroupModule:GetGroupsID(player) 
if groupID = CORRECT_GROUP_ID then
    --put your code here
end

Hope that’s easy enough to understand, but what I’ve done is added a loop in the module, which checks if the player is in a group. If the player is not in a group, it returns false. Otherwise, it returns the groupID of the group that the player is in (i.e. 22222 or 123456). Then you can make a decision based on that info.

1 Like

Why are you doing that? There is no table to index. This makes no sense.

Here’s what you might’ve meant:

if player:IsInGroup(v) then -- there's literally no table, why are you getting the index of one
    groupID = v
end

I don’t know, that was at like 3 am in the morning. Please don’t hurt me.
I stand corrected

Thank you, that’s exactly what I needed!

1 Like