I am trying to make an hatchet system but i am getting this bug with my blocking.
whenever i am trying to block while walking the walk animation will bug out and will play slowly and never stop. even if youre not moving this bugged walking animation wont stop. this happends only when the blocking starts.
attemted solutioins:
–setting walkspeed to 0 while start animation is playing
–disableing input while start animation is playing
–setting walking animation priority to movement.
–asking ai for solutions 100 times
–rewriting the block function
–force stopping walk animation
Here is the whole script:
local TheHatchet = {}
local animations = {
Start = nil,
End = nil,
Hit = nil,
Block = nil
}
local character, tool, humanoid
local blockingTransitionInProgress = false
local currentIdleTrack = nil
local SPEEDS = {
DEFAULT = 16,
BLOCKING = 8
}
local function playIdleAnimation()
if not tool or not humanoid then return end
local idleAnimation = tool:FindFirstChild("Idle")
if not idleAnimation then return end
if currentIdleTrack and currentIdleTrack.IsPlaying then
return currentIdleTrack
end
local idleTrack = humanoid:LoadAnimation(idleAnimation)
idleTrack.Looped = true
idleTrack.Priority = Enum.AnimationPriority.Idle
idleTrack:Play(0.25)
currentIdleTrack = idleTrack
return idleTrack
end
function TheHatchet.initialize(char, blockingFolder)
if not char then return false end
character = char
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
warn("No Humanoid found in character!")
return false
end
if not blockingFolder then
warn("Blocking folder is not provided!")
return false
end
local animator = humanoid:FindFirstChildOfClass("Animator")
or Instance.new("Animator", humanoid)
local startAnim = blockingFolder:FindFirstChild("Start")
local endAnim = blockingFolder:FindFirstChild("End")
local hitAnim = blockingFolder:FindFirstChild("Hit")
local blockAnim = blockingFolder:FindFirstChild("Block")
if not (startAnim and endAnim and hitAnim and blockAnim) then
warn("One or more animations missing!")
return false
end
animations.Start = animator:LoadAnimation(startAnim)
animations.End = animator:LoadAnimation(endAnim)
animations.Hit = animator:LoadAnimation(hitAnim)
animations.Block = animator:LoadAnimation(blockAnim)
for name, anim in pairs(animations) do
if not anim then
warn(name .. " animation failed to load!")
return false
end
end
animations.Start.Stopped:Connect(function()
if tool and tool:FindFirstChild("IsBlocking") and tool.IsBlocking.Value and not blockingTransitionInProgress then
if animations.Block then
animations.Block:Play()
end
end
end)
animations.End.Stopped:Connect(function()
if tool and tool:FindFirstChild("IsBlocking") and not tool.IsBlocking.Value then
blockingTransitionInProgress = false
playIdleAnimation()
end
end)
return true
end
function TheHatchet.setTool(currentTool)
tool = currentTool
playIdleAnimation()
end
local function stopAllAnimationsExcept(exceptAnims)
if not humanoid then return end
if type(exceptAnims) ~= "table" then
exceptAnims = {exceptAnims}
end
for _, track in pairs(humanoid:GetPlayingAnimationTracks()) do
local shouldStop = true
for _, exceptAnim in pairs(exceptAnims) do
if track == exceptAnim then
shouldStop = false
break
end
end
if shouldStop then
track:Stop(0.1)
end
end
end
function TheHatchet.block()
if not tool then return end
if blockingTransitionInProgress then return end
local isBlocking = tool:WaitForChild("IsBlocking")
local isAttacking = tool:WaitForChild("IsAttacking")
if isAttacking.Value then return end
blockingTransitionInProgress = true
if not isBlocking.Value then
isBlocking.Value = true
if not (animations.Start and animations.Block) then
blockingTransitionInProgress = false
return
end
animations.Start.Priority = Enum.AnimationPriority.Action4
animations.Block.Priority = Enum.AnimationPriority.Action4
stopAllAnimationsExcept(animations.Start)
animations.Start:Play()
humanoid.WalkSpeed = SPEEDS.BLOCKING
task.spawn(function()
if animations.Start.Length > 0 then
task.wait(animations.Start.Length)
else
task.wait(0.2)
end
if tool and isBlocking.Value and animations.Block and not animations.Block.IsPlaying then
animations.Block:Play()
stopAllAnimationsExcept(animations.Block)
end
blockingTransitionInProgress = false
end)
else
isBlocking.Value = false
if not animations.End then
blockingTransitionInProgress = false
return
end
animations.End.Priority = Enum.AnimationPriority.Action4
if animations.Block and animations.Block.IsPlaying then
animations.Block:Stop(0.1)
end
animations.End:Play()
humanoid.WalkSpeed = SPEEDS.DEFAULT
task.spawn(function()
task.wait(animations.End.Length + 0.1)
blockingTransitionInProgress = false
if tool and not isBlocking.Value and not tool:FindFirstChild("IsAttacking").Value then
playIdleAnimation()
end
end)
end
end
function TheHatchet.attack()
if not tool then return end
if blockingTransitionInProgress then return end
local isAttacking = tool:WaitForChild("IsAttacking")
local isBlocking = tool:WaitForChild("IsBlocking")
if isBlocking.Value or isAttacking.Value then
return
end
isAttacking.Value = true
local attackAnims = tool.Anims:GetChildren()
local randomAnim = attackAnims[math.random(1, #attackAnims)]
local loadedAnim = humanoid:LoadAnimation(randomAnim)
local preserveIdle = currentIdleTrack and currentIdleTrack.IsPlaying
local exceptAnims = preserveIdle and {loadedAnim, currentIdleTrack} or {loadedAnim}
loadedAnim.Priority = Enum.AnimationPriority.Action3
if preserveIdle then
currentIdleTrack:AdjustWeight(0.2, 0.2)
end
stopAllAnimationsExcept(exceptAnims)
loadedAnim:Play(0.1)
local canDamage = tool.canDamage
tool.s1:Play()
task.spawn(function()
task.wait(0.17)
if tool then
canDamage.Value = true
end
task.wait(0.27)
if tool then
canDamage.Value = false
end
local fadeOutStart = 0.66 - 0.2
task.wait(fadeOutStart)
if preserveIdle and currentIdleTrack and currentIdleTrack.IsPlaying then
currentIdleTrack:AdjustWeight(1, 0.2)
end
loadedAnim:Stop(0.2)
task.wait(0.2)
isAttacking.Value = false
if tool and not isBlocking.Value then
playIdleAnimation()
end
end)
end
function TheHatchet.cleanup()
blockingTransitionInProgress = false
currentIdleTrack = nil
if tool then
local isBlocking = tool:FindFirstChild("IsBlocking")
local isAttacking = tool:FindFirstChild("IsAttacking")
if isBlocking then isBlocking.Value = false end
if isAttacking then isAttacking.Value = false end
end
if humanoid then
stopAllAnimationsExcept(nil)
humanoid.WalkSpeed = SPEEDS.DEFAULT
end
for name in pairs(animations) do
animations[name] = nil
end
end
return TheHatchet