[PLS HELP]AnimationTrack.IsPlaying not working

Hello , i’ve been getting troubles when i try to check if an animation is playing or not within a server scripts it always returns false even if the animation is playing

here’s the script :

local replicatedStorage = game:GetService("ReplicatedStorage")
local M1SwordEvent = replicatedStorage.AbilityEvents.SwordEvents:WaitForChild("M1SwordEvent")

local Debris = game:GetService("Debris")

M1SwordEvent.OnServerEvent:Connect(function(player,hitTarget)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid") 

	local damageMultiplier = player:WaitForChild("valueFolder"):WaitForChild("DamageMultiplier")
	local isAttacking = player:WaitForChild("valueFolder"):WaitForChild("isAttacking")
	
	for i , targetPrimaryPart in pairs(hitTarget) do -- Loop through all targets
		local characterTarget = targetPrimaryPart.Parent
		local playerTarget = game.Players:GetPlayerFromCharacter(characterTarget) 
		local humanoidTarget = characterTarget:WaitForChild("Humanoid")
		local humanoidRootPartTarget = characterTarget:WaitForChild("HumanoidRootPart")

		local function effect(effect,emitCount) -- Just Some Effects
			local hitParticle = effect
			hitParticle.Parent = targetPrimaryPart.Parent.Torso

			hitParticle:Emit(emitCount)

			task.delay(.5,function()
				hitParticle:Destroy()
			end)
		end

		if playerTarget and playerTarget:WaitForChild("valueFolder"):WaitForChild("isBlocking").Value == true then -- if he blocks
			character:FindFirstChild("Sword"):FindFirstChild("BlockSound"):Play()
			
			effect(game.ReplicatedStorage.Fx.BlockEffect:Clone(),30)
		elseif humanoidTarget.Health > 0 then
			if humanoid:FindFirstChild("Tag") then
				if humanoid:FindFirstChild("Tag").Value ~= character then
					local tag = Instance.new("ObjectValue",humanoidTarget)
					tag.Name = "Tag"
					tag.Value = character

					Debris:AddItem(tag,2)
				end
			else
				local tag = Instance.new("ObjectValue",humanoidTarget)
				tag.Name = "Tag"
				tag.Value = character

				Debris:AddItem(tag,2)
			end

			characterTarget.Humanoid:TakeDamage(10 * damageMultiplier.Value)

			character:FindFirstChild("Sword"):FindFirstChild("Slash"):Play() -- This is a sound
			
			print(humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim")).IsPlaying)
			
			if humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim")).IsPlaying == false then -- The error is here
				humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim")):Play()
			end
			
			effect(replicatedStorage.Fx.HitParticle:Clone(),30)	
		end
	end
end)
1 Like

You can use Animator:GetPlayingAnimationTracks() and search for your animation as an alternative.

1 Like

I tried using that but what i want to achieve is to see if the animation is played then we won’t play it again but if it isn’t we start it and the problem with :GetPlayingAnimationTracks() is that it irriates one by one meaning that if one of the animation playing is not the hitanimation and we play it and after that maybe the other one is the hitanimation thus playing it twice.

The script will work even if the if statement isn’t present…

1 Like

I know but i don’t the hitanimation play multiple times because i think it’s kinda bad

In order for it to return true, there has to be another script / localscript which plays the HitAnim animation before the M1SwordEvent gets fired.

1 Like

So i create anoter remoteEvent only to play this animation ?

You are loading in a new animation and checking if it’s playing. It’s always going to return false because the animation you just loaded was never played. You have to search for the animation or store a variable for if the animation is playing or not.

1 Like

No, not really. I dont really understand the logic of your script that you’ve used here. The output seems valid to me since the animation is never played before the print statement and only after the printing. Is there any other localscript / script which plays that animation or stops it apart?

1 Like

Yes, I am saying the same thing.

1 Like

Also thanks to you guys you’ve helped me so much @Xerto and @snowingwings220 (I honestly don’t know wich message to check as a solution)

Here’s now the script if someone wants it :

local replicatedStorage = game:GetService("ReplicatedStorage")
local M1SwordEvent = replicatedStorage.AbilityEvents.SwordEvents:WaitForChild("M1SwordEvent")

local Debris = game:GetService("Debris")

local hitAnim

M1SwordEvent.OnServerEvent:Connect(function(player,hitTarget,Time)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid") 

	local damageMultiplier = player:WaitForChild("valueFolder"):WaitForChild("DamageMultiplier")
	local isAttacking = player:WaitForChild("valueFolder"):WaitForChild("isAttacking")
	
	isAttacking.Value = true
	
	task.delay(Time,function()
		isAttacking.Value = false
	end)
	
	for i , targetPrimaryPart in pairs(hitTarget) do -- Loop through all targets
		local characterTarget = targetPrimaryPart.Parent
		local playerTarget = game.Players:GetPlayerFromCharacter(characterTarget) 
		local humanoidTarget = characterTarget:WaitForChild("Humanoid")
		local humanoidRootPartTarget = characterTarget:WaitForChild("HumanoidRootPart")
		
		
		local function effect(effect,emitCount) -- Just Some Effects
			local hitParticle = effect
			hitParticle.Parent = targetPrimaryPart.Parent.Torso

			hitParticle:Emit(emitCount)

			task.delay(.5,function()
				hitParticle:Destroy()
			end)
		end

		if playerTarget and playerTarget:WaitForChild("valueFolder"):WaitForChild("isBlocking").Value == true then -- if he blocks
			character:FindFirstChild("Sword"):FindFirstChild("BlockSound"):Play()
			
			effect(game.ReplicatedStorage.Fx.BlockEffect:Clone(),30)
		elseif humanoidTarget.Health > 0 then
			if humanoid:FindFirstChild("Tag") then
				if humanoid:FindFirstChild("Tag").Value ~= character then
					local tag = Instance.new("ObjectValue",humanoidTarget)
					tag.Name = "Tag"
					tag.Value = character

					Debris:AddItem(tag,2)
				end
			else
				local tag = Instance.new("ObjectValue",humanoidTarget)
				tag.Name = "Tag"
				tag.Value = character

				Debris:AddItem(tag,2)
			end
			
			print(humanoidTarget:GetPlayingAnimationTracks())
			if #humanoidTarget:GetPlayingAnimationTracks() > 0  then
				for i , v in pairs(humanoidTarget:GetPlayingAnimationTracks()) do
					if v.Name == "HitAnim1" then
						hitAnim = v
					else
						hitAnim = humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim1"))
					end
				end
			else
				hitAnim = humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim1"))
			end
			
			print(hitAnim)
			characterTarget.Humanoid:TakeDamage(10 * damageMultiplier.Value)

			character:FindFirstChild("Sword"):FindFirstChild("Slash"):Play() -- This is a sound
			
			if hitAnim.IsPlaying == false then
				hitAnim:Play()
			end
			
			if playerTarget then
				local isStunnedTarget = playerTarget:WaitForChild("valueFolder"):WaitForChild("isStunned")

				isStunnedTarget.Value = true

				hitAnim.Stopped:Connect(function()
					isStunnedTarget.Value = false
				end)
			end
			
			effect(replicatedStorage.Fx.HitParticle:Clone(),30)	
		end
	end
end)
1 Like

You should play the animations on the client, not the server, but if you insist, this will not work for more than one player, because you are only storing one variable.

-- outside of function
local AnimationCache = {}
		
-- inside of function	
if not AnimationCache[player] then
    AnimationCache[player] = humanoidTarget:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("HitAnim"))
end

if not AnimationCache[player].IsPlaying then
    AnimationCache[player]:Play()
end

-- another function
local Players = game:GetService("Players")

local function listenForDeath(player: Player)
    player.CharacterAdded:Connect(function(character: Instance)
        AnimationCache[player] = nil
    end)
end

Players.PlayerAdded:Connect(listenForDeath)

for _, player in Players:GetPlayers() do
    listenForDeath(player)
end

Players.PlayerRemoving:Connect(function(player: Player)
    AnimationCache[player] = nil
end)
1 Like

I’m not sure if i understood the code correctly but i am looping through every target hit by a hitbox and just tested it right now and it works perfectly fine with two players

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