Animations continues to play when tools are not equipped

Recently, I’ve made a sword tool, and I added a blocking system into the sword. And I scripted the sword blocking animation into a certain keybind so that the animation plays whenever a desired key is pressed. But now, for some reason you’re still able to play the blocking animation when the tool isn’t equipped. I tried looking for other solutions on the dev forum, but still haven’t found a answer yet.

It’s a local script by the way.

script:

-- wait(2)

local UIS = game:GetService("UserInputService")
local char = game.Players.LocalPlayer.Character
local hum = char:FindFirstChild("Humanoid")

local walkspeed = hum.WalkSpeed
local blockSpeed = hum.WalkSpeed / 2
local block = game:GetService("ReplicatedStorage").Block

UIS.InputBegan:Connect(function(input,inchat)
	if inchat then
	elseif input.KeyCode == Enum.KeyCode.F then
		hum.WalkSpeed = blockSpeed
		local Anim = script.Block
		playanim = hum:LoadAnimation(Anim)
		playanim:Play()
		block:FireServer()
	end
end)

UIS.InputEnded:Connect(function(input,inchat)
	if inchat then
	elseif input.KeyCode == Enum.KeyCode.F then
		hum.WalkSpeed = walkspeed
		playanim:Stop()
		block:FireServer()
		
	end
end)


hum.block.Changed:Connect(function()
	if hum.block.Value == false then
		playanim:Stop()
	end
end)

1 Like

This is intended behavior. Animations are supposed to function even if some of the limbs (such as a sword) are missing. You could probably just add a boolean flag saying whether you have the sword equipped, bind that to the sword’s Tool.Equipped and Tool.Unequipped events, and conjoin that with the already-existing inchat flag.

-- defining variables...

local swordEquipped = false
local sword = --sword

sword.Equipped:Connect(function()
	swordEquipped = true
end)

sword.Unequipped:Connect(function()
	swordEquipped = false
	-- stop blocking here too maybe?
end)

UIS.InputBegan:Connect(function(input, inchat)
	if not inchat and swordEquipped and input.KeyCode == Enum.KeyCode.F then
		-- start blocking
	end
end)

-- similarly for UIS.InputEnded

Could you maybe clarify on what you said? I’m new to scripting and I don’t quite understand.

Which part are you confused about?

  • How animations work?

  • What an event is?

  • What a Tool is?

  • What a flag is? What a boolean is?

  • if ... then ... end statements?

  • The code I gave you? Comments in said code?

  • Something more/less specific?

  • Maybe I missed something.

I’m confused on the code that you gave me and can you be more specific on what a boolean is. And as well as if… then… end statements.