Staff only door won't carry out code to open it

I’m making a staff only door, it knows I’m the correct rank to open but doesn’t carry out the code for after that. It prints 1 then 2 and then returns to 1 without ding anything after the print("2") bit. I’ve switched the >= and <= parts around and it worked to deny people above the staff rank. Also doesn’t work if I make it so if the person is the wrong rank then do nothing elseif is a staff rank open it with the same thing.

SS:
https://gyazo.com/5ef45d0f8efcf176e11e0e212805b981

local Door = script.Parent
local Group = 9097814
local StaffRank = 9

local function onPartTouched(Hit)
	local humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		local Rank = Player:GetRankInGroup(Group)
		print("1")
		if Rank >= StaffRank then
			print("2")
			Door.CanCollide = false
			wait(1)
			Door.CanCollide = true
		elseif Rank <= StaffRank then
			print("3")
			Door.CanCollide = true
			Door.Transparency = 0 
			Door.SurfaceGui.Frame.Visible = true
			wait(3)
			Door.Transparency = 1 
			Door.SurfaceGui.Frame.Visible = false
		else 
			warn("Something went wrong. Rank is:".. Rank)
		end
	end
end

Door.Touched:Connect(onPartTouched)

Debounce is your solution.

Use debounce like @Vaschex recommended, but maybe try printing the player’s Rank after the print("1") to see what the Rank value is to see if that’s your issue.

This is my script now and it just goes to access denied, my debounce is meant to be the open variable.

local Door = script.Parent
local Group = 9097814
local StaffRank = 9
local Open = false

local function onPartTouched(Hit)
	local humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		local Rank = Player:GetRankInGroup(Group)
		print("1")
		if Rank >= StaffRank and not Open then
			Open = true
			print("2")
			Door.CanCollide = false
			wait(1)
			Door.CanCollide = true
			Open = false
		else
			Open = true
			Door.CanCollide = true
			Door.Transparency = 0 
			Door.SurfaceGui.Frame.Visible = true
			wait(3)
			Door.Transparency = 1 
			Door.SurfaceGui.Frame.Visible = false
			Open = false
		end
	end
end

Door.Touched:Connect(onPartTouched)

It’s likely because of the way your if statement is set up, regardless if you’re above the rank or not, it’ll go to access denied due to how your thing is set up.

If you’re going for a debounce that’s not where the if statement should go, checking Open should be the first thing the code does when the part has been touched

I would try something like this

local Door = script.Parent
local Group = 9097814
local StaffRank = 9
local Open = false

local function onPartTouched(Hit)
	local humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if Open or not humanoid then
		return
	end
	Open = true
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	local Rank = Player:GetRankInGroup(Group)
	print("1")
	if Rank >= StaffRank then
		print("2")
		Door.CanCollide = false
		wait(1)
		Door.CanCollide = true
	else
		Door.CanCollide = true
		Door.Transparency = 0 
		Door.SurfaceGui.Frame.Visible = true
		wait(3)
		Door.Transparency = 1 
		Door.SurfaceGui.Frame.Visible = false
	end
	Open = false
end

Door.Touched:Connect(onPartTouched)

It loops it like before, I’m going to try putting debounce inside the staff rank bit and do some stuff rq.

What do you mean exactly by “it loops”?

Prints 1 2 1 2 1 2 1 2… when you touch it without opening.

Does it do it in 1 second intervals or no? If it does then the code is doing as it should, if it isn’t then something else is up

I got it to wait then found out the builder had another part in where the door is so testing again.

Got it working now just needed to put the Open = true inside the check for if they’re staff like my script before.

I’m not sure if putting the Open = true part in there would help as it would mean people who aren’t staff would spam the Touched event and ignore your wait(3) completely, I think for your approach you can try a table based debounce, though not sure if that would cause its own problems with a staff causing non staff to enter your door, so I think that would fair enough

I’ll get my testers on it to see what happens.

As @EmbatTheHybrid said, if someone is above the rank they could open the door and let others in.
You should probably add a CanCollide Part in the doorway that only allows players above rank to pass through it as well.