Animation doesn't stop

I tried to make a script so that the player has a tool that when equipped it plays a combat animation, and when clicked, a punch animation plays.
but the Click Animation doesn’t stop playing
I tried changing the script and looking at some solutions on the internet, but nothing works.

I’m not sure if the problem is the script or the animations, but the “Hold Animation” is in a loop and the animation priority for both is “Action” and both have only one frame

there is the script →

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local player = players.LocalPlayer

local tool = script.Parent
local remotes = replicatedStorage:WaitForChild("Remotes")

local punchHoldAnimation = nil
local punchClickAnimation = nil

local debounce = false
local equipped = false

tool.Equipped:Connect(function()
	local character = player.Character
	if character == nil then
		return
	end
	equipped = true
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid == nil then
		return
	end
	local animator = humanoid:FindFirstChild("Animator")
	if animator == nil then
		return
	end
	punchHoldAnimation = animator:LoadAnimation(tool.Animations.PunchHold)
	punchHoldAnimation:Play()
end)

tool.Unequipped:Connect(function()
	equipped = false
	if punchHoldAnimation == nil then
		return
	end
	punchHoldAnimation:Stop()
end)

tool.Activated:Connect(function()
	if debounce then
		return			
	end
	local character = player.Character
	if character == nil then
		return
	end
	if punchHoldAnimation ~= nil then
		punchHoldAnimation:Stop()
	end
	debounce = true
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid == nil then
		return
	end
	local animator = humanoid:FindFirstChild("Animator")
	if animator == nil then
		return
	end

	punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)
	punchClickAnimation:Play()
	remotes.Punch:FireServer()

	punchClickAnimation.Stopped:Connect(function()
		if punchHoldAnimation ~= nil and equipped then
			punchHoldAnimation:Play()			
		end
		debounce = false
	end)
end)
1 Like

You should probably load the animation when adding the variable(local). The value probably is still nil outside of Tool.Equippped because it only changed in Tool.Equipped function.

Let me know if this helped!

hey, make sure that you always add

punchClickAnimation.Looped = false

in any case if you don’t want it to loop :slight_smile:

1 Like

I referenced the Animation, but still not working.
thanks anyway!

1 Like

wait, another solution:

move this:

punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)

above to replace nil, that way it will only reference it once iic
it might not work tho, do Ctrl+Z if u want to undo

1 Like

like this?

local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local punchHoldAnimation = script.Parent.Animations.PunchHold
local punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)

(in this case it would be written below “local remotes”)

1 Like

okay, have you tested it? If so, did it work?

1 Like

yea like this too

1 Like

didnt worked, but thanks for helping!

oh, i’m having the same trouble to animate

This is probably how it should work

  • I have changed your code a little to make it easier to read and more readable (I hope you won’t be offended)

Script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local player = players.LocalPlayer
local tool = script.Parent
local remotes = replicatedStorage:WaitForChild("Remotes")

local punchHoldAnimation = nil
local punchClickAnimation = nil

local debounce = false
local equipped = false

tool.Equipped:Connect(function()
    local character = player.Character
    if not character then return end
    equipped = true

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    local animator = humanoid:FindFirstChild("Animator")
    if not animator then return end

    punchHoldAnimation = animator:LoadAnimation(tool.Animations.PunchHold)
    punchHoldAnimation.Looped = true
    punchHoldAnimation:Play()
end)

tool.Unequipped:Connect(function()
    equipped = false
    if punchHoldAnimation then
        punchHoldAnimation:Stop()
    end
end)

tool.Activated:Connect(function()
    if debounce then return end
    local character = player.Character
    if not character then return end

    debounce = true

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    local animator = humanoid:FindFirstChild("Animator")
    if not animator then return end

    if punchHoldAnimation then
        punchHoldAnimation:Stop()
    end

    punchClickAnimation = animator:LoadAnimation(tool.Animations.PunchClick)
    punchClickAnimation:Play()
    remotes.Punch:FireServer()

    task.wait(punchClickAnimation.Length)
    if punchHoldAnimation and equipped then
        punchHoldAnimation:Play()
    end

    debounce = false
end)

i could still read it either way lol, btw PING me if you are replying otherwise i’ll take hours or days

try changing the animation type

(e.g action, idle, core)

both are actions btw