How to return true out of a table

Hello! Hope you’re having a blessed day!

I am attempting to read through multiple tables at the same time. Here is my config.

mdtConfig.departments = {
	[1] = {
		Name = "Basic Tech Police Department",
		ID = 1, 
		Ranks = {
			{name = "Chief", RID = 5, isBoss = true},
			{name = "Assistant Chief", RID = 4, isBoss = true},
			{name = "Lieutenant", RID = 3, isBoss = true},
			{name = "Officer", RID = 2, isBoss = false},
			{name = "Cadet", RID = 1, isBoss = false},
		},
		groupID = 32893104,
		teamNames =  "Police",
		playerID = {10000, 103242, 8324080234},
		Type = "police",
	},
	[2] = {
		Name = "ATPD",
		ID = 2, 
		Ranks = {
			{name = "Chief", RID = 5, isBoss = true},
			{name = "Assistant Chief", RID = 4, isBoss = true},
			{name = "Lieutenant", RID = 3, isBoss = true},
			{name = "Officer", RID = 2, isBoss = false},
			{name = "Cadet", RID = 1, isBoss = false},
		},
		groupID = 32893104,
		teamName =  "Police",
		playerID = {10000, 103242, 8324080234},
		Type = "police",
	},
	[3] = {
		Name = "FBI",
		ID = 3, 
		Ranks = {
			{name = "Chief", RID = 5, isBoss = true},
			{name = "Assistant Chief", RID = 4, isBoss = true},
			{name = "Lieutenant", RID = 3, isBoss = true},
			{name = "Officer", RID = 2, isBoss = false},
			{name = "Cadet", RID = 1, isBoss = false},
		},
		groupID = 32893104,
		teamName =  "Police",
		playerID = {10000, 103242, 8324080234},
		Type = "police",
	},
	[4] = {
		Name = "CIA",
		ID = 4, 
		Ranks = {
			{name = "Chief", RID = 5, isBoss = true},
			{name = "Assistant Chief", RID = 4, isBoss = true},
			{name = "Lieutenant", RID = 3, isBoss = true},
			{name = "Officer", RID = 2, isBoss = false},
			{name = "Cadet", RID = 1, isBoss = false},
		},
		groupID = 32893104,
		teamName =  "Police",
		playerID = {10000, 103242, 8324080234},
		Type = "police",
	},
	[5] = {
		Name = "Army",
		ID = 5, 
		Ranks = {
			{name = "Chief", RID = 5, isBoss = true},
			{name = "Assistant Chief", RID = 4, isBoss = true},
			{name = "Lieutenant", RID = 3, isBoss = true},
			{name = "Officer", RID = 2, isBoss = false},
			{name = "Cadet", RID = 1, isBoss = false},
		},
		groupID = 32893104,
		teamName =  "Police",
		playerID = {10000, 103242, 8324080234},
		Type = "police",
	},
		
}

How would I read through each of those tables and return a true value for the number (EX: 1, 2 ,3) if the player is in the group, team or it is their playerID?

My goal is to have a UI pop up in a frame for them to join the team if they have access.

1 Like

Firstly you dont need set index’s manually, luau compiler already automatically will do that.

So, you can make functions like that in module;

function module:isPlayerInDepartment(player: Player, department: string): boolean?
    assert(player and typeof(player) == 'Instance' and player:IsA('Player'), 'Invalid Player')
    assert(department and type(department) == 'string' and self.departments[department], 'Invalid Department')

    local department = self.departments[department]
    if player:IsInGroup(department.groupID) then
        return true
    else
        if department.playerID[player.UserId] then
            return true
        elseif player.Team.Name == department.teamName then
            return true
        end
    end

    return false
end
1 Like

A slightly cleaner way to write it:

local inGroup = player:IsInGroup(department.groupID)
local isUserId = table.find(department.playerID, player.UserId)
local isTeam = (player.Team.Name == department.teamName)

return isGroup or isUserId or isTeam