Script ignoring block debounce

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 my block debounce to actually work.

  2. What is the issue? Include screenshots / videos if possible!
    Whenever you hold block it kinda doesn’t work

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I have, they didn’t help.

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!

Script:

- Removed to prevent stealing of code

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.

2 Likes

try this

local UIS = game:GetService("UserInputService")

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local IsBlocking = Character:WaitForChild("Information").Blocking.Value
local BlockAnimation = Character.Humanoid.Animator:LoadAnimation(script:WaitForChild("Block"))
local debounce = false

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and debounce ~= true then
		BlockAnimation:Play()
		game.ReplicatedStorage.Blocking:FireServer("On")

	end
end)

UIS.InputEnded:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and debounce ~= true then
		debounce = true
		BlockAnimation:Stop()
		game.ReplicatedStorage.Blocking:FireServer("Off")
	task.wait(2.5)
		debounce = false
	end
end)

I would actually just move the debounce up to right after you check if its false:

local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local IsBlocking = Character:WaitForChild("Information").Blocking.Value
local BlockAnimation = Character.Humanoid.Animator:LoadAnimation(script:WaitForChild("Block"))
local debounce = false


UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and debounce == false then
        debounce = true
		BlockAnimation:Play()
		game.ReplicatedStorage.Blocking:FireServer("On")

		end
end)

UIS.InputEnded:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F then
		BlockAnimation:Stop()
		game.ReplicatedStorage.Blocking:FireServer("Off")
		wait(2.5)
		debounce = false
	end
end)

It didn’t work, the problem still resides, if you spam F you can sometimes still spam block without CD.

This one didn’t work either, you can still spam F and you sometimes get no CD.

Maybe try to debounce each function separately?

I don’t wanna do that, is there another way?

check for debounce in input ended connection and it should behave as expected

UIS.InputEnded:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and not debounce then
		debounce = true
		BlockAnimation:Stop()
		game.ReplicatedStorage.Blocking:FireServer("Off")
		wait(2.5)
		debounce = false
	end
end)
local UIS = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local IsBlocking = Character:WaitForChild("Information").Blocking.Value
local BlockAnimation = Character.Humanoid.Animator:LoadAnimation(script:WaitForChild("Block"))
local debounce = false


UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and not debounce then
		BlockAnimation:Play()
		game.ReplicatedStorage.Blocking:FireServer("On")

		end
end)

UIS.InputEnded:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.F and not debounce then
		debounce = true
		BlockAnimation:Stop()
		game.ReplicatedStorage.Blocking:FireServer("Off")
		wait(2.5)
		debounce = false
	end
end)

I’m pretty sure you just wrote smooth’s script into the full script.

no not really
image + i didnt saw his script

Ah, well it looks almost exactly like smooth’s script, but just with the debounce = false change.

yea

The general form of a debounce is this:

local debounce = false

some_function_definition(some_parameters)
	if not debounce then
		debounce = true
		-- Do Something
		delay(timeout, function()
			debounce = false
		end)
	end
end

The delay function spawns a new thread that doesn’t start executing the nested function until the timeout has elapsed. This is the preferred way because it doesn’t hang up the thread context that the some_function is executing in.

Although this is being executed on the client, you should also have similar code on the server to prevent abuse by exploiters.

1 Like

I’ve tested this a bit, if you spam F then the cooldown kinda happens twice. Any fixes to this?