I am making a dynamic menu and I want to add a automatic cooldown mechanism for opening and closing the menu.
So when the down tween is completed, you can do the up tween by calling the function or in my case, clicking the M key.
Problem is, if I tween it, I can keep tweening it by spamming M and it breaks the menu.
function Menu.Function(actionName, inputState, _inputObject)
if inputState ~= Enum.UserInputState.Begin then
return
end
if not localPlayer.Character then
CreateWarning("Character was not found trying to access menu!")
return
end
local Character = localPlayer.Character
local Humanoid = Character:FindFirstChild("Humanoid"):: Humanoid
if Humanoid.Health ~= math.huge then
CreateWarning("Try accessing the menu in the safe zone!")
return
end
local Background = MenuGui:FindFirstChild("Background"):: Frame
local tweenBackgroundDown = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,0.5,0) }):: Tween
local tweenBackgroundUp = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,-0.5,0) }):: Tween
local tweenDownCompleted = tweenBackgroundDown.Completed:Connect(function()
ENABLED = true
end)
local tweenUpCompleted = tweenBackgroundDown.Completed:Connect(function()
ENABLED = false
end)
--TODO: Create a automatic cooldown for tweens.
if not ENABLED then
tweenBackgroundDown:Play()
print(tostring(ENABLED))
else
tweenBackgroundUp:Play()
print(tostring(ENABLED)
end
--print("You tried to access the menu!")
end
)
as you can see, its very spammable…
Therefore, I want to check if one of the tweens is playing it will not be able to tween until it’s completed.
First of all, you need a value to toggle whether the menu is open or closed. The ENABLED value should be set to false when the tween completes and set to true when either tween plays. When the enabled value is true, it means the debounce is on and you shouldn’t open or close the menu.
I already have the toggle but I need help with the debounce
Here is what I have currently…
function Menu.Function(actionName, inputState, _inputObject)
if inputState ~= Enum.UserInputState.Begin then
return
end
if not localPlayer.Character then
CreateWarning("Character was not found trying to access menu!")
return
end
local Character = localPlayer.Character
local Humanoid = Character:FindFirstChild("Humanoid"):: Humanoid
if Humanoid.Health ~= math.huge then
CreateWarning("Try accessing the menu in the safe zone!")
return
end
local Background = MenuGui:FindFirstChild("Background"):: Frame
local tweenBackgroundDown = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,0.5,0) }):: Tween
local tweenBackgroundUp = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,-0.5,0) }):: Tween
local tweenDownCompleted = tweenBackgroundDown.Completed:Connect(function()
ENABLED = true
end)
local tweenUpCompleted = tweenBackgroundUp.Completed:Connect(function()
ENABLED = false
end)
--TODO: Create a automatic cooldown for tweens.
if not ENABLED then
tweenBackgroundDown:Play()
else
tweenBackgroundUp:Play()
end
if tweenBackgroundDown.PlaybackState == Enum.PlaybackState.Playing then
-- playing down
end
if tweenBackgroundUp.PlaybackState == Enum.PlaybackState.Playing then
-- playing up
end
print(ENABLED)
end
I also found out a error on my side that i fixed. Now it works, but it just doesn’t have a cooldown.
Just like what Subtotal said, you can create a debounce to fix this. A debounce let’s you control when a player can activate a function with their input. Basically, a debounce works like this:
local debounce = false
if debounce == false then
debounce = true
local function foo()
print(“bar”)
end
debounce = false
end
The first line in the if-statement, “debounce = true” prevents the code from being ran until the debounce is gone. We remove the debounce using the last line in this if-statement, “debounce = false”. This allows the code to be ran again.
You can use this to script many things, such as a gun reload, opening a door, etc.
I know what debounces are and I didn’t want to use them. I wanted to make a mechanism that auto checks without using debounces
Here’s one way I found a solution without using debounces
function Menu.Function(actionName, inputState, _inputObject)
if inputState ~= Enum.UserInputState.Begin then
return
end
if not localPlayer.Character then
CreateWarning("Character was not found trying to access menu!")
return
end
local Character = localPlayer.Character
local Humanoid = Character:FindFirstChild("Humanoid"):: Humanoid
if Humanoid.Health ~= math.huge then
CreateWarning("Try accessing the menu in the safe zone!")
return
end
local Background = MenuGui:FindFirstChild("Background"):: Frame
local tweenBackgroundDown = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,0.5,0) }):: Tween
local tweenBackgroundUp = TweenService:Create(Background, TweenInfo.new(BACKGROUND_SLIDE,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out),
{ Position = UDim2.new(0.5,0,-0.5,0) }):: Tween
--TODO: Create a automatic cooldown for tweens.
if not ENABLED and Background.Position == UDim2.new(0.5,0,-0.5,0) then
tweenBackgroundDown:Play()
elseif not ENABLED and Background.Position == UDim2.new(0.5,0,0.5,0) then
tweenBackgroundUp:Play()
end
end