Looped animation wont stop

im coding an r6 melee weapon (just for detail) and i’ve added an idle animation onto it… the idle animation itself is a looped animation, and it plays just fine!
i cant, however, get it to stop playing… :GetPlayingAnimationTracks returns as a nil value, and the script runs into an error once I call the track to :Stop()…

local tool = script.Parent

local m6d
local animation
local playa

script.Parent.equip.OnServerEvent:Connect(function(player)
	playa = player
end)


tool.Equipped:Connect(function()
	animation = script.idle
	local hum = script.Parent.Parent:FindFirstChild("Humanoid")
	local truck = hum:LoadAnimation(animation)
	
	local a:Weld = script.Parent.Parent:FindFirstChild("Right Arm"):WaitForChild("RightGrip")
	m6d = Instance.new("Motor6D")
	m6d.Parent = script.Parent.Parent:FindFirstChild("Right Arm")
	m6d.Name = "RightGrip"
	m6d.Part0 = a.Part0
	m6d.Part1 = a.Part1
	m6d.C0 = a.C0
	m6d.C1 = a.C1
	a:Destroy()
	
	truck:Play()
end)

tool.Unequipped:Connect(function()
	print(playa)
	animation = script.idle
	local hum = playa.Character:FindFirstChild("Humanoid")
	local truck = hum.Animator:GetPlayingAnimationTracks()
	
	m6d:Destroy()
	truck:Stop()
end)

im sorry if this is a really simple problem, i’m new to scripting and this is my first devforum post…
thank you in advance, though!

1 Like

I think GetPlayingAnimationTracks() is deprecated by roblox…

Yeah but there are literally no alternatives.

is there maybe another way i could do this?

Well just do truck:Stop() in a Unequipped event.
Also consider using Animator to LoadAnimation since Humanoid:LoadAnimation() is deprecated.

Its better to not use looped animations, set the looped property to false and keep playing the animation until he moves (or whenever you want) and stop after, better to use runService and make it wait until the animation is ended, feel free to ask for a code to understand what i meant!

Also you shouldn’t load the animation every time you equip the tool instead just load the Animation at the start of the script to prevent you from hitting the Animation limit.

so would I load them in something like

local idle = playa.Character:WaitForChild("Humanoid"):LoadAnimation(script.idle)

?
sorry, I dont fully understand how to use animator…

so I should unloop the animation and have it replay every time it finishes?
please do send an example script!

The animator instance is inside the Humanoid

Yes you should do it, ill write you a quick example, just give me a second here.

oh, I just noticed i called on it in the unequipped function…
so it would be

local idle = playa.Character:WaitForChild("Humanoid").Animator:LoadAnimation(script.idle)

im not fully sure if i could just put “.Animator” at the end of the humanoid there, sorry-

This way is deprecated, but if you want to use it don’t add animator

So it would be like this

Player.Character.Humanoid:LoadAnimation("YourAnim")

Ok so to fix this you first Load the animation at the start of the script like this

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Hum = char:FindFirstChildOfClass("Humanoid") or char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
local ani = script.idle

local truck = Animator:LoadAnimation(ani)

then just start the animation with

   truck:Play()

to stop the animation do

   truck:Stop()

This is written on phone so might have miss spelled some stuff

animation.looped = false --to force it un-looped

local runService = game:GetService("RunService")

local isPlaying = {} --a table so you can add as much animations as you want!

runService.renderStepped:Connect(function()
	if not table.find(isPlaying , animation.Name) then 
		animation:Play() --check capitalization...

		table.insert(isPlaying , animation.Name)

		animation.Stopped:Connect(function()
			table.remove(isPlaying , table.find(isPlaying , animation.Name))
		end)
	end
end)

im guessing that wouldnt work in a server script (which is what i have the script in currently)
what i used to get the humanoid (but only utilized it in the unequip function) was a remote event fired to the server from local script; do you think i could define player and character through that and the script you posted would still function the same way?

so what this does is that it unloops the animation, creates a table for animations, then… what exactly does the runservice function do? im sorry, imt rying to understand it

It keeps on running the animation, if it is NOT on the table then add it, play it and when it finishes then remove from table, you can add a break when you want to stop it (or another variable)

hmm okayy
how would stopping it work, exactly?

Gimme a sec, ill make some changes