Script only checks the last group ID

Hello there,

I have a playerlist UI which shows if your in a group or not, and if you are then it’ll say the group’s name on the playerlist. Basically what’s happening is that it’s only checking to see if your in the group at the end of the list instead of checking through the rest. Does anyone know what I did wrong?

Code
local groups = {
    { id = 3411267, name = "SVEALAND" },
    { id = 3240949, name = "MERCIA" },
    { id = 1246558, name = "NORTHUMBRIA" },
    { id = 3247990, name = "DANELAW" },
    { id = 3493953, name = "EAST ANGLIA" },
    { id = 3418930, name = "DANES" },
    { id = 3416823, name = "NORSE" },
    { id = 3412261, name = "GOTALAND" },
    { id = 3181532, name = "NORRLAND" },
    { id = 4121922, name = "DUBLIN" },
    { id = 1124324, name = "ICELAND" },
    { id = 3458210, name = "KIEVAN RUS" },
    { id = 4121912, name = "THE ISLES" },
    { id = 4121933, name = "THE POWYS" },
    { id = 3778509, name = "DYFED" },
    { id = 3493930, name = "CORNWALLUM" },
    { id = 3269061, name = "ALT CLUT" },
    { id = 3265487, name = "GWYNEDD" },
    { id = 3316962, name = "PICTLAND" },
    { id = 3172503, name = "MUNSTER" },
    { id = 3490619, name = "GLYWYSING" },
    { id = 3406180, name = "ULSTER" },
    { id = 3507435, name = "MEATH" },
    { id = 3406391, name = "CONNACHT" },
    { id = 3413132, name = "LEINSTER" },
    { id = 3414659, name = "BRITTANY" },
    { id = 3779588, name = "MIDDLE FRANKIA" },
    { id = 3419708, name = "WEST FRANKIA" },
    { id = 3421863, name = "EAST FRANKIA" },
    { id = 3421863, name = "MERCENARIES" },
    { id = 3422678, name = "THE CHURCH" },
    { id = 3128836, name = "PAGAN CHURCH" },
    { id = 1245097, name = "WESSEX" },
   
}
 
local ld = script.Parent:WaitForChild("Leaderboard")
local list = ld:WaitForChild("List")
--local rank = ld:WaitForChild("Player"):WaitForChild("PlrRank")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local enterfading = false
local leavefading = false
local gui = game:GetService("StarterGui")
gui:SetCoreGuiEnabled("PlayerList",false)
 
local function update()
    -- Get rid of the existing tags
    for e,d in pairs(list:GetChildren()) do
        d:remove()
    end
 
    for i,v in pairs(game.Players:GetChildren()) do
        if v ~= nil then
            for e,d in pairs(list:GetChildren()) do
                d.Position = d.Position + UDim2.new(0,0,0,d.Size.Y.Offset)
            end
 
            local tag = ld:WaitForChild("Player"):Clone()
            local plrName = tag:WaitForChild("PlrName")
            tag.Parent = list
 
            -- Make the name in uppercase letters for charity
            plrName.Text = string.upper(v.Name)
 
            -- Set Player's Rank
            tag.PlrRank.Text = string.upper(v:GetRoleInGroup(2965628)) -- Change to your group ID
 
            -- Set Player's Group
            for _, group in pairs(groups) do
                if v:IsInGroup(group.id) then
                    tag.GrpRank.Text = group.name
                else
                    tag.GrpRank.Text = "NO FACTION"
                end
            end
            tag.GrpRank.Visible = true
 
            -- Set Player's Thumbnail
            local content, bool = game.Players:GetUserThumbnailAsync(v.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
            tag.ImageLabel.Image = content
            tag.Visible = true
 
            tag.MouseEnter:connect(function()
                repeat wait() until leavefading == false
                enterfading = true
                repeat
                    tag.Brightness.BackgroundTransparency = tag.Brightness.BackgroundTransparency - 0.02
                    wait()
                until tag.Brightness.BackgroundTransparency <= 0.8
                tag:FindFirstChild("Brightness").BackgroundTransparency = 0.8
                wait(0.2)
                enterfading = false
            end)
 
            tag.MouseLeave:connect(function()
                repeat wait() until enterfading == false
                leavefading = true
                repeat
                    tag.Brightness.BackgroundTransparency = tag.Brightness.BackgroundTransparency + 0.02
                    wait()
                until tag.Brightness.BackgroundTransparency >= 1
                tag.Brightness.BackgroundTransparency = 1
                wait(0.2)
                leavefading = false
            end)
        end
    end
 
    list.CanvasSize = UDim2.new(0,0,0,#list:GetChildren()*45) -- Make sure we update the size of the list
end
 
game.Players.PlayerAdded:connect(update)
 
mouse.KeyDown:connect(function(key)
    if string.lower(key) == "l" then
        if ld.Visible then
            game.Lighting.Blur.Size = 0
            script.Parent:FindFirstChild("Open"):Play()
            ld.Visible = not ld.Visible
        elseif not ld.Visible then
            ld.Visible = true
            game.Lighting.Blur.Size = 15
            script.Parent:FindFirstChild("Close"):Play()
        end
    end
end)
 
update()
2 Likes

Right so I’m looking here
and this may be your problem:

         -- Set Player's Group
        for _, group in pairs(groups) do
            if v:IsInGroup(group.id) then
                tag.GrpRank.Text = group.name
            else
                tag.GrpRank.Text = "NO FACTION"
            end
        end

It’ll loop through till it gets to the last one and not stop if it finds a group they’re in. what I’d suggest is something like what you already got, but like this:

         -- Set Player's Group
        local isinagroup = false
        for _, group in pairs(groups) do
            if v:IsInGroup(group.id) then
                tag.GrpRank.Text = group.name
                isinagroup = true
            elseif isinagroup == false then
                tag.GrpRank.Text = "NO FACTION"
            end
        end

Something else to note is that it WILL stop once it finds the last group they are in, so I honestly don’t know how you will handle a user that’s in more than one of these groups as this will return the last group that the player is in.

Say if I’m in group, “DUBLIN” and the group, “DYFED” then it will say I’m in group “DYFED” as that is the last group it checked that I’m in. if I’m not in any of these then it will return “No Faction” so that’s just something to think about.

Anyways try the change I suggested and see if that fixes your problem.

4 Likes

Alternatively, try using break instead once you’ve found a group the player is in. This will escape the loop immediately instead of proceeding to check the rest of the table.

         -- Set Player's Group
        for _, group in pairs(groups) do
            if v:IsInGroup(group.id) then
                tag.GrpRank.Text = group.name
                break
            else
                tag.GrpRank.Text = "NO FACTION"
            end
        end
5 Likes

Thank you very much man, this worked. I’ve been struggling with this problem for about 3 days and now I can move onto something else. Thank you @qqtt991 as well, I will keep that noted.

1 Like