UNSOLVED DAY 17 ! Animation Track plays the animation but doesnt stop it

You can write your topic however you want, but you need to answer these questions:

  1. **Script says Animation track stops the animation but it stays as playing.(more in the vid)

2.Watch 2024-03-23 13-13-53 | Streamable as in the video at the last part script says that the animation is stopped but its not

  1. **I tried printing the animations that are getting played but it doesnt show the animation thats getting played
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DropItem = ReplicatedStorage.RE.DropItem
local DropNonStackableItem = ReplicatedStorage.RE.DropNonStackableItem
local ViewModeltem = ReplicatedStorage.RE.ViewModeltem
local DeleteItem = ReplicatedStorage.RE.DeleteItem

local player = game.Players.LocalPlayer

local MainGui = script.Parent

local ActivatedGui = MainGui.Parent.Parent:WaitForChild("ViewModelSlotNumber")
local ActivatedGuiItem =  MainGui.Parent.Parent:WaitForChild("ViewModelCurrentItem")

local ViewModel = game.Workspace.Camera.ViewModel
local animations = {
	["Idle"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Idle),
	["HoldNormalItem"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.HoldNormalItem),
	["Axe"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Axe),
	["Knife"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Knife),
	["Pickaxe"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Pickaxe)
}

animations.Idle:Play()

-----------------------------SLOT-GET-VPFRAME------------------------------

local function getViewPortFrame(frame)
	for _, v in ipairs(frame:GetChildren()) do
		if v:IsA("ViewportFrame") then
			return v 
		end
	end
	return nil
end

local slotHandDelay = false --DELAYS

---------------------ATTACH-ITEM-------------------------------
function AttachItemToVM(ItemToHold)
	game.Workspace.Camera.ViewModel.ItemPos.Item.Part1 = ItemToHold
	ItemToHold.Position = game.Workspace.Camera.ViewModel.ItemPos.Position
	ItemToHold.Parent = game.Workspace.Camera.ViewModel.ItemPos
end


-----------------------------SLOT-1-------------------------------

MainGui.Slot1.MouseButton2Down:Connect(function()
	if ActivatedGui.Value ~= MainGui.Slot1.Name then
		if ActivatedGui.Value == MainGui.Slot1.Name then
			animations.Idle:Play()
		end
		local MainSlot = MainGui.Slot1
		local getItem = getViewPortFrame(MainSlot) 
		if MainGui.Slot1.HowMany.Value > 1 then
			local DropItem = DropItem:InvokeServer(getItem)
		else
			local DropNonStackableItem = DropNonStackableItem:InvokeServer(getItem)
		end	
	end
end)

MainGui.Slot1.MouseButton1Down:Connect(function()
	local MainSlot = MainGui.Slot1
	local getItem = getViewPortFrame(MainSlot)
	local slot = "Slot1"

	if getItem then
		local ItemToHold = game.ReplicatedStorage.VMItemModels:FindFirstChild(getItem.Name):Clone()

		if ActivatedGui.Value == "" then
			if MainGui.SlotsItemName.Slot1.Value ~= "" then
				if MainGui.SlotsItemName.Slot1.Value == getItem.Name then
					if getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Axe" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Axe:Play()
						local guiItem = getItem.Name	
						local ItemClass = "Axe"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Normal Item" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.HoldNormalItem:Play()
						local guiItem = getItem.Name
						local ItemClass = "Normal Item"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Pickaxe" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Pickaxe:Play() 
						local guiItem = getItem.Name
						local ItemClass = "Pickaxe"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Knife" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Knife:Play()
						local guiItem = getItem.Name	
						local ItemClass = "Knife"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					end
				end
			end
		else
			print("no item")
		end
	end
end)

-----------------------------SLOT-2-------------------------------

MainGui.Slot2.MouseButton2Down:Connect(function()
	if ActivatedGui.Value ~= MainGui.Slot2.Name then
		if ActivatedGui.Value == MainGui.Slot2.Name then
			animations.Idle:Play()
		end
		local MainSlot = MainGui.Slot2
		local getItem = getViewPortFrame(MainSlot) 
		local getItemName = MainGui:GetChildren()
		if MainGui.Slot2.HowMany.Value > 1 then
			local DropItem = DropItem:InvokeServer(getItem)
		else
			local DropNonStackableItem = DropNonStackableItem:InvokeServer(getItem)
		end	
	end
end)

MainGui.Slot2.MouseButton1Down:Connect(function()
--	wait(0.2)
	local MainSlot = MainGui.Slot2
	local getItem = getViewPortFrame(MainSlot)
	local slot = "Slot2"

	if getItem then
		local ItemToHold = game.ReplicatedStorage.VMItemModels:FindFirstChild(getItem.Name):Clone()

		if ActivatedGui.Value == "" then
			if MainGui.SlotsItemName.Slot2.Value ~= "" then
				if MainGui.SlotsItemName.Slot2.Value == getItem.Name then
					if getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Axe" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Axe:Play()
						local guiItem = getItem.Name	
						local ItemClass = "Axe"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Normal Item" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.HoldNormalItem:Play()
						local guiItem = getItem.Name
						local ItemClass = "Normal Item"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Pickaxe" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Pickaxe:Play()
						local guiItem = getItem.Name
						local ItemClass = "Pickaxe"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					elseif getItem:FindFirstChild(getItem.Name).WhatItem.Value == "Knife" then

						AttachItemToVM(ItemToHold)
						animations.Idle:Stop()
						animations.Knife:Play()
						local guiItem = getItem.Name	
						local ItemClass = "Knife"
						ViewModeltem:FireServer(slot, guiItem, ItemClass)

					end
				end
			end
		end
	end
end)
  {

--MORE SLOTS HERE BUT I DELETED( they are same as the others)

}
-----------------------------SLOT-HAND-------------------------------

MainGui.SlotHand.MouseButton1Down:Connect(function()
	--print(slotHandDelay)
	if slotHandDelay ~= true then
		slotHandDelay = true
		if ActivatedGuiItem.Value ~= ""  then
			game.Workspace.Camera.ViewModel.ItemPos:FindFirstChild(ActivatedGuiItem.Value):Destroy()
			local slot = ""
			local guiItem = ""
			local ItemClass = ""
			ViewModeltem:FireServer(slot, guiItem, ItemClass)
			animations.Idle:Stop()
			animations.Idle:Play()
			animations.HoldNormalItem:Stop()
			animations.Axe:Stop()
			animations.Knife:Stop()
			animations.Pickaxe:Stop()
		end
		wait(1)
		slotHandDelay = false
	end
end)

ViewModeltem.OnClientEvent:Connect(function()
	animations.Idle:Stop()
	animations.Idle:Play()
	animations.HoldNormalItem:Stop()
	animations.Axe:Stop()
	animations.Knife:Stop()
	animations.Pickaxe:Stop()
end)

player.PlayerGui.ViewModelCurrentItem.Changed:Connect(function()
	if player.PlayerGui.ViewModelCurrentItem.Value == "" or player.PlayerGui.ViewModelCurrentItem.Value == nil then
		animations.Idle:Stop()
		animations.Idle:Play()
		animations.HoldNormalItem:Stop()
		animations.Axe:Stop()
		animations.Knife:Stop()
		animations.Pickaxe:Stop()
	else
		animations.Idle:Stop()
	end
end)

DeleteItem.OnClientEvent:Connect(function()
	local slot = ""
	local guiItem = ""	
	local ItemClass = ""
	if game.Workspace.Camera.ViewModel.ItemPos:FindFirstChild(ActivatedGuiItem.Value) then game.Workspace.Camera.ViewModel.ItemPos:FindFirstChild(ActivatedGuiItem.Value):Destroy()
	else
		print("Game Broken or Deleted from another script")
	end	
	wait(tick)
	ViewModeltem:FireServer(slot, guiItem, ItemClass)
	animations.Idle:Stop()
	animations.Idle:Play()
	animations.HoldNormalItem:Stop()
	animations.Axe:Stop()
	animations.Knife:Stop()
	animations.Pickaxe:Stop()
end)

script.Parent.TextButton.MouseButton1Click:Connect(function()
	print(ViewModel.AnimationController:GetPlayingAnimationTracks())
end)

I also checked every single thing possible but they are all working like they should.

The video says it is no longer available. Is the animation set to Looping in Animation Editor?

Im really sorry Im reuploading it! Also I dont think it is becuase it only happens on this.They are the same animations(even I didnt made a animation for the knife it uses the same id as others and it doesnt happen on them)

Its not clear from the video why the animation repeats. I am going suggest a change to the MouseButton1Down & MouseButton2Down connect events:

-- new variable name to match anim and getItem.Name variable:
local animations = {
	["Idle"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Idle),
	["Normal Item"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.HoldNormalItem),
	["Axe"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Axe),
	["Knife"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Knife),
	["Pickaxe"] = ViewModel.AnimationController.Animator:LoadAnimation(ReplicatedStorage.ViewModelAnims.Pickaxe)
}

-- Simplified connect, removing the endless if statements:
MainGui.Slot1.MouseButton1Down:Connect(function()
	local MainSlot = MainGui.Slot1
	local getItem = getViewPortFrame(MainSlot)
	local slot = "Slot1"

	if getItem then
		local ItemToHold = game.ReplicatedStorage.VMItemModels:FindFirstChild(getItem.Name):Clone()

		if ActivatedGui.Value == "" then
			if MainGui.SlotsItemName.Slot1.Value ~= "" then
				if MainGui.SlotsItemName.Slot1.Value == getItem.Name then
					AttachItemToVM(ItemToHold)
					animations.Idle:Stop()
					animations[getItem.Name]:Play()	-- Find the anim by name in the "animations" table, Change of name for Normal Item" to fix
					local guiItem = getItem.Name	
					local ItemClass = getItem.Name
					ViewModeltem:FireServer(slot, guiItem, ItemClass)
	
-- wait till anim ends then play idle
					animations[getItem.Name].Stopped:Wait() 
					animations.Idle:Play()

-- Alternative to above if anim is set to Looping
					animations[getItem.Name].Ended:Connect(function()
					 	animations[getItem.Name]:Stop()
					 	animations.Idle:Play()
					end)
				end
			end
		end
	end
end)

Note I have not tested this code, as it is written just ā€˜as is’. It should work with a little work. It check for the anim completing, forcibly stops it, then plays idle.

If the animation IDs are truly the same and the code appears identical there is no logical reason why when the knife anim is played it should loop.

1 Like

So I understand what the code does but it shouldnt be looped as I think because when I print the animations that are currently being played it doesnt show. I’m going to try the code and see if it works


As you can see i marked on the animation thats currently getting played even I tried the script (I edited it for it to work). I still dont understand what the issue is.
Edit: I also found it randomly stops after this aniamtions issue. Like when I put the item in the inventory and animation glitches, after some random time it stops. But it doesnt always happen.

Anyone can still help? please!

1 Like

Anyone can still help? please!

Instead of stopping individual animations, you can try something like this (sorry if you have already, the script is a bit long so I might have missed it):

local animator = player.Character.Humanoid.Animator

for _, anim in ipairs(animator:GetPlayingAnimationTracks()) do
    anim:Stop(0)
end
1 Like

Yes thank you for responding! I added this to my script but still have the issue of the animation getting stuck :frowning:

But surely if you stop them right before playing another one it would be unnoticeable?

1 Like

Yes! But even the animation doesnt stop so you cant play another one on top. It does stop but it gets stuck on that thing.

I don’t get what you mean. If you play an animation immediately after you stop all the others, it wouldn’t be noticeable. If the animation ends, you could loop it.

Do you mean the player’s character gets stuck with no animation? You can change the humanoid’s state when the animation finishes in order to trigger an animation.

1 Like

Nope let me explain:

  1. Player equips the Stone Tool (animation playing ā€œHoldā€)
  2. Player uses the stone tool to make a knife (animation ā€œHoldā€ Stops)
  3. player equips the knife (animation ā€œKnifeā€ plays)
    4 .player unequips the knife (animation ā€œKnifeā€ doesnt stop even it should! Any other animation that plays doesnt matter when or how it doesnt stop the ā€œKnifeā€ animation)

I mean the best I can explain

So you can try running this procedure for each tool:

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Humanoid")

local function stopAnimations(newAnim)
    --"newAnim" should be the new animation track.
    for _, anim in ipairs(animator:GetPlayingAnimationTracks()) do
        anim:Stop(0)
    end
    newAnim.Ended:Wait()
    for _, anim in ipairs(animator:GetPlayingAnimationTracks()) do
        anim:Stop(0)
    end
    hum:ChangeState(Enum.HumanoidStateType.Freefall) --this should force an animation
end
1 Like

Oh wait let me see. I hope this is the solution!

I added the line:

    for _, anim in ipairs(animator:GetPlayingAnimationTracks()) do
        anim:Stop(0)
    end

It doesnt stop the animation of the knife like its the same results in the video. Also they are not tools I forgot to mention this is a viewModel

Did you stop all animations after the knife animation finished? You might need to change the humanoid’s state to trigger default animations.

1 Like

Im using animation controller so it doesnt have humanoid:
image

Oh, alright. Am I right in thinking you want the knife animation to stop? If yes, you could just have it not looped. Or, do something like:

knifeAnimation.Ended:Wait()
knifeAnimation:Stop() --or knifeAnimation:Destroy()
1 Like