Hi i have this script for a block animation and i was wondering how i could make it so this script can only be activated every 3 seconds
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)
No, because it would cause delay and weird glitches. If you use task.wait and the script gets called again, it will create multiple tasks of one action.
local Check = False -- Debounce varible
Function()
if not Check then -- Checks if the Varible Check is false
Check = True -- Prevent the script to be called again
Task.wait(3) -- CoolDown
Check = False -- The Function Can be called again as check is now false
end
end)
Please Note this can be for anything and not just functions.
Just keep track of the time, pretty simple and does not require any sort of yielding to accomplish its goal.
local LastClick = os.time()
-- to keep track of the time since the function fired
function something()
local Elapsed = os.time() - LastClick -- gets the difference in time
if Elapsed < 5 then return end -- ends function if time has not reached
-- 5 seconds
-- fire something here
LastClick = os.time() -- resets timer
end
This would be more performant, and use less resources compared to yielding an entire thread, plus its a very simple and fast alternative to yielding
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()
local LastClick = os.time()
-- to keep track of the time since the function fired
local function CheckTime()
if os.time() - LastClick >= 1 then
Char:WaitForChild("Humanoid"):LoadAnimation(v.Animation):Play()
print("Played")
end
end
while v.IsPlaying do
CheckTime()
wait(5.5)
end
break
end
end
end
return module
Just like @Cairo said. Store the time when you first executed the function and have a conditional statement inside of that function to check weather or not that time has exceeded it’s cooldown time. If it’s not working then the reference you stored the time is not being cached properly.