Why wont this animation stop?

So I have a animation that wont stop playing for some reason and I do not know how to stop it.

Here’s My Code:

		----------------------------------------------------------------------------------
	anim.Parent = character
	anim2.Parent = character
	local animation = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim)
	local animation2 = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim2)
	----------------------------------------------------------------------------------
				local animation = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim)
			
			character["Right Arm"].Weld:Destroy()
			cocaColaNew.Anchored = false
			cocaColaNew.CanCollide = true
			cocaColaNew.Name = player.Name.. "'s Dropped Can"
			cocaColaNew.Parent = game.Workspace.Cans
			animation.Looped = false 
			animation:Stop()
			IsBeingHeld = false

hers a video of the problem:

1 Like

Is this the whole script? Where are you playing the animation and what animation are you playing? You also loaded 2 of the same animations with the same variable name local animation.

1 Like

Yes I know this isnt the whole script but I thought if i give the whole script itll be confusing but here is the entire thing.

1 Like
local event = game.ReplicatedStorage.RemoteEvents.Drink
local d = false
local IsBeingHeld = false
local cooldown = 4.432

----------------------------------------------------------------------------------
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6928507384"
anim.Name = "anim1"

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://6928670679"
anim2.Name = "anim2"
----------------------------------------------------------------------------------

event.OnServerEvent:Connect(function(player)
local character = player.Character
if not IsBeingHeld then
	IsBeingHeld = true
	--Equip Can
	----------------------------------------------------------------------------------
	anim.Parent = character
	anim2.Parent = character
	local animation = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim)
	local animation2 = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim2)
	----------------------------------------------------------------------------------
	local character = player.Character
	local cocaColaNew = game.ReplicatedStorage.CocaColaCan:Clone()
	cocaColaNew.CFrame = (character:WaitForChild("Right Arm").CFrame + 
character:WaitForChild("Right Arm").CFrame.UpVector * -1) * CFrame.Angles(math.rad(-90), math.rad(180), 
0)
	cocaColaNew.Parent = workspace.ActiveCans
	cocaColaNew.Transparency = 1
	cocaColaNew.ColaCan.Value = cocaColaNew
	cocaColaNew.Name = player.Name.. "'s CocaCola"
	
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = cocaColaNew
	weld.Part1 = character:WaitForChild("Right Arm")
	weld.Parent = character:WaitForChild("Right Arm")
	weld.Name = "Weld"

	cocaColaNew.Anchored = false
	
	cocaColaNew.openDrink:Play()
	animation2:Play()
	wait(.51)
	cocaColaNew.Transparency = 0
	animation2.Stopped:Wait()
	animation:Play()
	
	
else
	local cocaColaNew = game.Workspace.ActiveCans:WaitForChild(player.Name.. "'s CocaCola")
	if not cocaColaNew or cocaColaNew.drinksLeft.Value <= 1 then
		if not d then
			d = true
			local animation = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim)
			
			character["Right Arm"].Weld:Destroy()
			cocaColaNew.Anchored = false
			cocaColaNew.CanCollide = true
			cocaColaNew.Name = player.Name.. "'s Dropped Can"
			cocaColaNew.Parent = game.Workspace.Cans
			animation.Looped = false 
			animation:Stop()
			IsBeingHeld = false
			
			d = false
		end
	else
		if not d then
			d = true
			
			print("slurp..")
			cocaColaNew.drinksLeft.Value = cocaColaNew.drinksLeft.Value - 1
			cocaColaNew.drink:Play()
			wait()
			
			d= false
		end
	end
end
end)
1 Like

You’re not referencing the right animation as its not in the same scope. You really should only be loading the animations once and referencing those specific ones. What you’re doing currently is loading the same animation and stopping it thinking that it will stop the original one.

Honestly, the easiest solution for you would be to reference the animations outside the if statement:

event.OnServerEvent:Connect(function(player)
local character = player.Character
local animation = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim)
local animation2 = character.Humanoid:WaitForChild("Animator"):LoadAnimation(anim2)
if not IsBeingHeld then
	IsBeingHeld = true
	--Equip Can
	----------------------------------------------------------------------------------
	-- Whatever else you had here
	

Now all you would do is remove the below:

Note that you also don’t have to parent the animations to the character. You can just load them and play them. So you can remove the “anim.Parent = character” parts.

1 Like

Ok so now im doing this with my animations

----------------------------------------------------------------------------------

local anim = Instance.new("Animation")

anim.AnimationId = "rbxassetid://6928507384"

anim.Name = "anim1"

local anim2 = Instance.new("Animation")

anim2.AnimationId = "rbxassetid://6928670679"

anim2.Name = "anim2"

----------------------------------------------------------------------------------

event.OnServerEvent:Connect(function(player)

local character = player.Character

local animation = character.Humanoid.Animator:LoadAnimation(anim)

local animation2 = character.Humanoid.Animator:LoadAnimation(anim2)

but they still wont stop

1 Like

Send the full script with all edits.

1 Like
local event = game.ReplicatedStorage.RemoteEvents.Drink
local d = false
local IsBeingHeld = false
local cooldown = 4.432

----------------------------------------------------------------------------------
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6928507384"
anim.Name = "anim1"

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://6928670679"
anim2.Name = "anim2"
----------------------------------------------------------------------------------

event.OnServerEvent:Connect(function(player)
local character = player.Character
local animation = character.Humanoid.Animator:LoadAnimation(anim)
local animation2 = character.Humanoid.Animator:LoadAnimation(anim2)
if not IsBeingHeld then
	IsBeingHeld = true
	--Equip Can

	local character = player.Character
	local cocaColaNew = game.ReplicatedStorage.CocaColaCan:Clone()
	cocaColaNew.CFrame = (character:WaitForChild("Right Arm").CFrame +     
character:WaitForChild("Right Arm").CFrame.UpVector * -1) * CFrame.Angles(math.rad(-90), math.rad(180),         
0)
	cocaColaNew.Parent = workspace.ActiveCans
	cocaColaNew.Transparency = 1
	cocaColaNew.ColaCan.Value = cocaColaNew
	cocaColaNew.Name = player.Name.. "'s CocaCola"
	
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = cocaColaNew
	weld.Part1 = character:WaitForChild("Right Arm")
	weld.Parent = character:WaitForChild("Right Arm")
	weld.Name = "Weld"

	cocaColaNew.Anchored = false
	
	cocaColaNew.openDrink:Play()
	animation2:Play()
	wait(.51)
	cocaColaNew.Transparency = 0
	animation2.Stopped:Wait()
	animation:Play()
	
	
else
	local cocaColaNew = game.Workspace.ActiveCans:WaitForChild(player.Name.. "'s CocaCola")
	if not cocaColaNew or cocaColaNew.drinksLeft.Value <= 1 then
		if not d then
			d = true
			
			character["Right Arm"].Weld:Destroy()
			cocaColaNew.Anchored = false
			cocaColaNew.CanCollide = true
			cocaColaNew.Name = player.Name.. "'s Dropped Can"
			cocaColaNew.Parent = game.Workspace.Cans
			animation.Looped = false 
			animation:Stop()
			IsBeingHeld = false
			
			d = false
		end
	else
		if not d then
			d = true
			
			print("slurp..")
			cocaColaNew.drinksLeft.Value = cocaColaNew.drinksLeft.Value - 1
			cocaColaNew.drink:Play()
			wait()
			
			d= false
		end
	end
end
end)
1 Like

Could you add prints after you play the animation and when you stop it to see if they are actually running?

Just print some thing random like “print(“Animation has played”)” and “print(“Animation has stopped”)”.

Send the output once you’re done.

1 Like
local event = game.ReplicatedStorage.RemoteEvents.Drink
local d = false
local IsBeingHeld = false
local cooldown = 4.432

----------------------------------------------------------------------------------
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6928507384"
anim.Name = "anim1"

local anim2 = Instance.new("Animation")
anim2.AnimationId = "rbxassetid://6928670679"
anim2.Name = "anim2"
----------------------------------------------------------------------------------

event.OnServerEvent:Connect(function(player)
local character = player.Character
local animation = character.Humanoid.Animator:LoadAnimation(anim)
local animation2 = character.Humanoid.Animator:LoadAnimation(anim2)
if not IsBeingHeld then
	IsBeingHeld = true
	--Equip Can

	local character = player.Character
	local cocaColaNew = game.ReplicatedStorage.CocaColaCan:Clone()
	cocaColaNew.CFrame = (character:WaitForChild("Right Arm").CFrame + 
character:WaitForChild("Right Arm").CFrame.UpVector * -1) * CFrame.Angles(math.rad(-90), math.rad(180), 
0)
	cocaColaNew.Parent = workspace.ActiveCans
	cocaColaNew.Transparency = 1
	cocaColaNew.ColaCan.Value = cocaColaNew
	cocaColaNew.Name = player.Name.. "'s CocaCola"
	
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = cocaColaNew
	weld.Part1 = character:WaitForChild("Right Arm")
	weld.Parent = character:WaitForChild("Right Arm")
	weld.Name = "Weld"

	cocaColaNew.Anchored = false
	
	cocaColaNew.openDrink:Play()
	animation2:Play()
	wait(.51)
	cocaColaNew.Transparency = 0
	animation2.Stopped:Wait()
	animation:Play()
	print("Animation Has Played!")
	
	
else
	local cocaColaNew = game.Workspace.ActiveCans:WaitForChild(player.Name.. "'s CocaCola")
	if not cocaColaNew or cocaColaNew.drinksLeft.Value <= 1 then
		if not d then
			d = true
			
			character["Right Arm"].Weld:Destroy()
			cocaColaNew.Anchored = false
			cocaColaNew.CanCollide = true
			cocaColaNew.Name = player.Name.. "'s Dropped Can"
			cocaColaNew.Parent = game.Workspace.Cans
			animation.Looped = false 
			animation:Stop()
			print("Animation Has Stopped!")
			IsBeingHeld = false
			
			d = false
		end
	else
		if not d then
			d = true
			
			print("slurp..")
			cocaColaNew.drinksLeft.Value = cocaColaNew.drinksLeft.Value - 1
			cocaColaNew.drink:Play()
			wait()
			
			d= false
		end
	end
end
end)

and they print like normal after being run

1 Like

Both the running and stopping prints got printed right?

If you’re using a tool object, are you sure it isn’t the default hold position that making it seem like the animation hasn’t stopped?

1 Like

Im not using a tool either thats what im saying im stumped

1 Like

Opps, I forgot that we were still loading the animations within the .OnServerEvent. So basically the same thing as before is happening where everytime the event is fired we are loading a new animation and playing it. When its fired again and else condition is met, we are loading a NEW animation and stopping it (the original animation is still playing).

Theres 2 ways you can solve this.
→ Create a table and store the loaded animations. Every time the OnServerEvent is fired you would need to check whether the player has the animations loaded, if they do just reference and use those. If not load them and add them to the table.

→ Use :GetPlayingAnimationTracks() to get a list of currently playing tracks. Loop through the list and have an if statement that checks if its the same name as the animations you loaded previously. If it does stop it.

https://developer.roblox.com/en-us/api-reference/function/Humanoid/GetPlayingAnimationTracks

Note that it says its “deprecated” but thats referring to Humanoid:GetPlayingAnimationTracks(). This is similar to how Humanoid:LoadAnimation() is depreacted. All you have to do is reference the Animator like how you did when you loaded the animations.

1 Like

Allright yes I realised this but there may be another option but its not working and I do not know why so I can pass information through the event but it wont let me pass the animations here is my local script

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local cocaCola = game.ReplicatedStorage:WaitForChild("CocaColaCan")
----------------------------------------------------------------------------------
local anim = game.ReplicatedStorage.Animations.Animation1

local anim2 = game.ReplicatedStorage.Animations.Animation2
----------------------------------------------------------------------------------
local animation = character.Humanoid.Animator:LoadAnimation(anim)
local animation2 = character.Humanoid.Animator:LoadAnimation(anim2)


uis.InputBegan:Connect(function(input, gameProccessedEvent)
if input.KeyCode == Enum.KeyCode.E then
	if cocaCola then
		game.ReplicatedStorage.RemoteEvents.Drink:FireServer(player, animation, animation2)
	end
end
end)

but its erroring saying attempting to index nil

1 Like

Thats because you’re creating the animations on the client. They don’t exist on the server, therefore when you send them to the server you’re basically sending them something that doesn’t exist (aka nil). If you’re already loading them on the client why not just play and pause them on the client too?

E.g when the .OnServerEvent is fired, just have a the server fire the players client (use :FireClient(Player, Arguments)). Have the client listen for the event and stop or play the animations depending on what you want them to do.

It’s honestly better to do all effects and animations on the client, so this would probably be the best approach for you. Also don’t worry about animations not replicating to other clients, they automatically do that.

1 Like

Well not anymore I actually am referencing them I made to animation objects and stored them in a animations folder in rep storage and the problem with firing client and playing them on the client is I want everyone to see them holding the can so I need to play them on the server but they shouldnt be nil since im passing through an actual object in the game

1 Like

As I mentioned previously, animations automatically replicate to all other clients. So even if you play them on the players client, others will still see it on their screen.

Note that this only applies if the animator object wasn’t created on the client (which isn’t the case here).

1 Like

Just incase you want to verify this yourself. You can do a quick little test:

wait(4) -- just to make sure everything loaded so i dont have to do any fancy stuff to see if the player char loaded ;D
local Player = game.Players.LocalPlayer;
local Character = Player.Character;
local Animator = Character.Humanoid.Animator;

local Animation = Animator:LoadAnimation(script.Animation);

while wait(3)do
	Animation:Play();
	print("played animation on client");
end;

The animation is just a simple one of the player falling into the ground. This is the setup for it:
image

This is what the players client sees:

This is what the server sees:

As you can see the animation replicated to the server even though the animation was played on the client.

1 Like

Well it seems complex to switch between client and server but I will try this

1 Like

Ok I do not know how to go about this because my script uses waits and other things so would I have to make multiple remote events?

1 Like