So I’ve been wondering for a long time and I wasn’t sure about this but: Is it ok to add 2 debounces (cooldowns) to your script?
I’m talking about this:
local Debounce = false
local Debounce2 = false
local function BlahBlah()
if not Debounce then
Debounce = true
print("Gud")
wait(5)
Debounce = true
elseif not Debounce2 then
Debounce2 = true
print("Super Gud")
wait(3)
Debounce2 = true
end
end
And if the code above doesn’t work, would there be an alternative to this?
Let me know if you have the answer.
local UserInputService = game:GetService("UserInputService")
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local deb1 = false
local deb2 = false
UserInputService.InputBegan:Connect(function(Input)
if not deb1 then --For first debounce
deb1 = true
if Input.KeyCode == Enum.KeyCode.Z then
local Anim1 = Humanoid:LoadAnimation(script.Anim)
Anim:Play() --Plays the animation once Z is pressed
end
wait(5)
deb1 = false
elseif not deb2 then --For second debounce
deb2 = true
if Input.KeyCode == Enum.KeyCode.Q then
local Anim2 = Humanoid:LoadAnimation(script.Anim2)
Anim2:Play() --Plays second animation when Q is pressed
end
wait(3)
deb2 = true
end
end)
The only issue this proposes is if you want both to be able to be executed in either order. Anim2 can only be activated if Anim1 already has been, with your current implementation. You could fix this by changing the else if into it’s own if statement.
Also, you should be setting the debounces inside the keycode check, otherwise they get set off for every key.
Otherwise, having two debounces is perfectly fine.
I don’t think there are any problems with this script, it’s just a different way to use debounces.
If the first input has a cooldown, then the second input will not.
--EXAMPLE 1--
else
if not deb2 then --For second debounce
deb2 = true
if Input.KeyCode == Enum.KeyCode.Q then
local Anim2 = Humanoid:LoadAnimation(script.Anim2)
Anim2:Play() --Plays second animation when Q is pressed
end
wait(3)
deb2 = true
end
end)
or do you mean this?:
--EXAMPLE 2--
if not deb2 then --For second debounce
deb2 = true
if Input.KeyCode == Enum.KeyCode.Q then
local Anim2 = Humanoid:LoadAnimation(script.Anim2)
Anim2:Play() --Plays second animation when Q is pressed
end
wait(3)
deb2 = true
end
end)
So I still don’t get why you would use this, do you want it to be so if Q was pressed then you can’t press Q again until you press Z because Right now it looks like there’s really no point because when a player presses the button again they will still have to wait for the animation to played, unless I’m wrong?
local Debounce = false
local function BlahBlah()
if not Debounce then
Debounce = true
print("Gud")
wait(5)
print("Super Gud")
wait(3)
Debounce = false
end
end
More like the second one. However, you should also only be changing debounces after you verify the current key. Here’s what that would look like:
local UserInputService = game:GetService("UserInputService")
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local deb1 = false
local deb2 = false
UserInputService.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.Z then --KeyCode Z
if not deb1 then --For first debounce
deb1 = true
local Anim1 = Humanoid:LoadAnimation(script.Anim)
Anim:Play() --Plays the animation once Z is pressed
wait(5)
deb1 = false
end
elseif Input.KeyCode == Enum.KeyCode.Q then --KeyCode Q
if not deb2 then --For second debounce
deb2 = true
local Anim2 = Humanoid:LoadAnimation(script.Anim2)
Anim2:Play() --Plays second animation when Q is pressed
wait(3)
deb2 = true
end
end
end)