Animation wont stop

Trying to stop an animation, but it just keeps saying stop is not a valid memember of animation.
This is a server sided script.

game.ReplicatedStorage.nin1.OnServerEvent:Connect(function(plr)
	local player = game.Players:FindFirstChild(plr.Name)
		local char = game.Workspace:FindFirstChild(plr.Name)
		if char then
			local humanoid = char:WaitForChild("Humanoid")
			local anim
if char~=nil and humanoid~=nil then
	if player.PlayIT.Value == false then
		local id="rbxassetid://"..tostring(0) --left as 0 for an example.
		local animation=Instance.new("Animation",char)
		animation.Name="AnimationForAnimations"
		animation.AnimationId=id
		anim=humanoid:LoadAnimation(animation)
		anim:Play()
		local snds = Instance.new("Sound",char.HumanoidRootPart)
		snds.SoundId = "rbxassetid://0"
		snds:Play()
		player.PlayIT.Value = true
			else
	if player.PlayIT.Value == true then
local oldanim=char:FindFirstChild("AnimationForAnimations")
local snd = char.HumanoidRootPart:FindFirstChild('Sound')
player.PlayIT.Value = false
snd:Destroy()
oldanim:Destroy()
	end
	local oldanim=char:FindFirstChild("AnimationForAnimations")
		if oldanim ~= nil then
			if oldanim then
				wait(.1)
			oldanim:Destroy()
			player.PlayIT.Value = false
local snd = char.HumanoidRootPart:FindFirstChild('Sound')
snd:Destroy()
player.PlayIT.Value = false
			end
			oldanim:Destroy()
			player.PlayIT.Value = false
local snd = char.HumanoidRootPart:FindFirstChild('Sound')
snd:Destroy()
		end
		end
		end
		end
end)

Would be appreciated if a solution could be found!

1 Like

I cleaned up the code so people can understand it. Where did you try to stop it?

game.ReplicatedStorage.nin1.OnServerEvent:Connect(function(plr)
	local player = game.Players:FindFirstChild(plr.Name)
	if player.Character then
		local char = player.Character
		local humanoid = char:WaitForChild("Humanoid")
		local anim
		if humanoid~=nil then
			if player.PlayIT.Value == false then
				local id="rbxassetid://"..tostring(0) --left as 0 for an example.
				local animation=Instance.new("Animation",char)
				animation.Name="AnimationForAnimations"
				animation.AnimationId=id
				anim=humanoid:LoadAnimation(animation)
				anim:Play()
				local snds = Instance.new("Sound",char.HumanoidRootPart)
				snds.SoundId = "rbxassetid://0"
				snds:Play()
				player.PlayIT.Value = true
			elseif player.PlayIT.Value == true then
				local oldanim=char:FindFirstChild("AnimationForAnimations")
				local snd = char.HumanoidRootPart:FindFirstChild('Sound')
				player.PlayIT.Value = false
				snd:Destroy()
				oldanim:Destroy()
			end
		end
		local oldanim=char:FindFirstChild("AnimationForAnimations")
		if oldanim ~= nil then
			if oldanim then
				wait(.1)
				oldanim:Destroy()
				player.PlayIT.Value = false
				local snd = char.HumanoidRootPart:FindFirstChild('Sound')
				snd:Destroy()
				player.PlayIT.Value = false
			end
			oldanim:Destroy()
			player.PlayIT.Value = false
			local snd = char.HumanoidRootPart:FindFirstChild('Sound')
			snd:Destroy()
		end
	end
end)

hehe ok

1 Like

You aren’t using :Stop() anywhere in your code. Mind sharing what line the error is occuring in? Also you don’t need to parent the animation to the char. Just leave it blank.

EDIT: That isn’t cool to steal my comment @GoldStorm950

1 Like

Sorry, all areas with oldanim:Destroy() are meant to be oldanim:Stop() , but I changed it to :Destroy() to see if it works, still doesn’t.

1 Like

I’m pretty sure you can only use :Stop() only on AnimationTracks.

If you look at the wiki you will see that the wiki uses it on the variable that is defined with LoadAnimation so try using the ‘anim’ variable instead of your ‘oldanim’ variable. You’re referencing the actual animation which doesn’t have the :Stop() function, instead you need to do it to the variable that has the :LoadAnimation().

Edit: wiki link is here: Using Animations | Documentation - Roblox Creator Hub

2 Likes
if player.PlayIT.Value == false then
			local id="rbxassetid://"..tostring(0) --left as 0 for an example.
			local animation=Instance.new("Animation",char)
			animation.Name="AnimationForAnimations"
			animation.AnimationId=id
			anim=humanoid:LoadAnimation(animation)
			anim:Play()

If you’re playing the animation using anim:Play(), you’d have to stop the animation with anim:Stop()

You have this value oldanim, which you’re trying to stop using :Destroy(), but you’ve never played the animation in the first place.

Unless I’m completely missing something, i’d recommend trying to stop the animation with anim:Stop().

1 Like
 game.ReplicatedStorage.nin1.OnServerEvent:Connect(function(plr)
	local player = game.Players:FindFirstChild(plr.Name)
	if player.Character then
		local char = player.Character
		local humanoid = char:WaitForChild("Humanoid")
		local anim
		if humanoid~=nil then
			if player.PlayIT.Value == false then
				local id="rbxassetid://"..tostring(0) --left as 0 for an example.
				local animation=Instance.new("Animation",char)
				animation.Name="AnimationForAnimations"
				animation.AnimationId=id
				anim=humanoid:LoadAnimation(animation)
				anim:Play()
				local snds = Instance.new("Sound",char.HumanoidRootPart)
				snds.SoundId = "rbxassetid://0"
				snds:Play()
				player.PlayIT.Value = true
			elseif player.PlayIT.Value == true then
				local oldanim=char:FindFirstChild("AnimationForAnimations")
				local snd = char.HumanoidRootPart:FindFirstChild('Sound')
				player.PlayIT.Value = false
				snd:Stop()
				snd:Destroy()
				anim:Stop()
				oldanim:Destroy()
			end
		end
		local oldanim=char:FindFirstChild("AnimationForAnimations")
		if oldanim ~= nil then
			if oldanim then
				wait(.1)
				anim:Stop()
				oldanim:Destroy()
				player.PlayIT.Value = false
				local snd = char.HumanoidRootPart:FindFirstChild('Sound')
				snd:Stop()
				snd:Destroy()
				player.PlayIT.Value = false
			end
			anim:Stop()
			oldanim:Destroy()
			player.PlayIT.Value = false
			local snd = char.HumanoidRootPart:FindFirstChild('Sound')
			snd:Stop()
			snd:Destroy()
		end
	end
end)

Here, see if this works. Before you destroy any animation, I stoped playing the animation. That way the animation stops playing and there is no animation stuck left in the player. I also stoped the sound before you destroyed the sound. Idk if that is necessary or not.

1 Like