Punch animations not playing?

Ha… hello again, Developers. I once again find myself on the brink of insanity coding a game that I definitely do not have enough skill for.

This time, I greet you all, ashamed, with a look into how horrible my punch animation code is, playing every time the user clicks:

if players.LocalPlayer.CharacterData.Moveset == "Mokou" then --never do this. there has to be a faster way.
				if count == 1 then --Funny. Who knew you could animate on the client and it would auto replicate to the server?
					local animation = Instance.new("Animation")
					animation.AnimationId = "rbxassetid://76593291004229"
					local track = animator:LoadAnimation(animation)
					track:Play()
					task.wait(animLength)
					
				elseif count == 2 then
					local animation = Instance.new("Animation")
					animation.AnimationId = "rbxassetid://90977379848094"
					local track = animator:LoadAnimation(animation)
					track:Play()
					task.wait(animLength)
					
				elseif count == 3 then
					local animation = Instance.new("Animation")
					animation.AnimationId = "rbxassetid://138453285755703"
					local track = animator:LoadAnimation(animation)
					track:Play()
					MokouFireHandler:FireServer(char["Right Arm"], "Punch3")
					task.wait(animLength)
					
				elseif count == 4 then
					print("Nothing here yet!")
				end
			elseif players.LocalPlayer.CharacterData.Moveset == "Sen/Sin" then
				if count == 1 then 
					print("Nothing here yet!")
				elseif count == 2 then
					print("Nothing here yet!")
				elseif count == 3 then
					print("Nothing here yet!")
				elseif count == 4 then
					print("Nothing here yet!")
				end
			elseif players.LocalPlayer.CharacterData.Moveset == "Sakuya" then
				if count == 1 then 
					print("Nothing here yet!")
				elseif count == 2 then
					print("Nothing here yet!")
				elseif count == 3 then
					print("Nothing here yet!")
				elseif count == 4 then
					print("Nothing here yet!")
				end
			end

Yes, yes, I know. It’s an abomination. But I’m not knowledgeable enough to make this better. I come to you all asking two things:

  1. Please tell me if there’s a way to optimize this, and if there is, how?
  2. Why aren’t the animations playing?
    Just so you know, animator is defined as the LocalPlayer.Character.Humanoid.Animator.
    I… kind of desperately need assistance. And my other scripters in this project, who are both better than me, haven’t been active in a while. So please, tell me how I can optimize this and how to make the animations work..

For one, please don’t create animations like this. You’re causing a memory leak by loading a new animation into the Animator; even if the animation instance gets GCed since it’s parented to nil and not referenced by your code after the AnimationTrack is loaded, you’re still adding up those AnimationTracks over time.

And, for two, I believe they aren’t playing because you’re attempting to load them while they’re parented to nil (lol). I usually prefer to keep animations in a folder in ReplicatedStorage; both because it’s accessible, and you can do something like this:

-- This is just a table of animations; you'd do this for all movesets, essentially
local MOVESET_ANIMATIONS: { [string]: { Animation } } = table.freeze{
    -- "Animations" would just be some folder in ReplicatedStorage like I said earlier
    Mokou = { Animations.Mokou.Punch1, ... }
}

So, when you want to load them into the animator:

-- Then, you'd load the animations, if possible
local moveset: { Animation }? = MOVESET_ANIMATIONS[CharacterData.Moveset]
if not moveset then
    return
end
-- Create the table to hold your punch animations
local punchAnimations: { AnimationTrack } = {}
for index: number, animation: Animation in moveset do
    punchAnimations[index] = animator:LoadAnimation(animation)
end

And, finally, when you want to play them…

punchAnimations[count]:Play()

Much cleaner, and you won’t be compared to a certain someone who really, really likes their elseif statements…

1 Like

Oh my god; thank you so much. You’re a lifesaver.

I had considered using tables, but wasn’t sure how to properly use them in this context, so i decided to go with the easier but definitely worse route.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.