Having trouble with stopping an animation and then playing a new one on button click

Hello, I’m having trouble stopping an animation from playing inside a dummy, and then playing the one that is clicked.

Local Script:

Dance.MouseButton1Click:Connect(function()
	if game.ReplicatedStorage:WaitForChild("EventPlaying").Value == false then
		for i, v in pairs(workspace:WaitForChild("Player1"):GetChildren()) do
			local Animation = v.Animation.AnimationId
			local anim = v.Humanoid:LoadAnimation(Animation)
			anim:Stop()
			Animation = "rbxassetid://"..game.ReplicatedStorage:WaitForChild("Animations"):FindFirstChild("Dance").Value
			anim:Play()
		end
	end
end

I’m not familiar with animations so it may be off.

Keep in mind, the script was working fine until I added the anim:Stop()

Any and all help is appreciated!!!

Also, an error I’m getting now is: Unable to cast value to Object, which is this line: local anim = v.Humanoid:LoadAnimation(Animation).

1 Like

load animation requires the animation instance not the animation id

local anim = v.Humanoid:LoadAnimation(v.Animation)

1 Like

also you shouldnt load animation every function you should store it outside of it
else you will get an error once you exceeded 256 tracks

1 Like

Sorry for the really late reply. But the value of Animation isn’t changing for some reason.

Dance.MouseButton1Click:Connect(function()
	if game.ReplicatedStorage:WaitForChild("EventPlaying").Value == false then
		for i, v in pairs(workspace:WaitForChild("Player1"):GetChildren()) do
			local Animation = v.Animation.AnimationId
			local anim = v.Humanoid:LoadAnimation(v.Animation)
			anim:Stop()
			Animation = "rbxassetid://"..game.ReplicatedStorage:WaitForChild("Animations"):FindFirstChild("Dance").Value
			anim:Play()
		end
	end
end)

basically everything inside of the for loop needs to change, local Animation's value is AnimationId
you loaded the animation v.Animation to stop it
Animation = ... doesn’t actually change the value of variable anim, which means anim:Play() would still be playing v.Animation

and I assume game.ReplicatedStorage.EventPlaying.Value determines if the animation is playing or not? If so you’d need to set the value to true/false each click with a elseif-then statement

best I could explain

EventPlaying is a totally different aspect of the game but it is to prevent the animations from overlapping and breaking.

Anyways, how would I make the value of Animation change?

scrambled a quick one, see if that works

local loadedAnimation --non-nested variable

Dance.MouseButton1Click:Connect(function()
	if game.ReplicatedStorage:WaitForChild("EventPlaying").Value == false then
		for i, v in pairs(workspace:WaitForChild("Player1"):GetChildren()) do
			if loadedAnimation then
				loadedAnimation:Stop() --if there's a loaded animation playing, stop it
			end
			v.Animation.AnimationId = "rbxassetid://"..game.ReplicatedStorage:WaitForChild("Animations"):FindFirstChild("Dance").Value -- set the new animation ID to the animation object
			loadedAnimation = v.Humanoid:LoadAnimation(v.Animation) --load the new animation
			loadedAnimation:Play() --play
		end
	end
end)
1 Like

Works. Thank you so much, I really appreciate it!

1 Like