I made a door and tried to debounce it. The door works fine but the I can still spam the door open and closed. Can anyone show me why the debounce doesnt work?
Thanks
local TweenService = game:GetService("TweenService")
local door = script.Parent.Door
local hinge = script.Parent.Hinge
local prompt = door.ProximityPrompt
local goalOpen = {}
goalOpen.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(-90), 0)
local goalClose = {}
goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(1.5)
local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)
local soundopen = script.Parent.Door.opening
local soundclose = script.Parent.Door.closing
local debounce = false
prompt.Triggered:Connect(function()
if debounce then return end
debounce = true
soundopen:Play()
if prompt.ActionText == "Close" then
tweenClose:Play()
prompt.ActionText = "Open"
soundclose:Play()
else
tweenOpen:Play()
prompt.ActionText = "Close"
end
debounce = false
end)
local TweenService = game:GetService("TweenService")
local door = script.Parent.Door
local hinge = script.Parent.Hinge
local prompt = door.ProximityPrompt
local goalOpen = {}
goalOpen.CFrame = hinge.CFrame * CFrame.Angles(0, math.rad(-90), 0)
local goalClose = {}
goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)
local tweenInfo = TweenInfo.new(1.5)
local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)
local soundopen = script.Parent.Door.opening
local soundclose = script.Parent.Door.closing
local debounce = false
prompt.Triggered:Connect(function()
if debounce then return end
debounce = true
soundopen:Play()
if prompt.ActionText == "Close" then
tweenClose:Play()
prompt.ActionText = "Open"
soundclose:Play()
else
tweenOpen:Play()
prompt.ActionText = "Close"
end
task.wait(5)
debounce = false
end)
You just forgot to add a delay, I’ve added a temporary one which you’re free to change.
local TweenService = game:GetService("TweenService")
local Action = false
local TweenStyle, TweenDirection = Enum.EasingStyle.Quint, Enum.EasingDirection.Out
local Parent = script.Parent
local Hinge = Parent.Hinge
local Door = Parent.Door
local OpenAngle = CFrame.Angles(0, math.rad(-90), 0)
local CloseAngle = CFrame.Angles(0, 0, 0)
local CloseGoal = {CFrame = Hinge.CFrame * CloseAngle}
local OpenGoal = {CFrame = Hinge.CFrame * OpenAngle}
local Prompt = Door.ProximityProp
local Debounce = os.clock()
local Tweens = {
[false] = TweenService:Create(Hinge, TweenInfo.new(1.5, TweenStyle, TweenDirection), CloseGoal),
[true] = TweenService:Create(Hinge, TweenInfo.new(1.5, TweenStyle, TweenDirection), OpenGoal)
}
local Sounds = {
[false] = Door.Closing,
[true] = Door.Opening
}
local function OnTriggeredUpdate()
if os.clock() - Debounce > 1 then
Debounce = os.clock()
local TweenAnimation = Tweens[Action]
TweenAnimation:Play()
local SoundEffect = Sounds[Action]
SoundEffect:Play()
Action = not Action
end
end
Prompt.Triggered:Connect(OnTriggeredUpdate)