IsPlaying not working properly

Hello. So im making a movement script. And basically if the Unit found the target it will play their Moving animations. Its stored inside Module Script

So the issue here is the animations play more than once and the IsPlaying seems like its not working properly

It also give me a warning
:arrow_forward: AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played. (x3048)

function Unit.SearchTarget(unit)
	if unit then
		local unitstats = require(unit:WaitForChild("Stats"))
		local unitHum = unit:WaitForChild("Humanoid")
		local UnitAnimator = unitHum:WaitForChild("Animator")
		local UnitAnims = unit:WaitForChild("Animations")
		local UnitIdle = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Idle"))
        local UnitWalk = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Move"))
	
		
		local agrodistance = math.huge
		local target = nil
		for i, v in pairs(game.Workspace.Enemies:GetChildren()) do
			local human = v:FindFirstChild("Humanoid")
			local torso = v:FindFirstChild("Torso")
			if human and torso and v ~= script.Parent then

				if (unit.Torso.Position - torso.Position).magnitude < agrodistance then
					agrodistance = (unit.Torso.Position - torso.Position).magnitude
					target = torso
					
					if target then
						
						unit:WaitForChild("Humanoid"):MoveTo(target.Position)
						
						if not UnitWalk.IsPlaying then
							UnitWalk:Play()
						end
						
					end
					
				end
			end
		end
		
		return target
	end
	
	task.wait()
end


--- Triggering function

function Unit.Attack(unit,player)
	local target = Unit.SearchTarget(unit)
1 Like

Few things you could try.

  • Condition to check if the animation is already playing before playing it again. This should prevent the animations from playing multiple times unnecessarily.

  • Condition to stop the animation if no target is found. This ensures that the animation stops when there is no target, preventing it from playing indefinitely.

  • Remove unnecessary code related to playing animations outside of the loop where the target is searched.

function Unit.SearchTarget(unit)
	if unit then
		local unitstats = require(unit:WaitForChild("Stats"))
		local unitHum = unit:WaitForChild("Humanoid")
		local UnitAnimator = unitHum:WaitForChild("Animator")
		local UnitAnims = unit:WaitForChild("Animations")
		local UnitIdle = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Idle"))
        local UnitWalk = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Move"))

You’re loading the animation everytime the bot searches for target, you should keep loading animations out of loops:

local UnitIdle = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Idle"))
local UnitWalk = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Move"))

function Unit.SearchTarget(unit)
	if unit then
		UnitWalk:Play()
    end
end

but its a module script though, so that wouldnt get the unithum animator and animations

This is only a rough example, you’d have to make a class for the unit, or save it to a table

what if i create another function like function.UnitAnim(unit,anim), would that work?

It’d do the same thing if you load it there, the easiest way would probably be to make a table

local Units = {}

function Unit.SearchTarget(unit)
	if Units[unit] then
		Units[unit]["UnitWalk"]:Play()
    else
        Units[unit] = {}
        Units[unit]["UnitWalk"] = UnitAnimator:LoadAnimation(UnitAnims:WaitForChild("Move"))
        Units[unit]["UnitWalk"]:Play()
    end
end

eh well i think they sounds more complicated, i’ll find another way but thanks for your help

1 Like

Did you ever find a solution?

I also made a custom movement script / animations. They are all cached in a table and only loaded once, yet when I spam jump / keys / etc. it eventually starts giving that warning and counting up to infinity until I end the test. I don’t have a clue where it’s coming from, I’ve checked :GetPlayingAnimationTracks() on every Animator I have and printed the table cache and none of them are the culprit.

Do you think it could somehow have to do with “Stacking” animations via :Play() or maybe with how .Jump = true works?

I’m almost certain this is some sort of engine bug, I’ve isolated every imaginable variable I can.

EDIT: I just added a print before every possible LoadAnimation() in my game.
Only 30 prints TOTAL, yet I still end up with this warning somehow. Definitely a bug of some sort.

yea i did but like its still messed up as i aint prioriting the anims at this point but other stuff