How can I configure my local script to multiple groups/ranks?

I want the ability to bind a script to multiple groups in select ranks on one door so I wouldn’t have to be complicated and have this done in two completely separate doors.

The issue I’m having so far right now is that when I bind it to a single group, works completely fine, but if I attempt at adding another group + rank using the else if statement, it doesn’t seem to work.

I looked around the forums and YouTube for a bit and seems like no one has came up with this issue before. I’m not a scripter and requested someone else to do it for me, however their script didn’t work.

This is the script:

local rank = 30 -- The rank the player will need to get through (out of 255)
local groupID = 8610690 -- The group ID the player must be in
local Player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
	if not hit:IsDescendantOf(Player.Character) then
		return
	end
	if (hit and hit.Parent and game.Players:FindFirstChild(hit.Parent.Name)) then
		if (game.Players[hit.Parent.Name]:IsInGroup(groupID) and game.Players[hit.Parent.Name]:GetRankInGroup(groupID) >= rank) then
			script.Parent.CanCollide = false
			script.Parent.Transparency = 1
			wait(0.5)
			script.Parent.CanCollide = true
			script.Parent.Transparency = 1
		elseif (game.Players[hit.Parent.Name]:IsInGroup(11549657) and game.Players[hit.Parent.Name]:GetRankInGroup(11549657) >= 3) then
			script.Parent.CanCollide = false
			script.Parent.Transparency = 1
			wait(0.5)
			script.Parent.CanCollide = true
			script.Parent.Transparency = 1
		else
			script.Parent.CanCollide = true
		end
	end
end)

I didn’t receive any errors from the script so I’m really not sure what could be the issue here. How would I fix this?

You could probably create a function that iterates through a dictionary for certain groups and ranks.

Example: (comments to explain how the script woks)

function is_valid(player)
   -- 'player' is the player instance
   local groups = { 
   -- this is an array that holds the table values for the groups so that the script can
   -- check this for verification
        {
             -- this is an example, this table holds the group's id and the minimum rank the player needs to be
             -- in order to pass the check
             group_id = 1, -- the group's id
             min_rank = 1 -- the minimum rank required
        },
   }

   for _, group in ipairs(groups) do
      -- this is an ipairs loop that iterates through the 'groups' array
      -- '_' is just a placeholder variable since the array's index is never going to be used
      -- 'group' is the current table the loop is on, inside the 'groups' array
      if player:IsInGroup(group.group_id) then
          -- this checks if the player is a member of the group with the provided group id
          return (player:GetRankInGroup(group.group_id) >= group.min_rank)
          -- this returns either a true or false value depending on the player's rank
      else
          -- if the player isn't a member of the group, then it skips this iteration
          continue
      end
   end

   -- this returns false by default (when the player isn't in the group at all)
   return false
end

-- how this can be used:
if is_valid(Player) then -- 'Player' would be the player
   -- player is a member of the group with a certain rank
else
   -- player is either not in the group or does have a high enough rank
end

If you want something to check if the player is less than or equal to a rank or exactly a rank, you’d have to modify the sample code I gave you.

1 Like