Tables Registering Last Object

local groupperm = {
[3654488] = 150,
[3654488] = 160,
[3654488] = 255,
}

function verperm(plr)
local hit = 0
for i,v in pairs(groupperm) do
if plr:GetRankInGroup(i) == v then
hit = hit + 1
end
end
if hit > 0 then
return true
end
end

When the function is ran and all that fun nonsense, it for some reason only registers the last key in the table, specifically “[3654488] = 255”.

Either I’m an absolute numbnuts, or they changed something in damned tables that’s screwing me over. Never had this problem until recently.

Because you’re setting all the values to the same key. Reread your table.

2 Likes

Right. My bad then. Now that I think about all this key setting nonsense, I believe I may have done this before, it was just something I didn’t quite recall, as it was quite awhile ago.

Thanks.

1 Like

Glad to hear it’s resolved. Remember to mark my answer as a solution if it solved your issue.

Glad that this was solved, but for future reference could you please use code formatting? e.g.

local X = math.random(0, 999)

image

1 Like

In general though, I gotta pick at that code; it makes me feel a little weird. There’s other ways to go about that are cleaner; continuing to iterate through a table when it’s no longer necessary to do so isn’t fun and so is the method of verifying whether the user has sufficient permissions or not. Your callback function is also incomplete and will return nil if the player doesn’t have sufficient permissions. I feel it’s cleaner to return something solid.

local GroupId = 3654488
local RanksAllowed = {255, 160, 150}

local function GetCanAccess(Player)
    local PlayerRank = Player:GetRankInGroup(GroupId)
    for _, Rank in pairs(RanksAllowed) do
        if Rank == PlayerRank then
            return true -- Auto break
        end
    end
    return false -- If nothing was true, give false
end

print(GetCanAccess(game:GetService("Players").AloysiusLazvari)) --> probably true, idk your groups

local CanAccess = GetCanAccess(game:GetService("Players").AloysiusLazvari)
if CanAccess then (do)
    -- Code
end (end)

Be aware: GetRankInGroup caches. You must take the longer route by using GroupService if you want instantaneous results.

local GroupService = game:GetService("GroupService")
local GroupId = 3654488

-- blah blah blah whatever but player exists ok
-- this is example code just break it apart and make it work

local Groups = GroupService:GetGroupsAsync(Player.UserId)
local GroupData = nil
for index, Group in pairs(Groups) do
    if Group.GroupId == GroupId then
        GroupData = Group
        break
    end
end

print(GroupData.Rank) --> your rank

-- im on mobile I didn't check the API so if I made a mistake that's why