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)