Blocking Animation won't stop

I have a block script where if I hold a key down a block animation plays and when I release it, the animation is meant to stop.

But when I try to stop the animation, it does nothing.

CODE
ReplicatedStorage.Remotes.Combat.Block.OnServerEvent:Connect(function(player, blocking)
	
	local blockAnimations = {
		"rbxassetid://5325171131" -- block
	}
	
	local character = player.Character
	
	local blockAnimation = Instance.new("Animation")
	blockAnimation.AnimationId = blockAnimations[1]
	
	local blockTrack = character.Humanoid:LoadAnimation(blockAnimation)
	if blocking then
		isBlocking = true
		blockTrack:Play(0.2)
		print("Blocking animation playing!")
	else
		isBlocking = false
		blockTrack:Stop(0.2)
		print("Blocking animation stopped!")
	end
end)

The odd thing is that the print statements work as intended. Print “Blocking animation playing!” whilst I hold down the F key, and prints, “Blocking animation stopped!” when I stop holding down the F key.

What is the problem?

The reason is because you’re running 2 separate “block” things to the server
This makes it so that the “blockTrack” for the first remotefire is separate from the 2nd remotefire.
That means that it will play the track on the first one, but the 2nd one won’t stop anything because it’s a new loaded animation.

Suggested fix would be (considering that “isBlocking” is an outside boolvalue)

Code

ReplicatedStorage.Remotes.Combat.Block.OnServerEvent:Connect(function(player, blocking)

local blockAnimations = {
	"rbxassetid://5325171131" -- block
}

local character = player.Character

local blockAnimation = Instance.new("Animation")
blockAnimation.AnimationId = blockAnimations[1]

local blockTrack = character.Humanoid:LoadAnimation(blockAnimation)
if blocking then
	isBlocking = true
	blockTrack:Play(0.2)
	print("Blocking animation playing!")
            while isBlocking == true do wait(.1) end
            blockTrack:Stop(0.2)
	print("Blocking animation stopped!")
else
	isBlocking = false
end

end)

Made it so that it only changes the isBlocking to false on disable.
and made it so it never stops until isBlocking is set to false

1 Like