How would I allow only certain group ranks to use my door?

My door that I have made in Roblox Studio allows anyone to go through it, how would I allow only specific group ranks to enter it? Just to be clear, I would like multiple group ranks to enter it. I will put the script below as the door has an animation to it and I intend to keep it.

local TweenService = game:GetService("TweenService")

local DoorPart = script.Parent

local OpenDoor = TweenService:Create(DoorPart, TweenInfo.new(2), {Position = Vector3.new(33.181, 4.034, -156.476)})
local CloseDoor = TweenService:Create(DoorPart, TweenInfo.new(2), {Position = Vector3.new(41.019, 4.034, -156.476)})
local CD = false

DoorPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and CD == false then
CD = true
OpenDoor:Play()
wait(3)
CloseDoor:Play()
wait(3)
CD = false
end
end)

player:GetRankInGroup(<groupid>)

Example;

if player:GetRankInGroup(<groupid>) == 4 then
-- do something
end

There are two ways that you can do this, using roles or ranks. I prefer using ranks, as it is universal to name changes in the roles, but that is my preference.

First, you would need to get the player of the character that touched the door. This can be done as such:

local player = Players:GetPlayerFromCharacter(hit.Parent)

Second, you need to verify they are the rank or the role that you want. For example, if you only want the owner of the group and one rank below him to be allowed through, you would use the 255 and 254 Ranks. This can be done as such:

local rank = player:GetRankInGroup(yourGroupID)
local allowedRanks = {255, 254}

if table.find(allowedRanks, rank) then

Putting this all together, your new script would work as such:

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local DoorPart = script.Parent

local allowedRanks = {255, 254}

local OpenDoor = TweenService:Create(DoorPart, TweenInfo.new(2), {Position = Vector3.new(33.181, 4.034, -156.476)})
local CloseDoor = TweenService:Create(DoorPart, TweenInfo.new(2), {Position = Vector3.new(41.019, 4.034, -156.476)})
local CD = false

DoorPart.Touched:Connect(function(hit)
     local player = Players:GetPlayerFromCharacter(hit.Parent)
     if hit.Parent:FindFirstChild(“Humanoid”) and CD == false then
          local rank = player:GetRankInGroup(yourGroupID)
          if table.find(allowedRanks, rank) then
               CD = true
               OpenDoor:Play()
               wait(3)
               CloseDoor:Play()
               wait(3)
               CD = false
          end
     end
end)
1 Like

Can you send me a screenshot of how this is all put together?

What do you mean ‘how this is all put together’? I edited the script that you posted at the top of your post.

Well, you have 2 of the same statements. The statement is local allowedRanks.

I said in my post that the first two code blocks were explaining the parts individually, and the final code block was putting them both together. You don’t need to add the code from the first two code blocks to the final code block.

Never mind, sorry for the confusion.

Do you mind showing me how to play a sound when the door opens and then closes?

It would be quite simple, you would insert the sound into the model and do script.SoundName:Play(), assuming you set all of the properties correctly.