Debounce not working?

I’ve made a door script but for some reason debouncing won’t work, any help?

--// Services
local ReplStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

--// Events
local hasInteracted = ReplStorage.Events:WaitForChild("hasInteracted")

--// Door
local hinge = script.Parent
local door = hinge.Parent
local clickDetector = door.ClickDetector

--// IsClosed
local locked = false
local isOpen = false
local debounce = false

--// Tweening
local Info = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)

local open = {
	CFrame = hinge.CFrame * CFrame.Angles(0,math.rad(-90),0)}
local close = {
	CFrame = hinge.CFrame * CFrame.Angles(0,0,0)}

local doorOpen = TweenService:Create(hinge, Info, open)
local doorClose = TweenService:Create(hinge, Info, close)

--// Functions 
local function Clicked(player)
	print(player.Name.." Interacted: "..door.Name)

	if locked then
		if debounce then return end
		debounce = true
		hasInteracted:FireClient(player, locked)

		debounce = false
	elseif not locked then
		if debounce then return end
		debounce = true
		if isOpen == false then
			doorOpen:Play()
			isOpen = true

			debounce = false
		else
			doorClose:Play()
			isOpen = false

			debounce = false
		end
	end
end

clickDetector.MouseClick:Connect(Clicked)

EDIT: I’ve tried stuff like these before posting so they won’t work either:

if locked then
  if debounce then return end
end
--or
if locked and not debounce then
end
1 Like

Because you are immediatly setting debounce to false,

debounce = true
if isOpen == false then
			doorOpen:Play()
			isOpen = true

			debounce = false

what this does is it starts playing the tween AND it does NOT wait until it is finished, it immediat

Technically you are doing this with the debounce:
true
false
With no wait

so basically,

doorOpen:Play(); doorOpen.Completed:Wait()

fixes the issue, right?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.