Animations Delayed

I am having problems with my knife script. Basically, multiple animations are played and timed accordingly. For instance, the equip animation is 0.333 seconds long, so it waits that long, then plays the idle animation.

My problem is that after that 0.333 wait time, when it stops all the other knife animations and plays the idle animation, it has a tiny delay in which the characters arm are attempting to go to the default tool animation position. It makes the arms appear twitchy, which I DO NOT want.

Here is an example:

So far I tried extending the animations where they show the frame that follows them, but it sill delays because the previous animation is stopped before playing another.

Here is some of my code with the animations:

local AnimationTracks = {}
local CurrentAnim = {}

for _, Animation in pairs(Animations:GetChildren()) do
	if Animation:IsA("Animation") then
		AnimationTracks[Animation.Name] = Humanoid:LoadAnimation(Animation)
	elseif Animation:IsA("Folder") then
		local AnimationTable = {}
		
		for _, Anim in pairs(Animation:GetChildren()) do
			table.insert(AnimationTable,Humanoid:LoadAnimation(Anim))
		end
		
		CurrentAnim[Animation.Name] = {1,#AnimationTable}
		AnimationTracks[Animation.Name] = AnimationTable
	end
end

local function StopAllAnimations()
	for _, Animation in pairs(AnimationTracks) do
		if type(Animation) == "table" then
			for _, Anim in pairs(Animation) do
				Anim:Stop()
			end
		else
			Animation:Stop()
		end
	end
end

local function StopAnimation(Name)
	local Animation = AnimationTracks[Name]
	
	if Animation then
		if type(Animation) == "table" then
			for _, Anim in pairs(Animation) do
				Anim:Stop()
			end
		else
			Animation:Stop()
		end
	end
end

local function PlayAnimation(Name,Override)
	if Override then
		StopAllAnimations()
	end
	
	local Animation = AnimationTracks[Name]
	
	if type(Animation) == "table" then
		if #Animation > 1 then
			Animation[CurrentAnim[Name][1]]:Play()
			CurrentAnim[Name][1] = CurrentAnim[Name][1] + 1
			
			if CurrentAnim[Name][1] > CurrentAnim[Name][2] then
				CurrentAnim[Name][1] = 1
			end
		else
			Animation[1]:Play()
		end
	else
		Animation:Play()
	end
end

Here is where I play the animations:

Knife.Equipped:connect(function()
	Equipped = true
	Ready = false
	Holding = false
	PlaySound("Equip",Handle)
	PlayAnimation("Equip",true)
	
	coroutine.resume(coroutine.create(function()
		wait(0.333)
		
		if Equipped then
			PlayAnimation("Idle",true)
			Ready = true
		end
	end))
end)
Knife.Unequipped:connect(function()
	Equipped = false
	Ready = false
	Holding = false
	StopAllAnimations()
end)
local Start = tick()
local CurrentHold = 0

UserInputService.InputBegan:connect(function(Input,GameProcessed)
	if not GameProcessed then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
			if not Holding then
				if Equipped and Ready then
					Start = tick()
					Holding = true
					CurrentHold = CurrentHold + 1
					PlayAnimation("Draw",true)
					
					coroutine.resume(coroutine.create(function()
						local LastHold = CurrentHold
						
						wait(1)
						
						if Equipped and Ready and Holding and LastHold == CurrentHold then
							PlayAnimation("Hold",true)
						end
					end))
				end
			end
		end
	end
end)

UserInputService.InputEnded:connect(function(Input,GameProcessed)
	if not GameProcessed then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
			if Equipped and Ready and Holding then
				if Start and tick() - Start >= 0.2 then
					Holding = false
					Ready = false
					PlayAnimation("Throw",true)
					PlaySound("Throw",Handle)
					ThrowEvent:FireServer()
					ThrowFunction(Handle.Position,Mouse.Hit.p,math.min(1,tick() - Start))
					
					coroutine.resume(coroutine.create(function()
						wait(1)
						
						if Equipped then
							Ready = true
							PlayAnimation("Idle",true)
						end
					end))
				else
					Holding = false
					Ready = false
					PlayAnimation("Stab",true)
					PlaySound("Slash",Handle)
					StabEvent:FireServer()
					
					coroutine.resume(coroutine.create(function()
						wait(0.4)
						
						if Equipped then
							Ready = true
							PlayAnimation("Idle",true)
						end
					end))
				end
			end
		end
	end
end)

Please let me know if you have any ideas, suggestions, or solutions. Thanks! Have a good day.

  • Galactiq

I am aware that bumping isn’t allowed, but it has been five hours since I posted. I really need some help with my problem. :confused:

I have not faced such an issue yet so I am not pretty sure about it… but I think the reason for this is that even after you put wait(0.333), it still takes a little amount of time to stop the animation (maybe because the animation plays a few moments after your code to run it executes [a common problem with interpreters])

Well if that is the case then instead of using wait(0.333) try using

AnimationTrack.Stopped:wait()

where AnimationTrack is the current track you are playing (say for instance ‘Equip’ in you case).

I am not sure this would work but try giving it a shot…

Your coroutines may be suspended. I would suggest replacing:

coroutine.resume(coroutine.create(function()))

With a very simple way of using coroutines:

coroutine.wrap(function())()

coroutine.wrap will always run smoothly without pausing the thread.

1 Like