This code works but I don’t know if it’s the best way to do it. When you have your tool equipped and you press ‘F’ (I’m binding a ContextActionService when you equip the tool), it triggers the block modulescript and passes the argument “Pressed”. When you let go of the key it passes through “Released”, stops the animation, turns the attribute to not blocking, and goes on cooldown.
What should I change if anything?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FistsFolder = ReplicatedStorage.Assets.Animations.Weapons.Fists
local ParryTime = 0.2
local BlockingCooldown = 1
local Blocking = {}
local Debounce = {}
local function TriggerParry(char)
char:SetAttribute("CanParry", true)
task.delay(ParryTime, function()
char:SetAttribute("CanParry", false)
end)
end
local function PlayAnimation(hum, anim)
for _, name in pairs(hum.Animator:GetPlayingAnimationTracks()) do
if name.IsPlaying and name ~= anim and name.Priority == anim.Priority then
name:Stop()
end
end
anim:Play()
end
local function StopAnimation(hum, anim)
for _, name in pairs(hum.Animator:GetPlayingAnimationTracks()) do
if name.IsPlaying and name.Name == anim.Name and name.Priority == anim.Priority then
name:Stop()
end
end
end
function Blocking:Trigger(plr, arg)
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid
local AnimationTrack = hum.Animator:LoadAnimation(FistsFolder:WaitForChild("Block"))
if arg == "Pressed" then
if char:GetAttribute("Blocking") == true or table.find(Debounce, plr.Name) ~= nil then return end
char:SetAttribute("Blocking", true)
hum.WalkSpeed = 5
PlayAnimation(hum, AnimationTrack)
TriggerParry(char)
elseif arg == "Released" then
if char:GetAttribute("Blocking") == false or table.find(Debounce, plr.Name) ~= nil then return end
table.insert(Debounce, plr.Name)
char:SetAttribute("Blocking", false)
hum.WalkSpeed = char:GetAttribute("BaseWalkSpeed")
StopAnimation(hum, AnimationTrack)
task.wait(BlockingCooldown)
Debounce[table.find(Debounce, plr.Name)] = nil
end
end
return Blocking