Door opens, but when button is pressed again it doesn't close

Topic has been withdrawn.

There is another post on this, but the poster was so vague and didn’t elaborate on his issue.

Connect the system to a debounce like this, basically you’re trying to call two different functions under the same event which does not work.

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

    print("Button Pressed") 
    Door1.Transparency = 1 
    Door1.CanCollide = false 
    wait(.01) 
    Door2.CanCollide = false
    Door2.Transparency = 1
    Door3.CanCollide = false
    Door3.Transparency = 1
    wait(.01) 
    Door4.CanCollide = false
    Door4.Transparency = 1
    Door5.CanCollide = false
    Door5.Transparency = 1
     wait(.01) 
    Door6.CanCollide = false
    Door6.Transparency = 1
    Door7.CanCollide = false
    Door7.Transparency = 1
    wait(.01) 
    Door8.CanCollide = false
    Door8.Transparency = 1
    Door9.CanCollide = false
    Door9.Transparency = 1
    wait(.01) 
    Door10.CanCollide = false
    Door10.Transparency = 1

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

    Door1.Transparency = 0 
    Door1.CanCollide = true 
    wait(.01) 
    Door2.CanCollide = true
    Door2.Transparency = 0
    Door3.CanCollide = true
    Door3.Transparency = 0
    wait(.01) 
    Door4.CanCollide = true
    Door4.Transparency = 0
    Door5.CanCollide = true
    Door5.Transparency = 0
    wait(.01) 
    Door6.CanCollide = true
    Door6.Transparency = 0
    Door7.CanCollide = true
    Door7.Transparency = 0
    wait(.01) 
    Door8.CanCollide = true
    Door8.Transparency = 0
    Door9.CanCollide = true
    Door9.Transparency = 0
    wait(.01) 
    Door10.CanCollide = true
    Door10.Transparency = 0
    wait (3)
    script.Parent.BrickColor = BrickColor.new("Lime green")

end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
4 Likes

You have create the function, onClicked, with two different mechanics.

The script will be unable to identify which function should be fire, as they aren’t differentiable to the script. You should use a different name for the function, i.e OnClicked and onClicked.

So what you’re saying is create two functions that both are OnClicked just have them different names?

Not if you want the same button to do two different things. Use debounce like in the code I posted above, that should solve your issue.

1 Like

I’m going to try this and see if it works. I knew about debounce, I was just confused on how to implement it into my script.

1 Like

This worked, thank you so much. I’ve gone ahead and marked it as the solution

1 Like

Actually, I wanted help with something else to do with this. If I wanted to make it so only certain ranks in the group can use the button and open the door how would I do that. (sorry about bugging you so much)

Well, another person has already answered the post, however, if you would like to let certain ranks access the door, integrate :GetRankInGroup with the script. Here is a wiki article to help you do so: Player | Documentation - Roblox Creator Hub

2 Likes

I added this in

local group = 5221697
local rank = {
	255;
	18;
	17;
	240;
	230;
}

	if player:GetRankInGroup(group) == rank then

Then the rest of the script but it still doesn’t open

1 Like

That’s not how it works. The new function just overwrote the old one, there’s not two different functions associated with the same name.

2 Likes

This part was already solved, but thank you. /

Actually, I wanted help with something else to do with this. If I wanted to make it so only certain ranks in the group can use the button and open the door how would I do that. (sorry about bugging you so much)

Is what I need help with atm.

2 Likes

I understand that you got to the right solution, but his reasoning for why it was not working was wrong. It’s important to understand why something doesn’t work rather than just making stuff up like he was doing.

3 Likes

Agreed, thank you, for explaining it to me. I really appreciate the help. :smiley:

1 Like

Instead of creating a table for the ranks, you can just do:

if Player:GetRankInGroup(5221697) >= 17 then 
— Code
end

Make sure to change ‘17’ to the desired minimum rank.

2 Likes

Would I put this after the debounce or before the debounce, or does it matter.

1 Like

The reason this doesn’t work is because you’re comparing a number to a table. If you want to check whether rank is in the table, you have two ways of doing this. You can either restructure your table to be a dictionary, like this:

local rank = {
	[255] = true;
	[18] = true;
	[17] = true; -- and so on
}
if rank[player:GetRankInGroup(group)] then -- see if it's a key in the table

The benefit of this is that it’s easy to check whether the rank is part of the table. Alternatively, if you don’t want to change how it’s formatted, you can loop through the table and compare each value to the player’s rank, like so:

local rank = {
	255;
	18;
	17; -- and so on
}
local playerRank = player:GetRankInGroup(group)
for _, validRank in pairs(rank) do
	if validRank == playerRank then
		local canOpen = true
		break
	end
end

if canOpen then -- and so on

This method isn’t that great, but it means you don’t have to restructure your table.

2 Likes

Player seems undefined in this one.