Button opens door for certain roles in a group

I have a button that opens up a door when pressed, however I want to make it so certain ranks can open it. It worked before when everyone could open/close it, but it doesn’t work anymore once I implemented the certain ranks.

Script that doesn't work
if Player:GetRankInGroup(5221697) >= 17 then 


local debounce = false
function onClicked() 
if debounce == false then
    debounce = true


    print("Button Pressed") 
    Door1.Transparency = 1 
    Door1.CanCollide = false 
    wait(.03) 
    Door2.CanCollide = false
    Door2.Transparency = 1
    Door3.CanCollide = false
    Door3.Transparency = 1
    wait(.03) 
    Door4.CanCollide = false
    Door4.Transparency = 1
    Door5.CanCollide = false
    Door5.Transparency = 1
     wait(.03) 
    Door6.CanCollide = false
    Door6.Transparency = 1
    Door7.CanCollide = false
    Door7.Transparency = 1
game.Workspace.Locked.SurfaceGui.Enabled = false 
wait(.03) 
    Door8.CanCollide = false
    Door8.Transparency = 1
    Door9.CanCollide = false
    Door9.Transparency = 1
    wait(.03) 
    Door10.CanCollide = false
    Door10.Transparency = 1

    script.Parent.BrickColor = BrickColor.new("Really red")
else
    debounce = false

    Door1.Transparency = 0 
    Door1.CanCollide = true 
    wait(.03) 
    Door2.CanCollide = true
    Door2.Transparency = 0
    Door3.CanCollide = true
    Door3.Transparency = 0
    wait(.03) 
    Door4.CanCollide = true
    Door4.Transparency = 0
    Door5.CanCollide = true
    Door5.Transparency = 0
wait(.03) 
    Door6.CanCollide = true
    Door6.Transparency = 0
    Door7.CanCollide = true
    Door7.Transparency = 0
game.Workspace.Locked.SurfaceGui.Enabled = true 

    wait(.03) 
    Door8.CanCollide = true
    Door8.Transparency = 0
    Door9.CanCollide = true
    Door9.Transparency = 0
    wait(.03) 
    Door10.CanCollide = true
    Door10.Transparency = 0
    script.Parent.BrickColor = BrickColor.new("Lime green")
end

end


script.Parent.ClickDetector.MouseClick:connect(onClicked)
1 Like

The rank check seems to be the first place to look for a problem. Just before the first line, try adding this manual confirmation:

print("Player:GetRankInGroup(5221697): " .. Player:GetRankInGroup(5221697))

Does this print a number value? Is the number greater than 17?

You can use CollectionService to accomplish this too.
https://developer.roblox.com/en-us/api-reference/class/CollectionService

This article on Collision Filtering Team Doors may be interesting to you. Instead of team filtering, you could set the collision groups to be by rank, either when the button is pressed or all the time.
https://developer.roblox.com/en-us/articles/Collision-Filtering-Team-Doors

You may find For loops helpful when dealing with receptive chunks of code such as this.

https://developer.roblox.com/en-us/articles/For-Loops

1 Like

It doesn’t print anything, once I add that

Could you please specify what exactly doesn’t work? Does the doors not open at all or do they open regardless of what rank the player is? Also do you get any errors?

1 Like

The door doesn’t open no matter the rank, but if I remove the rank part it will open to everyone, I also don’t receive and errors.

If you did add the print statement before the first line of your script and it doesn’t print any value, that means the value of Player:GetRankInGroup(5221697) is nil before the beginning of this script.

Your problem does not lie in this script, other than the if Player:GetRankInGroup(5221697) check, which is not really the problem. The check itself is fine, but …

Is 5221697 the correct group number? What can you do to confirm that?

Is this Player in this Group? Are you certain they have rank value in this Group? Where and when was that set?

You may need to write more basic test checks on this Player and this Group either at the beginning of the script or before the script runs, or disable this script and write another script with some tests and checks in it.

1 Like

I can confirm it’s the correct group, because of my leaderboard that displays the player’s role in the group.

Where in the code are you actually calling the onClicked() function, you most likely need to do something like this to fix your issue:

local ClickDetector = --put the clickDetector here
local Debounce = false
ClickDetector.MouseClick:Connect(function(Player)
    if Player:GetRankInGroup(5221697) >= 17 and not Debounce then
        Debounce = true
        Door1.Transparency = 1 
        Door1.CanCollide = false 
        wait(.03) 
        Door2.CanCollide = false
        Door2.Transparency = 1
        Door3.CanCollide = false
        Door3.Transparency = 1
        wait(.03) 
        Door4.CanCollide = false
        Door4.Transparency = 1
        Door5.CanCollide = false
        Door5.Transparency = 1
         wait(.03) 
        Door6.CanCollide = false
        Door6.Transparency = 1
        Door7.CanCollide = false
        Door7.Transparency = 1
        game.Workspace.Locked.SurfaceGui.Enabled = false 
        wait(.03) 
        Door8.CanCollide = false
        Door8.Transparency = 1
        Door9.CanCollide = false
        Door9.Transparency = 1
        wait(.03) 
        Door10.CanCollide = false
        Door10.Transparency = 1
        script.Parent.BrickColor = BrickColor.new("Really red")
    elseif Player:GetRankInGroup(5221697) >= 17 and Debounce then
        Debounce = false
        Door1.Transparency = 0 
        Door1.CanCollide = true 
        wait(.03) 
        Door2.CanCollide = true
        Door2.Transparency = 0
        Door3.CanCollide = true
        Door3.Transparency = 0
        wait(.03) 
        Door4.CanCollide = true
        Door4.Transparency = 0
        Door5.CanCollide = true
        Door5.Transparency = 0
        wait(.03) 
        Door6.CanCollide = true
        Door6.Transparency = 0
        Door7.CanCollide = true
        Door7.Transparency = 0
        game.Workspace.Locked.SurfaceGui.Enabled = true 
        wait(.03) 
        Door8.CanCollide = true
        Door8.Transparency = 0
        Door9.CanCollide = true
        Door9.Transparency = 0
        wait(.03) 
        Door10.CanCollide = true
        Door10.Transparency = 0
        script.Parent.BrickColor = BrickColor.new("Lime green")
   end
end)

Also, to make your code more efficient you could just tag all the doors with a ‘door’ tag and use the collection service for modifying the each door’s transparency and canCollide properties, here is the info on it CollectionService | Documentation - Roblox Creator Hub

2 Likes

Thank you, I just had to configure apart of your script and then it seemed to worked. Thank you very much!

1 Like

sure no problem :slight_smile: glad to help

1 Like

That’s not how you use a Debounce. In this case you’re relegating it to a standard boolean flag to describe the door’s state. You also have some bad code going on in the case of your if statement and running the group check per condition.

You can combine both a Debounce and a state.

local ClickDetector = nil

local OpenState = false
local Debounce = false

ClickDetector.MouseClick:Connect(function (player)
    if player:GetRankInGroup(5221697) >= 17 then
        if not Debounce then
            Debounce = true
            OpenState = not OpenState
            if OpenState then
                -- Open the door
            else
                -- Close the door
            end
            Debounce = false
        end
    end
end)

This is cleaner. Any code is prevented from continuing if the user does not have the required credentials. If they do, then it checks to see if the function is no debounced. If it isn’t, it continues on and flips the state of the door before acting on it.

2 Likes

I think he wants you to click to open and then click to close thats why he is using the debounce variable. I know its used to prevent excessive firing of something.

1 Like

and why do have the clickDetector set to nil

1 Like