So, I have a horse system I’m making and I’m trying to get the animations to work. I’ve seen a lot of threads on here about similar issues but nothing has given me a solution yet.
Basically, different animations play depending on the speed of the horse’s movement. All of my animations are pre-loaded onto tracks into tables at the start of the script, for easy access later so I don’t need to load them every single time.
However, it appears that no matter how I structure the code, the animations sometimes just don’t stop playing. And then, when I run Animator:GetPlayingAnimationTracks
it sometimes returns empty, even though there is very clearly an animation playing, and other times it has animations in there that weren’t stopped for some reason. The issue is with looped animations specifically, not others.
Video: Watch 2024-09-02 23-58-41 | Streamable (p.s. ignore the sanitized ID errors, they aren’t relevant)
Relevant Code Here
local currentAnim
HorseConfig:GetAttributeChangedSignal("STATE"):Connect(function()
currentAnim:Stop() -- Trying to stop the currently playing animation
for i,v in pairs(Animator:GetPlayingAnimationTracks()) do
v:Stop() -- Once again trying to stop ANY playing animations
end
warn(Animator:GetPlayingAnimationTracks()) -- Sometimes this shows as {} or sometimes it shows a playing animation, even though I've already tried to stop it twice...
if HorseConfig:GetAttribute("STATE") == "idle" then
while HorseConfig:GetAttribute("STATE") == "idle" do
currentAnim:Stop()
currentAnim = StandAnimations[math.random(1, #StandAnimations)]
currentAnim:Play()
currentAnim.Ended:Wait()
wait(math.random(0, 15))
end
elseif HorseConfig:GetAttribute("STATE") == "trot" then
for i, v in ipairs(OtherAnimations) do
if v:GetAttribute("name") == "horse_trot" then
currentAnim = v
break
end
end
currentAnim:Play()
elseif HorseConfig:GetAttribute("STATE") == "gallop" then
for i, v in ipairs(OtherAnimations) do
if v:GetAttribute("name") == "horse_gallop" then
currentAnim = v
break
end
end
currentAnim:Play()
end
end)
Thoughts? I have tried so many ways to fix this and I have no idea what’s going on. Sometimes it works, sometimes it doesn’t. ROBLOX animations are the bane of my existence right now.
Please take note that I am not sure on how to solve this completely but I’ll give you my thoughts
- When you’re idle, you are yielding the script with that
animation.Ended:Wait()
, and another wait(0,15)
. So when you move the horse I don’t think the script is recording that movement since it’s waiting, which means that it still thinks the animation is playing
- You are moving the horse very frequently (maybe), so if the state changes to trot and then immediately changes to gallop, the animations may clash and cause these anomalies
Your first point could potentially be it, if the loop just gets stuck for whatever reason. Though, since this entire section fires whenever the attribute changes, I don’t know if that will solve everything, but it’ll be something I check out when I get back on PC tomorrow and see if removing that subsection fixes the issue.
As for the second one, (and I suppose this bleeds over to the first as well) shouldn’t the loop stopping all running animations still technically deal with this if there’s already an animation running? Theoretically the animations should clear out, but they don’t. I also don’t think the horse speeds up fast enough for it to cause the function to overlap itself, but I could be wrong and I’ll check it out tomorrow as well.
(referring to this:)
for i,v in pairs(Animator:GetPlayingAnimationTracks()) do
v:Stop()
end
Good food for thought either way.
Also, just had another thought that perhaps the idle state isn’t firing for some reason, but I think there’s already checks in place to make sure it fires under certain conditions. Another thing I’ll look into tomorrow.
Yeah, unfortunately none of the above things helped. Still have issues with animations continuing to play even if :GetPlayingAnimationTracks returns empty.
Idle state is definitely firing. Removing the idle state loop code doesn’t work, either, and slowing down the horse so that states don’t change too quickly doesn’t help either - I can be going as slow as molasses and it’ll still have these issues.
For anyone else who may be looking at this post, I’ve updated my code a little bit just for testing purposes.
HorseConfig:GetAttributeChangedSignal("STATE"):Connect(function()
local state = HorseConfig:GetAttribute("STATE")
warn(state)
for _, anim in ipairs(Animator:GetPlayingAnimationTracks()) do
anim:Stop(0.25)
end
if state == "trot" then
for i,v in pairs(OtherAnimations) do
if v.Name == "horse_trot" then
if not v.IsPlaying then
v:Play()
end
HorseConfig:GetAttributeChangedSignal("STATE"):Once(function()
if HorseConfig:GetAttribute("STATE") ~= "trot" then
v:Stop(0.25)
v.Ended:Once(function()
warn("Stopped!")
end)
end
end)
break
end
end
end
end)
Essentially now, all that fires is the trot animation. Despite having set up basically two layers of code to try to stop the animation, it still sometimes doesn’t stop. The animation only seems to stop when I very quickly switch between idle and trot states, and then it’ll end the animation.
I added the fadeTime
just as a test to see if for some reason it was ending too quickly or something, as there was something I read about the default fadeTime causing issues. I don’t think it changed anything.
To be clear, this didn’t fix anything. It just changed the behavior very slightly.