Can't change default animation during game

I’ve been trying for weeks now to change the walking/idle animation, but nothing i do will work. i already tried trying to change the animate script, changing the AnimationId of RunAnim and Idle, and i even tried deleting the animations from the animate script and replacing them with the desired animations, but the game keeps playing the animations that don’t even exist anymore somehow. i don’t know if this is a glitch or something, but if anyone has any idea on how i can change the default animations with code during game runtime, it would really help me =)
The code i’m currently using:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local laserOff = ReplicatedStorage:WaitForChild("LaserOff")
local laserOn = ReplicatedStorage:WaitForChild("LaserOn")

local laserOffAnims = game:GetService("ServerStorage").Animations.LaserOff
local laserOnAnims = game:GetService("ServerStorage").Animations.LaserOn

laserOff.OnServerEvent:Connect(function(player)
	print "LaserOff fired"
	local char = player.Character or player.CharacterAdded:wait()
	local humanoid = char:WaitForChild("Humanoid")
	local animate = char:WaitForChild("Animate")
	if animate then
		print "animate"
	end
	for _, stuff in pairs(animate:GetChildren()) do
		print("a")
		stuff:Destroy()
	end
	for _, stuff in pairs(laserOffAnims:GetChildren()) do
		local newStuff = stuff:Clone()
		newStuff.Parent = animate
	end
end)

laserOn.OnServerEvent:Connect(function(player)
	print "LaserOn fired"
	local char = player.Character or player.CharacterAdded:wait()
	local humanoid = char:WaitForChild("Humanoid")
	local animate = char:WaitForChild("Animate")
	if animate then
		print "animate"
	end
	for _, stuff in pairs(animate:GetChildren()) do
		print("a")
		stuff:Destroy()
	end
	for _, stuff in pairs(laserOnAnims:GetChildren()) do
		local newStuff = stuff:Clone()
		newStuff.Parent = animate
	end
end)

btw I’ve tried debugging and the code works perfectly fine, the problem is just that even after the animations are deleted from the game, they still play.

1 Like

You actually have to retrieve the active AnimationTrack objects that are playing on the Animator component of the humanoid. Calling the stop method on those objects will stop them from playing.

for _, animationTrack in char.Humanoid.Animator:GetPlayingAnimationTracks() do
    animationTrack:Stop()
end
1 Like

didn’t work. idk if its the way i applied the code or something. i put this for loop in the end of both functions

1 Like

Run it locally. If the animator script was already destroyed this should stop any active animations

2 Likes

it stops the animation apparently, but when i stop walking and start to walk again it just plays the same default animation again. btw i’m deleting the animations in the animate script, not the entire script. ill try to just replace the script during the game to one with the correct ID’s and see if it works

1 Like

Hey, I’m here for 2 reasons.
First off, tysm @C_Sharper, you genuinely made my day. I had been struggling with this for over a week, after I realized how I could pull this off and I was actually able to.

Second of all, Even though im a bit of an ametuer, I recommend reverting back to changing the default animation in the “Animate” script. The method I used to pull this off is totally not hacky kinda hacky, but not to the point that its terrible. What I did was change the AnimationId through the server, then stop all current animations with the client, then created a “cover-up” animation if you will. First check which animation was last playing, heres how I did this, (And I stopped the animation right after)

local state
	for _, animationTrack in Character.Humanoid.Animator:GetPlayingAnimationTracks() do
		state = animationTrack.Name
		animationTrack:Stop()
	end

It will give the value “state” a string value, and it directly correlates to those in the Animate script (credited you havent edited it, but it should still work fine in that scenario)
Then I actually make the new animation, set the new animations AnimationId to the id of the “state” value I mentioned earlier, Load it using humanoid:LoadAnimation(), loop it, and then play it.

Afterwards, and this step is kind of important, theres a event for the Animator object called “AnimationPlayed” , so I check for that and then when a new animation IS played, I make sure its not the same animation we jsut made, and if its not stop the track and destroy the animation. This has seemed to work for me.

Here the code for those previous 2 paragraphs, note it was made specifically for the idle animation.

if state == "Animation1" then
		local newAnim = Instance.new("Animation")
		newAnim.AnimationId = AnimScript.idle.Animation1.AnimationId --- Did this simply because of how often i have to change the id, lol
		local newTrack = Character.Humanoid:LoadAnimation(newAnim)
		newTrack:Play()
		newTrack.Looped=true
		local Animator = Character.Humanoid.Animator
		Animator.AnimationPlayed:Connect(function(animation)
			if animation ~= newAnim then
				newTrack:Stop()
				newAnim:Destroy()
			end
		end)
	end

Hope this helped, and thanks again, seriously.

3 Likes

i tried this but couldn’t get it to work with the walk/run animations, so i tried just changing the animate script during the game and it worked! i got a few issues still related to animation priority and stuff but the script works. thanks for the help! =)

1 Like

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