Can't stop animations from playing

Hey devs!

I make a system where if the player equips a tool it will start the walking and idle animations. When I unequip the tool though, it does not stop.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

local RaycastHitboxV4 = require(ReplicatedStorage.Modules.RaycastHitboxV4)

local Tool = script.Parent
local Handle = Tool.Handle
local Anims = Tool.Anims

local Hitbox = RaycastHitboxV4.new(Tool.Hitbox)

local debounce = false
local canHit = false
local walking = false
local currentWalkAnim = nil
local currentIdleAnim = nil

local swingIndex = 1
local swingAnimations = Anims:GetChildren()
local swingDamage = {10, 10, 10, 30}
local knockbackMagnitude = {50, 50, 50, 100}

config = {
	AttackTime = 0.5,
	Cooldown = 0.25,
	TouchCooldown = 0.2,
	WalkAnimationId = "rbxassetid://18894708421",
	IdleAnimationId = "rbxassetid://18894696004",
}

local lastTouchTime = 0

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

local function GivePushback(character, magnitude)
	local bv = Instance.new("BodyVelocity", character:FindFirstChild("HumanoidRootPart"))
	bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * -magnitude
	Debris:AddItem(bv, 0.1)
end

Hitbox.OnHit:Connect(function(hit, humanoid)
	if not canHit then return end

	local currentTime = tick()
	if currentTime - lastTouchTime < config.TouchCooldown then
		return
	end

	lastTouchTime = currentTime
	canHit = false

	if humanoid.Parent ~= script.Parent.Parent then
		UntagHumanoid(humanoid)
		TagHumanoid(humanoid, Player)
		humanoid:TakeDamage(swingDamage[swingIndex])

		local Character = humanoid.Parent
		GivePushback(Character, knockbackMagnitude[swingIndex])

		task.wait(0.35)
	end

	canHit = true
end)

local function PlayAnimation(Humanoid, animationId)
	local anim = Instance.new("Animation")
	anim.AnimationId = animationId
	local animationTrack = Humanoid:LoadAnimation(anim)
	animationTrack:Play()
	return animationTrack
end

local function StopCurrentAnimation(animationTrack)
	if animationTrack then
		animationTrack:Stop()
	end
end

local function OnWalking(Humanoid, speed)
	if speed > 0 then
		if not walking then
			walking = true
			StopCurrentAnimation(currentIdleAnim)
			currentWalkAnim = PlayAnimation(Humanoid, config.WalkAnimationId)
		end
	else
		if walking then
			walking = false
			StopCurrentAnimation(currentWalkAnim)
			currentIdleAnim = PlayAnimation(Humanoid, config.IdleAnimationId)
		end
	end
end

script.Parent.Hit.OnServerEvent:Connect(function(player)
	local Humanoid = player.Character.Humanoid

	if not debounce then
		debounce = true

		local Selected = Humanoid:LoadAnimation(swingAnimations[swingIndex])
		Selected:Play()

		Player = player

		print("Playing swing animation")

		Hitbox:HitStart()
		task.wait(config.AttackTime)
		canHit = true
		task.wait(config.Cooldown)
		Hitbox:HitStop()
		canHit = false

		swingIndex = (swingIndex % #swingAnimations) + 1

		debounce = false
	end
end)

Tool.Equipped:Connect(function()
	local Humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		Humanoid.Running:Connect(function(speed)
			OnWalking(Humanoid, speed)
		end)

		currentIdleAnim = PlayAnimation(Humanoid, config.IdleAnimationId)
	end
end)

Tool.Unequipped:Connect(function()
	local Humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid")
	if Humanoid then
		StopCurrentAnimation(currentWalkAnim)
		StopCurrentAnimation(currentIdleAnim)
	end
end)

All help is appreciated! Thanks.

2 Likes

Why all this complicated code and not just

local Animation = Humanoid:LoadAnimation(Animation)

Tool.Equipped:Connect(function()
	Humanoid.Running:Connect(function(speed)
		OnWalking(Humanoid, speed)
	end)

    Animation:Play()
end)

Tool.Unequipped:Connect(function()
	 Animation:Stop()
end)
4 Likes

Interesting, let me try that solution out.

1 Like

@sketchfan123321 is right, but if it doesn’t work you can always read the animation documentation

2 Likes

Alright I fixed it. For some reason @sketchfan123321 's idea didn’t work (it looks like it should though).

I just setup a connection for the Humanoid.Running event and just disconnected it when the player unequips the tool.

Thanks for all of the help everyone!

3 Likes

Nice! I’m glad you got it figured out!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.