Sword Block Anim Not Working

I thought it was an issue with the animation itself, but I checked several times and it’s not. So I was beginning to work on blocking for the sword, and when you hold F it’s meant to block. I tried to start by just making it play an animation when holding F. This doesn’t work though and I can’t figure out why. Just curious if anyone can figure out the issue here, thanks!

Script:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local unsheatheSound = Instance.new("Sound")
unsheatheSound.SoundId = "rbxassetid://12222225"
unsheatheSound.Parent = handle
local attackSound = Instance.new("Sound")
attackSound.SoundId = "rbxassetid://12222216"
attackSound.Parent = handle
local attackAnimationIds = {
	"rbxassetid://92578161823729",
	"rbxassetid://130993364618704",
	"rbxassetid://139811827055049",
	"rbxassetid://114162687869256"
}
local idleAnimationId = "rbxassetid://86960806700106"
local fAnimationId = "rbxassetid://136442390834081"
local idleTrack = nil
local fAnimTrack = nil
local isAttacking = false
local attackCount = 0
local comboCooldown = false
local lastAttackFinished = 0
local defaultWalkSpeed = nil
local currentHumanoid = nil
local inputBeganConn, inputEndedConn = nil, nil

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if idleTrack then
			idleTrack:Stop()
			idleTrack = nil
		end
		if currentHumanoid then
			local fAnim = Instance.new("Animation")
			fAnim.AnimationId = fAnimationId
			fAnimTrack = currentHumanoid:LoadAnimation(fAnim)
			fAnimTrack.Looped = true
			fAnimTrack:Play()
		end
	end
end

local function onInputEnded(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if fAnimTrack then
			fAnimTrack:Stop()
			fAnimTrack = nil
		end
		if currentHumanoid then
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end
	end
end

local function onEquipped()
	unsheatheSound:Play()
	local character = tool.Parent
	currentHumanoid = character:FindFirstChildOfClass("Humanoid")
	if currentHumanoid then
		defaultWalkSpeed = currentHumanoid.WalkSpeed
		local equipAnim = Instance.new("Animation")
		equipAnim.AnimationId = "rbxassetid://102168004033968"
		local equipTrack = currentHumanoid:LoadAnimation(equipAnim)
		equipTrack:Play()
		equipTrack.Stopped:Connect(function()
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end)
	end
	inputBeganConn = UserInputService.InputBegan:Connect(onInputBegan)
	inputEndedConn = UserInputService.InputEnded:Connect(onInputEnded)
end

local function startCooldown()
	comboCooldown = true
	delay(1.5, function()
		comboCooldown = false
		attackCount = 0
	end)
end

local function onActivated()
	if isAttacking or comboCooldown then return end
	isAttacking = true
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = 5
		if idleTrack then idleTrack:Stop() end
		attackCount = attackCount + 1
		local index = attackCount
		if index > #attackAnimationIds then index = #attackAnimationIds end
		local attackAnim = Instance.new("Animation")
		attackAnim.AnimationId = attackAnimationIds[index]
		local currentAttack = humanoid:LoadAnimation(attackAnim)
		currentAttack:Play()
		attackSound:Play()
		delay(0.2, function()
			local replicatedStorage = game:GetService("ReplicatedStorage")
			local hitboxTemplate = replicatedStorage:WaitForChild("sword_hitbox")
			local hitbox = hitboxTemplate:Clone()
			hitbox.Anchored = false
			hitbox.CanCollide = false
			local hrp = character:FindFirstChild("HumanoidRootPart")
			if hrp then
				hitbox.CFrame = hrp.CFrame * CFrame.new(0, 0, -3)
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = hrp
				weld.Part1 = hitbox
				weld.Parent = hitbox
			else
				hitbox.CFrame = handle.CFrame * CFrame.new(0, 0, 3)
			end
			local hitHumanoids = {}
			hitbox.Touched:Connect(function(hit)
				local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
				if not hitHumanoid and hit.Parent.Parent then
					hitHumanoid = hit.Parent.Parent:FindFirstChildOfClass("Humanoid")
				end
				if hitHumanoid and not hit:IsDescendantOf(character) and not hitHumanoids[hitHumanoid] then
					hitHumanoids[hitHumanoid] = true
					hitHumanoid:TakeDamage(10)
				end
			end)
			hitbox.Parent = workspace
			game:GetService("Debris"):AddItem(hitbox, 0.25)
		end)
		currentAttack.Stopped:Connect(function()
			if humanoid and defaultWalkSpeed then
				humanoid.WalkSpeed = defaultWalkSpeed
			end
			isAttacking = false
			if tool.Parent and tool.Parent:FindFirstChildOfClass("Humanoid") then
				local idleAnim = Instance.new("Animation")
				idleAnim.AnimationId = idleAnimationId
				idleTrack = humanoid:LoadAnimation(idleAnim)
				idleTrack.Looped = true
				idleTrack:Play()
			end
			lastAttackFinished = tick()
			if attackCount >= #attackAnimationIds then
				startCooldown()
			else
				delay(0.5, function()
					if tick() - lastAttackFinished >= 0.5 and not isAttacking then
						startCooldown()
					end
				end)
			end
		end)
	else
		isAttacking = false
	end
end

local function onUnequipped()
	local character = tool.Parent
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid and defaultWalkSpeed then
			humanoid.WalkSpeed = defaultWalkSpeed
		end
	end
	if idleTrack then
		idleTrack:Stop()
		idleTrack = nil
	end
	if fAnimTrack then
		fAnimTrack:Stop()
		fAnimTrack = nil
	end
	if inputBeganConn then
		inputBeganConn:Disconnect()
		inputBeganConn = nil
	end
	if inputEndedConn then
		inputEndedConn:Disconnect()
		inputEndedConn = nil
	end
	currentHumanoid = nil
	isAttacking = false
	attackCount = 0
	comboCooldown = false
end

tool.Equipped:Connect(onEquipped)
tool.Activated:Connect(onActivated)
tool.Unequipped:Connect(onUnequipped)

Thanks again!

1 Like

I would suggest to load animations once and then utilize them as you want.
Instead of Loading each time different animation instance that breaks all the logic

2 Likes

I tried this like you suggested, doesn’t work. Any ideas what I did wrong?:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local unsheatheSound = Instance.new("Sound")
unsheatheSound.SoundId = "rbxassetid://12222225"
unsheatheSound.Parent = handle
local attackSound = Instance.new("Sound")
attackSound.SoundId = "rbxassetid://12222216"
attackSound.Parent = handle
local attackAnimationIds = {
	"rbxassetid://92578161823729",
	"rbxassetid://130993364618704",
	"rbxassetid://139811827055049",
	"rbxassetid://114162687869256"
}
local idleAnimationId = "rbxassetid://86960806700106"
local fAnimationId = "rbxassetid://136442390834081"
local idleTrack = nil
local fAnimTrack = nil
local isAttacking = false
local attackCount = 0
local comboCooldown = false
local lastAttackFinished = 0
local defaultWalkSpeed = nil
local currentHumanoid = nil
local inputBeganConn, inputEndedConn = nil, nil

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if idleTrack then
			idleTrack:Stop()
			idleTrack = nil
		end
		if fAnimTrack then
			fAnimTrack:Play()
		end
	end
end

local function onInputEnded(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if fAnimTrack then
			fAnimTrack:Stop()
		end
		if currentHumanoid then
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end
	end
end

local function onEquipped()
	unsheatheSound:Play()
	local character = tool.Parent
	currentHumanoid = character:FindFirstChildOfClass("Humanoid")
	if currentHumanoid then
		defaultWalkSpeed = currentHumanoid.WalkSpeed
		local equipAnim = Instance.new("Animation")
		equipAnim.AnimationId = "rbxassetid://102168004033968"
		local equipTrack = currentHumanoid:LoadAnimation(equipAnim)
		equipTrack:Play()
		equipTrack.Stopped:Connect(function()
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end)
		local fAnim = Instance.new("Animation")
		fAnim.AnimationId = fAnimationId
		fAnimTrack = currentHumanoid:LoadAnimation(fAnim)
		fAnimTrack.Looped = true
	end
	inputBeganConn = UserInputService.InputBegan:Connect(onInputBegan)
	inputEndedConn = UserInputService.InputEnded:Connect(onInputEnded)
end

local function startCooldown()
	comboCooldown = true
	delay(1.5, function()
		comboCooldown = false
		attackCount = 0
	end)
end

local function onActivated()
	if isAttacking or comboCooldown then return end
	isAttacking = true
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = 5
		if idleTrack then idleTrack:Stop() end
		attackCount = attackCount + 1
		local index = attackCount
		if index > #attackAnimationIds then index = #attackAnimationIds end
		local attackAnim = Instance.new("Animation")
		attackAnim.AnimationId = attackAnimationIds[index]
		local currentAttack = humanoid:LoadAnimation(attackAnim)
		currentAttack:Play()
		attackSound:Play()
		delay(0.2, function()
			local replicatedStorage = game:GetService("ReplicatedStorage")
			local hitboxTemplate = replicatedStorage:WaitForChild("sword_hitbox")
			local hitbox = hitboxTemplate:Clone()
			hitbox.Anchored = false
			hitbox.CanCollide = false
			local hrp = character:FindFirstChild("HumanoidRootPart")
			if hrp then
				hitbox.CFrame = hrp.CFrame * CFrame.new(0, 0, -3)
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = hrp
				weld.Part1 = hitbox
				weld.Parent = hitbox
			else
				hitbox.CFrame = handle.CFrame * CFrame.new(0, 0, 3)
			end
			local hitHumanoids = {}
			hitbox.Touched:Connect(function(hit)
				local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
				if not hitHumanoid and hit.Parent.Parent then
					hitHumanoid = hit.Parent.Parent:FindFirstChildOfClass("Humanoid")
				end
				if hitHumanoid and not hit:IsDescendantOf(character) and not hitHumanoids[hitHumanoid] then
					hitHumanoids[hitHumanoid] = true
					hitHumanoid:TakeDamage(10)
				end
			end)
			hitbox.Parent = workspace
			game:GetService("Debris"):AddItem(hitbox, 0.25)
		end)
		currentAttack.Stopped:Connect(function()
			if humanoid and defaultWalkSpeed then
				humanoid.WalkSpeed = defaultWalkSpeed
			end
			isAttacking = false
			if tool.Parent and tool.Parent:FindFirstChildOfClass("Humanoid") then
				local idleAnim = Instance.new("Animation")
				idleAnim.AnimationId = idleAnimationId
				idleTrack = humanoid:LoadAnimation(idleAnim)
				idleTrack.Looped = true
				idleTrack:Play()
			end
			lastAttackFinished = tick()
			if attackCount >= #attackAnimationIds then
				startCooldown()
			else
				delay(0.5, function()
					if tick() - lastAttackFinished >= 0.5 and not isAttacking then
						startCooldown()
					end
				end)
			end
		end)
	else
		isAttacking = false
	end
end

local function onUnequipped()
	local character = tool.Parent
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid and defaultWalkSpeed then
			humanoid.WalkSpeed = defaultWalkSpeed
		end
	end
	if idleTrack then
		idleTrack:Stop()
		idleTrack = nil
	end
	if fAnimTrack then
		fAnimTrack:Stop()
		fAnimTrack = nil
	end
	if inputBeganConn then
		inputBeganConn:Disconnect()
		inputBeganConn = nil
	end
	if inputEndedConn then
		inputEndedConn:Disconnect()
		inputEndedConn = nil
	end
	currentHumanoid = nil
	isAttacking = false
	attackCount = 0
	comboCooldown = false
end

tool.Equipped:Connect(onEquipped)
tool.Activated:Connect(onActivated)
tool.Unequipped:Connect(onUnequipped)

Thanks!

1 Like

what exactly did not work? Any errors?

2 Likes

Everything else works, it just doesn’t play the blocking animation when I hold F, that’s the only issue. No errors showing up in the script or command output.

1 Like

Through my testing I believe I found no issues with any of the logic, you need to double-check that the animation you are using is correct.

2 Likes

I’ve check everything with the animation, what else could there be that I’m not thinking about?

1 Like

Well, unfortunately without more information we cannot help. I was able to get it working in my own game, and was able to toggle the animation between idle and fAnim.

2 Likes

k, Ill try more things with the animation and get back to you if I figure something out

1 Like

Sorry I took a break for a while. Can I ask, did you put your own animation IDs in the script while testing it, and if so can I ask the priority each of yours?

1 Like

To make it easier i manually set animation priorities to show you, and it still doesnt work:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local unsheatheSound = Instance.new("Sound")
unsheatheSound.SoundId = "rbxassetid://12222225"
unsheatheSound.Parent = handle
local attackSound = Instance.new("Sound")
attackSound.SoundId = "rbxassetid://12222216"
attackSound.Parent = handle
local attackAnimationIds = {
	"rbxassetid://92578161823729",
	"rbxassetid://130993364618704",
	"rbxassetid://139811827055049",
	"rbxassetid://114162687869256"
}
local idleAnimationId = "rbxassetid://138012174595072"
local fAnimationId = "rbxassetid://88277359605875"
local idleTrack = nil
local fAnimTrack = nil
local isAttacking = false
local attackCount = 0
local comboCooldown = false
local lastAttackFinished = 0
local defaultWalkSpeed = nil
local currentHumanoid = nil
local inputBeganConn, inputEndedConn = nil, nil

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if idleTrack then
			idleTrack:Stop()
			idleTrack = nil
		end
		if fAnimTrack then
			fAnimTrack:Play()
		end
	end
end

local function onInputEnded(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.F then
		if fAnimTrack then
			fAnimTrack:Stop()
		end
		if currentHumanoid then
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleAnim.Priority = Enum.AnimationPriority.Idle
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end
	end
end

local function onEquipped()
	unsheatheSound:Play()
	local character = tool.Parent
	currentHumanoid = character:FindFirstChildOfClass("Humanoid")
	if currentHumanoid then
		defaultWalkSpeed = currentHumanoid.WalkSpeed
		local equipAnim = Instance.new("Animation")
		equipAnim.AnimationId = "rbxassetid://75142917130197"
		equipAnim.Priority = Enum.AnimationPriority.Action
		local equipTrack = currentHumanoid:LoadAnimation(equipAnim)
		equipTrack:Play()
		equipTrack.Stopped:Connect(function()
			local idleAnim = Instance.new("Animation")
			idleAnim.AnimationId = idleAnimationId
			idleAnim.Priority = Enum.AnimationPriority.Idle
			idleTrack = currentHumanoid:LoadAnimation(idleAnim)
			idleTrack.Looped = true
			idleTrack:Play()
		end)
		local fAnim = Instance.new("Animation")
		fAnim.AnimationId = fAnimationId
		fAnim.Priority = Enum.AnimationPriority.Action2
		fAnimTrack = currentHumanoid:LoadAnimation(fAnim)
		fAnimTrack.Looped = true
	end
	inputBeganConn = UserInputService.InputBegan:Connect(onInputBegan)
	inputEndedConn = UserInputService.InputEnded:Connect(onInputEnded)
end

local function startCooldown()
	comboCooldown = true
	delay(1.5, function()
		comboCooldown = false
		attackCount = 0
	end)
end

local function onActivated()
	if isAttacking or comboCooldown then return end
	isAttacking = true
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		humanoid.WalkSpeed = 5
		if idleTrack then idleTrack:Stop() end
		attackCount = attackCount + 1
		local index = attackCount
		if index > #attackAnimationIds then index = #attackAnimationIds end
		local attackAnim = Instance.new("Animation")
		attackAnim.AnimationId = attackAnimationIds[index]
		attackAnim.Priority = Enum.AnimationPriority.Action
		local currentAttack = humanoid:LoadAnimation(attackAnim)
		currentAttack:Play()
		attackSound:Play()
		delay(0.2, function()
			local replicatedStorage = game:GetService("ReplicatedStorage")
			local hitboxTemplate = replicatedStorage:WaitForChild("sword_hitbox")
			local hitbox = hitboxTemplate:Clone()
			hitbox.Anchored = false
			hitbox.CanCollide = false
			local hrp = character:FindFirstChild("HumanoidRootPart")
			if hrp then
				hitbox.CFrame = hrp.CFrame * CFrame.new(0, 0, -3)
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = hrp
				weld.Part1 = hitbox
				weld.Parent = hitbox
			else
				hitbox.CFrame = handle.CFrame * CFrame.new(0, 0, 3)
			end
			local hitHumanoids = {}
			hitbox.Touched:Connect(function(hit)
				local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
				if not hitHumanoid and hit.Parent.Parent then
					hitHumanoid = hit.Parent.Parent:FindFirstChildOfClass("Humanoid")
				end
				if hitHumanoid and not hit:IsDescendantOf(character) and not hitHumanoids[hitHumanoid] then
					hitHumanoids[hitHumanoid] = true
					hitHumanoid:TakeDamage(10)
				end
			end)
			hitbox.Parent = workspace
			game:GetService("Debris"):AddItem(hitbox, 0.25)
		end)
		currentAttack.Stopped:Connect(function()
			if humanoid and defaultWalkSpeed then
				humanoid.WalkSpeed = defaultWalkSpeed
			end
			isAttacking = false
			if tool.Parent and tool.Parent:FindFirstChildOfClass("Humanoid") then
				local idleAnim = Instance.new("Animation")
				idleAnim.AnimationId = idleAnimationId
				idleAnim.Priority = Enum.AnimationPriority.Idle
				idleTrack = humanoid:LoadAnimation(idleAnim)
				idleTrack.Looped = true
				idleTrack:Play()
			end
			lastAttackFinished = tick()
			if attackCount >= #attackAnimationIds then
				startCooldown()
			else
				delay(0.5, function()
					if tick() - lastAttackFinished >= 0.5 and not isAttacking then
						startCooldown()
					end
				end)
			end
		end)
	else
		isAttacking = false
	end
end

local function onUnequipped()
	local character = tool.Parent
	if character then
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if humanoid and defaultWalkSpeed then
			humanoid.WalkSpeed = defaultWalkSpeed
		end
	end
	if idleTrack then
		idleTrack:Stop()
		idleTrack = nil
	end
	if fAnimTrack then
		fAnimTrack:Stop()
		fAnimTrack = nil
	end
	if inputBeganConn then
		inputBeganConn:Disconnect()
		inputBeganConn = nil
	end
	if inputEndedConn then
		inputEndedConn:Disconnect()
		inputEndedConn = nil
	end
	currentHumanoid = nil
	isAttacking = false
	attackCount = 0
	comboCooldown = false
end

tool.Equipped:Connect(onEquipped)
tool.Activated:Connect(onActivated)
tool.Unequipped:Connect(onUnequipped)

Have you made sure the animations and published from the same group/user that owns the experience?

Sorry, went to bed for the night. Yes, I’ve published the animations myself. I’ve tried this script in a fresh baseplate and it DID NOT work. The game is in R6 if that could possibly affect the animations, and yes all my animations have been R6 as well.

Yes I would have had to use my own animation Ids