Debounce For This Script

Any body able to modify this script so that a player can only blocks every 3 seconds?

module = {}

module.Block = function(Char,Health,Animation,ParryTime)
	local ParVal = Instance.new("BoolValue",Char)
	ParVal.Name = "Parry"
	game.Debris:AddItem(ParVal,ParryTime)
	
	Char:WaitForChild("Humanoid"):LoadAnimation(Animation):Play()
	
	local BlockVal = Instance.new("NumberValue",Char)
	BlockVal.Name = "Blocking"
	BlockVal.Value = Health
	
	delay(2,function()
		BlockVal:Destroy()
		print("Destroyed")
	end)
end

module.UnBlock = function(Char,AnimName)
	for i,v in pairs(Char:GetChildren()) do
		if v.Name == "Blocking" then
			v:Destroy()
		end
	end
	
	local Tracks = Char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()
	
	for i,v in pairs(Tracks) do
		if v.Name == AnimName then
			v:Stop()
			
		end
	end
	
end


return module




1 Like

ooops, here is corrected one

module = {}
local debounce = false

module.Block = function(Char,Health,Animation,ParryTime)
	if debounce then return end
	debounce = true
	local ParVal = Instance.new("BoolValue",Char)
	ParVal.Name = "Parry"
	game.Debris:AddItem(ParVal,ParryTime)

	Char:WaitForChild("Humanoid"):LoadAnimation(Animation):Play()

	local BlockVal = Instance.new("NumberValue",Char)
	BlockVal.Name = "Blocking"
	BlockVal.Value = Health

	delay(2,function() -- i assume this is the delay hit
		BlockVal:Destroy()
		debounce = false
		print("Destroyed")
	end)
end

If this is being called on the server, then you’d want a unique debounce per player in order to prevent one player’s debounce from affecting every other player whenever the Block function is called. This method stores a Debounce table in the module, and uses the player’s UserId as a unique key. If the player’s UserId is in the debounce table, then return and do nothing. Otherwise, add the player’s UserId to the debounce table and delay a function which removes it from the table, allowing them to Block again.

local Players = game:GetService("Players")

local DEBOUNCE_TIME = 3


module = {}

module.Debounce = {}

module.Block = function(Char,Health,Animation,ParryTime)
	
	local plr = Players:GetPlayerFromCharacter(Char)
	if module.Debounce[plr.UserId] then return end
	module.Debounce[plr.UserId] = true
	task.delay(DEBOUNCE_TIME, function() module.Debounce[plr.UserId] = nil end)

	local ParVal = Instance.new("BoolValue",Char)
	ParVal.Name = "Parry"
	game.Debris:AddItem(ParVal,ParryTime)

	Char:WaitForChild("Humanoid"):LoadAnimation(Animation):Play()

	local BlockVal = Instance.new("NumberValue",Char)
	BlockVal.Name = "Blocking"
	BlockVal.Value = Health

	delay(2,function()
		BlockVal:Destroy()
		print("Destroyed")
	end)
end

module.UnBlock = function(Char,AnimName)
	for i,v in pairs(Char:GetChildren()) do
		if v.Name == "Blocking" then
			v:Destroy()
		end
	end

	local Tracks = Char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()

	for i,v in pairs(Tracks) do
		if v.Name == AnimName then
			v:Stop()

		end
	end

end


return module

EDIT:
Unrelated, but if you are using a module script, it’s common practice to use

local module = {}

function module.Block()
     -- do stuff
end

return module
1 Like

ok thanks i will ty both of these

these are the other 2 script for blocking

RunS = game:GetService("RunService")
local Blocking = false

local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

local Stuns = require(game.ReplicatedStorage:WaitForChild("Combat"):WaitForChild("Stuns"))


local UIS = game:GetService("UserInputService")

Char.ChildAdded:Connect(function()
	for i,v in pairs(Stuns) do
		if v ~= "Blocking" then
			if game.Players.LocalPlayer.Character:FindFirstChild(v) then Blocking = false return end
		end
	end
end)

RunS.Heartbeat:Connect(function()
	
	for i,v in pairs(Stuns) do
		if game.Players.LocalPlayer.Character:FindFirstChild(v) then return end	
	end
	
	
	if UIS:IsKeyDown(Enum.KeyCode.F) then
		Blocking = true
		if Char:FindFirstChild("Blocking") == nil then
			script:WaitForChild("RemoteEvent"):FireServer("Start")
			end
	else
		Blocking = false
		if Char:FindFirstChild("Blocking") then
			script:WaitForChild("RemoteEvent"):FireServer("Stop")

				end)
			end
		end
	end
end)

local Values = require(game.ReplicatedStorage:WaitForChild(“Combat”):WaitForChild(“Values”))
script.Parent.OnServerEvent:Connect(function(plr,Action)
local Char = plr.Character
local Hum = Char:WaitForChild(“Humanoid”)
if Action == “Start” then
Values:CreateValue(“BoolValue”,Char,“Blocking”,false,math.huge)
Values:CreateValue(“BoolValue”,Char,“PB”,false,.1)

	local BlockAnim = Hum:LoadAnimation(script:WaitForChild("BlockAnim"))
	BlockAnim:Play()
end

if Action == "Stop" then
	for i,v in pairs(Char:GetChildren()) do
		if v.Name == "Blocking" then
			v:Destroy()
		end
	end
	
	local AnimTracks = Hum:GetPlayingAnimationTracks()
	for i,v in pairs(AnimTracks) do
		if v.Name == "BlockAnim" then
			v:Stop()
	
			


		end
	end
end

end)

i can still block with no cooldown