Animations not playing fully or causing other animations to stop permanently

I have created a custom character, and I’m trying to create some custom animations with it. I got the idle and walking animation working so far, but when I added the dodge animation, it either didn’t play entirely or stopped playing animations entirely after it had fully played. I don’t think anything is wrong with the animations; I set the priority of the dodge to action, idle to idle, walk to movement, but still have problems. I tried adding a debounce to see if that would help but it still gives me the same results.

Code

I edited this code, the newer one is at the bottom

local ContextActionService = game:GetService("ContextActionService")

local Figure = script.Parent
local Torso = Figure:WaitForChild("Torso")
local Head = Torso:WaitForChild("Head")
local LeftArm = Torso:WaitForChild("LeftArm")
local RightArm = Torso:WaitForChild("RightArm")
local LeftLeg = Torso:WaitForChild("LeftLeg")
local RightLeg = Torso:WaitForChild("RightLeg")
local Humanoid = Figure:WaitForChild("Humanoid")

local currentAnim = ""
local currentAnimInstance = nil
local currentAnimTrack = nil
local currentAnimKeyframeHandler = nil
local currentAnimSpeed = 1.0
local animation = Instance.new("Animation")

local animNames = { 
	idle = "http://www.roblox.com/asset/?id=5390538505",
	preWalk = "http://www.roblox.com/asset/?id=5390475590",
	walk = "http://www.roblox.com/asset/?id=5390656131",
	dodge = "http://www.roblox.com/asset/?id=5390625526"
}

local waitForFinishAnimation = false
function playAnimation(name, transitionTime, humanoid)
	if name == "dodge" then
		waitForFinishAnimation = true
	end
	local anim = animNames[name]
	-- switch animation		
	if (anim ~= currentAnimInstance) then
		if (currentAnimTrack ~= nil) then
			currentAnimTrack:Stop(transitionTime)
			currentAnimTrack:Destroy()
		end
		animation.AnimationId = anim
		currentAnimTrack = humanoid:LoadAnimation(animation)

		currentAnimTrack:Play(transitionTime)
		currentAnimInstance = anim
		currentAnimTrack.Stopped:wait()
		waitForFinishAnimation = false
	end
end

local function onRunning(speed)
	if waitForFinishAnimation ~= true then
		if speed > 0.01 then
			playAnimation("walk", 0.2, Humanoid)
		else
			playAnimation("idle", 0.05, Humanoid)
		end
	end
end

local function onDodging(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		playAnimation("dodge", .1, Humanoid)
	end
end

Humanoid.Running:connect(onRunning)
ContextActionService:BindAction("dodging", onDodging, false, Enum.KeyCode.Space)

video on what happens when I press the space bar…
https://gyazo.com/8286940c210f60a8490658f407c1fd85

FYI, this also has the same results for when I’m idle, not just when I’m moving.


edit/update on the problem: after some testing, I was able to ensure that the animation played all the way through but it still has a long pause after finishing the dodge animation and if the space key is pressed while the animation is playing then it won’t play any more animations. Here’s the new code…

Code
local ContextActionService = game:GetService("ContextActionService")

local Figure = script.Parent
local Torso = Figure:WaitForChild("Torso")
local Head = Torso:WaitForChild("Head")
local LeftArm = Torso:WaitForChild("LeftArm")
local RightArm = Torso:WaitForChild("RightArm")
local LeftLeg = Torso:WaitForChild("LeftLeg")
local RightLeg = Torso:WaitForChild("RightLeg")
local Humanoid = Figure:WaitForChild("Humanoid")

local currentAnim = ""
local currentAnimInstance = nil
local currentAnimTrack = nil
local currentAnimKeyframeHandler = nil
local currentAnimSpeed = 1.0
local animation = Instance.new("Animation")

local animNames = { 
	idle = "http://www.roblox.com/asset/?id=5390538505",
	preWalk = "http://www.roblox.com/asset/?id=5390475590",
	walk = "http://www.roblox.com/asset/?id=5390656131",
	dodge = "http://www.roblox.com/asset/?id=5390625526"
}

local waitForFinishAnimation = false
function playAnimation(name, transitionTime, humanoid)
	local anim = animNames[name]
	-- switch animation		
	if (anim ~= currentAnimInstance) then
		if (currentAnimTrack ~= nil) then
			currentAnimTrack:Stop(transitionTime)
			currentAnimTrack:Destroy()
		end
		animation.AnimationId = anim
		currentAnimTrack = humanoid:LoadAnimation(animation)

		currentAnimTrack:Play(transitionTime)
		currentAnimInstance = anim
	end
end

local function onRunning(speed)
	print(waitForFinishAnimation)
	if waitForFinishAnimation ~= true then
		print("attempt walk or idle")
		if speed > 0.01 then
			playAnimation("walk", 0.2, Humanoid)
		else
			playAnimation("idle", 0.05, Humanoid)
		end
	end
end

local function onDodging(actionName, inputState)
	print("attempt to dodge")
	if inputState == Enum.UserInputState.Begin then
		waitForFinishAnimation = true
		playAnimation("dodge", .1, Humanoid)
		currentAnimTrack.Stopped:wait()
		waitForFinishAnimation = false
	end
end

Humanoid.Running:connect(onRunning)
ContextActionService:BindAction("dodging", onDodging, false, Enum.KeyCode.Space)
1 Like

I would guess it’s a priority problem from my experience… Not sure though.
Have you assigned priorities in the animator already?

Yes, if you read the post it says what I assigned everything to

Honestly, I’m so tired of this code that I don’t care about how it works anymore lol. Checking if the animation is playing in onRunning function doesn’t work, no idea why, but running onRunning after the animation played works, which just plays an animation depending on the speed of the character.

Code
local ContextActionService = game:GetService("ContextActionService")

local Figure = script.Parent
local Torso = Figure:WaitForChild("Torso")
local Head = Torso:WaitForChild("Head")
local LeftArm = Torso:WaitForChild("LeftArm")
local RightArm = Torso:WaitForChild("RightArm")
local LeftLeg = Torso:WaitForChild("LeftLeg")
local RightLeg = Torso:WaitForChild("RightLeg")
local Humanoid = Figure:WaitForChild("Humanoid")

local currentAnim = ""
local currentAnimInstance = nil
local currentAnimTrack = nil
local currentAnimKeyframeHandler = nil
local currentAnimSpeed = 1.0
local animation = Instance.new("Animation")
local movementSpeed

local animNames = { 
	idle = "http://www.roblox.com/asset/?id=5390538505",
	preWalk = "http://www.roblox.com/asset/?id=5390475590",
	walk = "http://www.roblox.com/asset/?id=5390656131",
	dodge = "http://www.roblox.com/asset/?id=5390625526"
}

local waitForFinishAnimation = false
function playAnimation(name, transitionTime, humanoid)
	local anim = animNames[name]
	-- switch animation		
	if (anim ~= currentAnimInstance) then
		if (currentAnimTrack ~= nil) then
			currentAnimTrack:Stop(transitionTime)
			currentAnimTrack:Destroy()
		end
		animation.AnimationId = anim
		currentAnimTrack = humanoid:LoadAnimation(animation)

		currentAnimTrack:Play(transitionTime)
		currentAnimInstance = anim
	end
end

local function onRunning(speed)
	movementSpeed = speed
	print(waitForFinishAnimation)
	if waitForFinishAnimation ~= true then
		print("attempt walk or idle")
		if speed > 0.01 then
			playAnimation("walk", 0.2, Humanoid)
		else
			playAnimation("idle", 0.05, Humanoid)
		end
	end
end

local function onDodging(actionName, inputState)
	print("attempt to dodge")
	if waitForFinishAnimation ~= true then
		if inputState == Enum.UserInputState.Begin then
			waitForFinishAnimation = true
			playAnimation("dodge", .1, Humanoid)
			currentAnimTrack.Stopped:wait()
			waitForFinishAnimation = false
		end
		onRunning(movementSpeed)
	end
end

Humanoid.Running:connect(onRunning)
ContextActionService:BindAction("dodging", onDodging, false, Enum.KeyCode.Space)
1 Like