How would I add a debounce to this gate script to prevent someone to spam click it?

I have an issue with my gate script that when I add a wait(3.5) before I make the debounce false, it doesn’t fix the issue where you cannot make the gate go back down until its fully up. I’ve used this method for other scripts, but it doesn’t work for this one?

It seems that even with the wait(3.5) before I make the debounce false, it does not fix the issue with spam clicking the gate. It goes up, then down, then up, then down, and breaks the whole thing.

I’ve tried many solutions to solve with this problem and none of them worked. Such as tick(), waits in different areas, switching up the debounce, nothing worked best.

I’m not looking for an entire script, instead anything to help me fix this issue would help. Thanks!

Here is the script:

local door1 = workspace.MainHolderPart
local clicker = door1.Clicker.ClickDetector
local currentCFrame = workspace.MainHolderPart.CFrame
local currentOrientation = currentCFrame - currentCFrame.p
 
local tweeningInformation = TweenInfo.new(
    3.5,   
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local door1Open = {CFrame = CFrame.new(-161.163, 15.155, -125.809) * currentOrientation}
local door1Close = {CFrame = CFrame.new(-161.163, 5.679, -125.809) * currentOrientation}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)
local open = false
game.ServerScriptService.RegisterToGateService.OnInvoke = function(a)
clicker.MouseClick:Connect(function(player)
	if player.Team == game.Teams["Asylum Command"] then
	if not open then 
	open = true
    tween1open:Play()
wait(3.5)
open = false
	else
    tween1close:Play()
wait(3.5)
open = false
	end
	elseif player.Team == game.Teams["Riot Control Unit"] then
		if not open then 
	open = true
    tween1open:Play()
wait(3.5)
open = false
	else
    tween1close:Play()
wait(3.5)
open = false
	end
	elseif player.Team == game.Teams["Hospital Administration"] then
		if not open then 
	open = true
    tween1open:Play()
wait(3.5)
open = false
	else
    tween1close:Play()
wait(3.5)
open = false
	end	
	end
end)
end 

I’m new to the developer form, So any suggestions to what I should add in my posts would also be helpful!

Indent your code and put in the correct format. Surround your code with ```.

You should probably put at the end of your tweens .Completed:Wait() when you play them. That could be the issue. You should always have that event the line after you play the tween. For example:

tween1open:Play()
tween1open.Completed:Wait()

Thank you! :smile: I’ll try that too.

Didn’t work the same issue is happening. But, I think you have the right idea, Maybe I should remove the waits?

No need to remove the waits. However, does anything print in the output?

No, I have noticed I changed it so it checks if its false and turns the debounce true after It tweens it, it fixes the gate when going up, But when you can make it go down before its fully up.
This is the script now:

TweenService = game:GetService("TweenService")
local door1 = workspace.MainHolderPart
local clicker = door1.Clicker.ClickDetector
local currentCFrame = workspace.MainHolderPart.CFrame
local currentOrientation = currentCFrame - currentCFrame.p
 
local tweeningInformation = TweenInfo.new(
    3.5,   
    Enum.EasingStyle.Quad,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local door1Open = {CFrame = CFrame.new(-161.163, 15.155, -125.809) * currentOrientation}
local door1Close = {CFrame = CFrame.new(-161.163, 5.679, -125.809) * currentOrientation}
local tween1open = TweenService:Create(door1,tweeningInformation,door1Open)
local tween1close = TweenService:Create(door1,tweeningInformation,door1Close)
local open = false
local completed = false
game.ServerScriptService.RegisterToGateService.OnInvoke = function(a)
clicker.MouseClick:Connect(function(player)
	if player.Team == game.Teams["Asylum Command"] then
    tween1open:Play()
tween1open.Completed:Wait()
	if not open then 
	open = true
	wait(3.5)
 else
    tween1close:Play()
tween1close.Completed:Wait()
if not open == true then
	wait(3.5)
	open = false
end
	end
	elseif player.Team == game.Teams["Riot Control Unit"] then
		if not open then 
	open = true
    tween1open:Play()
tween1open.Completed:Wait()
	else
    tween1close:Play()
tween1close.Completed:Wait()
open = false
	end
	elseif player.Team == game.Teams["Hospital Administration"] then
		if not open then 
	open = true
    tween1open:Play()
tween1open.Completed:Wait()
open = false
	else
    tween1close:Play()
tween1close.Completed:Wait()
open = false
	end	
	end
end)
end

Note: I only added the change where it checks the team for Asylum Command, Not anything else.
Another edit: Its going back down after the tween is finished by itself.
Edit three: Thanks for the help, It did open my eyes to more ideas. I will keep experimenting.