If you spam 'F' the cooldown happens twice

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want it so that the cooldown on block only happens one time.

  2. What is the issue? Include screenshots / videos if possible!
    If you spam ‘F’ on your keyboard sometimes the cooldown happens two times instead of once.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I haven’t found a solution to my problem on devforum.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the server script:

local KeyEvent = game.ReplicatedStorage.KeyBindEvent
local blockdebounce = false
local IsBlocking = character:WaitForChild("Data").IsBlocking.Value
local BlockAnimation = character.Humanoid.Animator:LoadAnimation(game.ServerStorage.CharacterAnims.Test)
local KeyEndedEvent = game.ReplicatedStorage.KeyEndedEvent


KeyEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and not blockdebounce then
		BlockAnimation:Play()
		character:WaitForChild("BlockingEffects").Trail3.Enabled = true
		character:WaitForChild("Data").CantMove.Value = true
		character:WaitForChild("Data").IsBlocking.Value = true
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = true
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = true
	end
end)

KeyEndedEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and not blockdebounce then
		blockdebounce = true
		BlockAnimation:Stop()
		character:WaitForChild("Data").CantMove.Value = false 
		character:WaitForChild("Data").IsBlocking.Value = false
		character.Data:WaitForChild("BlockingEffects").Trail3.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = false
		wait(0.75)
		blockdebounce = false
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

I’m a bit confused about how this code even works sometimes as it is. The way I see it you press “F”, the debounce is set to true and then key ended won’t run?

My thoughts on what is going wrong are that you don’t currently check if the player is blocking during the key ended function. If you added that check I reckon you would fix the issue.

that’s why sometimes i add two debounce, The debounce at the start of the function, and another one when the command runs, hardly the client will bypass two debounce, only if your coding is really weird somehow

No, it runs because the blocking effects disappear.

It just seems to me like there’s a lot more going on than the code you’ve shown. What is in the client script?

Nothing, it just fires a remote event when a key is pressed and sends over what key was pressed, the server then checks what key it is and if it’s F then it blocks, In this case I’m only using the client to send information to the server, that’s it.

An alternative to using debounces is to use a timestamp instead. E.g. your key press function would check that os.time() - timestamp > 0.75 and the key ended function would update the timestamp to the current time.

How exactly would I replace the debounce turning to true or checking if the debounce was true?

Looking at your code as posted the blockdebounce is global to all functions. So when the function KeyEvent. turned it true … it was also true when it got to the KeyEndedEvent. function.

Server-Side debounce:

local debounce = {}
someRemote.OnServerEvent:Connect(function(player,...)
	if not table.find(debounce,player) then
		table.insert(debounce,player)
		
		--code here
		
		--when done:
		table.remove(debounce,table.find(debounce,player))
	end
end)
1 Like

Okay, I will try out this script and see if it works.

It still sometimes does the cooldown twice if you spam ‘F’

Did you put a delay/cooldown in the event function?

You haven’t made the blockdebounce true below it.

I tried that and I couldn’t unblock.

here

local KeyEvent = game.ReplicatedStorage.KeyBindEvent
local blockdebounce = false
local blockdebounce2 = false
local IsBlocking = character:WaitForChild("Data").IsBlocking.Value
local BlockAnimation = character.Humanoid.Animator:LoadAnimation(game.ServerStorage.CharacterAnims.Test)
local KeyEndedEvent = game.ReplicatedStorage.KeyEndedEvent


KeyEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and not blockdebounce and not blockdebounce2 then
blockdebounce2 = true
		BlockAnimation:Play()
		character:WaitForChild("BlockingEffects").Trail3.Enabled = true
		character:WaitForChild("Data").CantMove.Value = true
		character:WaitForChild("Data").IsBlocking.Value = true
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = true
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = true
	end
end)

KeyEndedEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and not blockdebounce and blockdebounce2 then
		blockdebounce = true
                blockdebounce2 = false
		BlockAnimation:Stop()
		character:WaitForChild("Data").CantMove.Value = false 
		character:WaitForChild("Data").IsBlocking.Value = false
		character.Data:WaitForChild("BlockingEffects").Trail3.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = false
		wait(0.75)
		blockdebounce = false
	end
end)
1 Like

Key should not be == F, rather. Key == Enum.KeyCode.F

Have you tried a debounce on both the Server-Side and Client-Side?

local KeyEvent = game.ReplicatedStorage.KeyBindEvent
local blockdebounce = false
local IsBlocking = character:WaitForChild("Data").IsBlocking.Value
local BlockAnimation = character.Humanoid.Animator:LoadAnimation(game.ServerStorage.CharacterAnims.Test)
local KeyEndedEvent = game.ReplicatedStorage.KeyEndedEvent


KeyEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and not blockdebounce then
        blockdebounce = true
		BlockAnimation:Play()
		character:WaitForChild("BlockingEffects").Trail3.Enabled = true
		character:WaitForChild("Data").CantMove.Value = true
		character:WaitForChild("Data").IsBlocking.Value = true
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = true
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = true
	end
end)

KeyEndedEvent.OnServerEvent:Connect(function(plr, key, IsTyping)
	if IsTyping then return end
	if key == "F" and blockdebounce then
		blockdebounce = true
		BlockAnimation:Stop()
		character:WaitForChild("Data").CantMove.Value = false 
		character:WaitForChild("Data").IsBlocking.Value = false
		character.Data:WaitForChild("BlockingEffects").Trail3.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail.Enabled = false
		character.Data:WaitForChild("BlockingEffects").Trail1.Enabled = false
		wait(0.75)
		blockdebounce = false
	end
end)

there should fix it

We are not looking at the whole code here as posted it don’t even work. He must be passing the key value but who knows … I pointed out a clear logic error and got no response back …

1 Like