Changing Animation Issue

Alrighty, yes! your script accomplished my first goal, which was to change the script the first time, now my main intention is to be able to change it multiple times possibly in my shop. Let me show you my whole animation changer script

local aksdfkjf = false
local Players = game:GetService("Players")

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	print("haha")
	for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end
 	
	local animateScript = character:WaitForChild("Animate")
	wait(1)
	local firstAnimation = animateScript:WaitForChild("run"):WaitForChild("RunAnim")
	local firstAnimationClone = firstAnimation:Clone()
	animateScript.run.RunAnim:Destroy()
	firstAnimationClone.AnimationId = "rbxassetid://616010382"        
	local secondAnimation = animateScript.walk.RunAnim
	local secondAnimationClone = secondAnimation:Clone()
	animateScript.walk.RunAnim:Destroy()
	secondAnimationClone.AnimationId = "rbxassetid://616013216"
	local thirdAnimation = animateScript.jump.JumpAnim
	local thirdAnimationClone = thirdAnimation:Clone()
	animateScript.jump.JumpAnim:Destroy()
	thirdAnimationClone.AnimationId = "rbxassetid://616008936"
	local fourthAnimation = animateScript.idle.Animation1
	local fourthAnimationClone = fourthAnimation:Clone()
	animateScript.idle.Animation1:Destroy()
	fourthAnimationClone.AnimationId = "rbxassetid://616006778" 
	local fifthAnimation = animateScript.idle.Animation2
	local fifthAnimationClone = fifthAnimation:Clone()
	animateScript.idle.Animation2:Destroy()
	fifthAnimationClone.AnimationId = "rbxassetid://616008087"
	local sixthAnimation = animateScript.fall.FallAnim
	local sixthAnimationClone = sixthAnimation:Clone()
	animateScript.fall.FallAnim:Destroy()
	sixthAnimationClone.AnimationId = "rbxassetid://616005863"
	local seventhAnimation = animateScript.swim.Swim
	local seventhAnimationClone = seventhAnimation:Clone()
	animateScript.swim.Swim:Destroy()
	seventhAnimationClone.AnimationId = "rbxassetid://616011509"
	local eighthAnimation = animateScript.swimidle.SwimIdle
	local eighthAnimationClone = eighthAnimation:Clone()
	animateScript.swimidle.SwimIdle:Destroy()
	eighthAnimationClone.AnimationId = "rbxassetid://616012453"
	local ninthAnimation = animateScript.climb.ClimbAnim
	local ninthAnimationClone = ninthAnimation:Clone()
	animateScript.climb.ClimbAnim:Destroy()
	ninthAnimationClone.AnimationId = "rbxassetid://616003713"
end

local function onCharacterAddedOther(character)
	local humanoid = character:WaitForChild("Humanoid")
	print("haha")
	for _, playingTracks in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTracks:Stop(0)
	end
 	
	local animateScript = character:WaitForChild("Animate")
	wait(1)
	local firstAnimation = animateScript:WaitForChild("run"):WaitForChild("RunAnim")
	local firstAnimationClone = firstAnimation:Clone()
	animateScript.run.RunAnim:Destroy()
	firstAnimationClone.AnimationId = "rbxassetid://750783738"        
	local secondAnimation = animateScript.walk.RunAnim
	local secondAnimationClone = secondAnimation:Clone()
	animateScript.walk.RunAnim:Destroy()
	secondAnimationClone.AnimationId = "rbxassetid://750785693"
	local thirdAnimation = animateScript.jump.JumpAnim
	local thirdAnimationClone = thirdAnimation:Clone()
	animateScript.jump.JumpAnim:Destroy()
	thirdAnimationClone.AnimationId = "rbxassetid://750782230"
	local fourthAnimation = animateScript.idle.Animation1
	local fourthAnimationClone = fourthAnimation:Clone()
	animateScript.idle.Animation1:Destroy()
	fourthAnimationClone.AnimationId = "rbxassetid://750781874" 
	local fifthAnimation = animateScript.idle.Animation2
	local fifthAnimationClone = fifthAnimation:Clone()
	animateScript.idle.Animation2:Destroy()
	fifthAnimationClone.AnimationId = "rbxassetid://750782770"
	local sixthAnimation = animateScript.fall.FallAnim
	local sixthAnimationClone = sixthAnimation:Clone()
	animateScript.fall.FallAnim:Destroy()
	sixthAnimationClone.AnimationId = "rbxassetid://750780242"
	local seventhAnimation = animateScript.swim.Swim
	local seventhAnimationClone = seventhAnimation:Clone()
	animateScript.swim.Swim:Destroy()
	seventhAnimationClone.AnimationId = "rbxassetid://750784579"
	local eighthAnimation = animateScript.swimidle.SwimIdle
	local eighthAnimationClone = eighthAnimation:Clone()
	animateScript.swimidle.SwimIdle:Destroy()
	eighthAnimationClone.AnimationId = "rbxassetid://750785176"
	local ninthAnimation = animateScript.climb.ClimbAnim
	local ninthAnimationClone = ninthAnimation:Clone()
	animateScript.climb.ClimbAnim:Destroy()
	ninthAnimationClone.AnimationId = "rbxassetid://750779899"
end

local function whichAnim(character)
	local player = game.Players:GetPlayerFromCharacter(character)
	if not player:FindFirstChild("PlayerAnim") then
		aksdfkjf = true
	end
	if player:WaitForChild('PlayerAnim').Value == "DefaultAnim" then
		print("You have the DefaultAnimation")
		player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
	end
	if player:WaitForChild('PlayerAnim').Value == "OtherAnim" then
		print("You have the OtherAnimation")
		player.CharacterAppearanceLoaded:Connect(onCharacterAddedOther)
	end
	wait(1)
	if aksdfkjf == true then
		player:LoadCharacter()
		aksdfkjf = false
	end	
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(whichAnim)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

This is a clip of my new issue, the DefaultAnim is what my character starts with, when I use the Proximity Prompt (my substitution for my shop until I get this whole terrible and funky issue fixed), it changes it to the OtherAnim, which is a pirate animation, but it won’t change as you can see.
robloxapp-20201215-1838488.wmv (4.6 MB)
(sorry I am unsure how to put a video on the forum from studio without it coming up as a download)
I am getting this error normally too, but it isn’t affecting the game…

   18:43:42.219  Infinite yield possible on 'Workspace.MRKYLO20.Animate.run:WaitForChild("RunAnim")'  -  Studio
  18:43:42.220  Stack Begin  -  Studio
  18:43:42.221  Script 'ServerScriptService.ChangeAnimationScript', Line 13 - function onCharacterAdded  -  Studio  -  ChangeAnimationScript:13
  18:43:42.222  Stack End  -  Studio

I would think that this would halt the whole thing, but it doesn’t, it may have nothing to do with the main problem, but I don’t know?

No that is one of the main issues. It appears as though the run animation doesn’t exist. At all. Which is weird.

I have a fix though. I implemented this about a month ago and it worked well, but now looking back I noticed I did something different than what I described to you in my earlier posts.

First thing: I have a folder in server storage that holds the default animations when I want to switch back to default. Notice that I keep everything that the Animate script would have.
image

You can grab these by pressing play in studio and going to your character model (It will have the animations you have on your avatar; if you unequip the ninja animation pack beforehand you will get the default ones).
image

Make sure you grab everything under the animate script. You don’t need the animate script itself. The image above is cropped since I can’t fit the entire list on my screen, but you get the idea I hope.

The second part is to select the animations that you need/that you are going to change. For me, all I needed to change were these (again, another folder under server storage):
image

(Keep the non-animation stuff too like the remote function and the scale dampening constant)

In your case, this would be your pirate animation. You would change the animation IDs for the animation objects in each one, but ahead of time rather than in the script. You will see why in a second.
image

Now the scripting part. It’s actually very simple, since you’ve pre-selected all the animations. We will use the behaviour of the animation script that auto-updates the animations, but instead we will remove everything under the animation script, rather than looking for something specific, and then replace it with all the new stuff.

(warning: I didn’t test this code so check for typos)

local DefaultAnimations = game:GetService("ServerStorage").Default
local PirateAnimations = game:GetService("ServerStorage").Pirate

local function onCharacterAdded(character)
    local humanoid = character:WaitForChild("Humanoid")
    local animateScript = character:WaitForChild("Animate")
    wait(1)
    for _, stuff in pairs(animateScript:GetChildren()) do
        stuff:Destroy()
    end
    for _, stuff in pairs(DefaultAnimations:GetChildren()) do
        local newStuff = stuff:Clone()
        newStuff.Parent = animateScript
    end
end

And you would do the same thing for your pirate animation. Hope this helps

4 Likes

HALLEUJAH!!! IT WORKS!!! Thanks so much!! And I am sorry it took forever, I am such a dummy. Thanks again!

1 Like