Looped animation wont stop

Here

local runService = game:GetService("RunService")

local stopped = {} --a table so you can add as much as you want!
local isPlaying = {} --a table so you can add as much animations as you want, same as before.

function handle(animation)
	if table.find(stopped , animation.Name) then return end

	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

runService.renderStepped:Connect(function()
	--your animation.looped = false
	--handle(your animation)
	--add it to the stopped table to stop it from playing and remove it so it continues.
end)

so the second function is how youd stop the animation, right?

To stop it add it to the stopped table and remove it to continue, read whats written inside the renderStepped, i explained everything, the function will just handle the whole animating part

im sorry im really slow today…
i get that youd use the function “handle”… but i dont get how to add it to the table, would I just add it into those brackets? is there anyway i could add them in with a line of code so i could do that on the unequipped event?

No problem, yes you can

--to add
table.insert(stopped , animation.Name)
--to remove
table.remove(stopped , table.find(stopped , animation.Name))

so for example i would do table.insert(stopped, animation.idle) to run the idle?

No this will stop it, if you add it to the table it will NOT work, if you remove it it will work, it isn’t there originally so it will just work

OHHH OKAY THANK YOU
how would i use this to run an animation once though?

Just use handle(animation) once in the script, if you want it to fully stop the animation from working, add it to the table after you call the function

okay okay,
so to run an animation once, you run handle(animation) in the script, to loop the animation you would add it to the table but then remove it, and to stop the loop you would just keep it on the table?

Yes exactly but if you want it to try running until its playable, have the handle(animation) inside the render stepped function and the adding / removing from outside.

running until its playable? like, have the animation continuously going?

It will always try to play the animation until you remove it from the table, basically trial and error until it plays and just goes on for infinity

oohhh okay thank you again! im gonna try and get it to work with the tool now

did everything work good? if yes mark the code as a solution if not send me your current code FULL code and i’ll help u fix it

Wait is the script a local script?

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

Must be a server script in order for this to work. Animations should be loaded and played on the client though.

My script was made for a local script. That’s why i asked if this is a local script.

local truck = hum:LoadAnimation(animation)

this should be changed to

local truck = hum.Animator:LoadAnimation(animation)

because in the unequipped function you are using GetPlayingAnimationTracks On the animator…
I hope this helps!

also, you shouldn’t load the animation every time, instead, do something like this

local tool = script.Parent

local m6d
local playa
local char
local humanoid

script.Parent.equip.OnServerEvent:Connect(function(player)
	playa = player
    char = playa.Character or playa.CharacterAdded:Wait()
end)

local anim = char.Humanoid.Animator:LoadAnimation(script.idle)

tool.Equipped:Connect(function()
	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()
	
	anim:Play()
end)

tool.Unequipped:Connect(function()
	print(playa)
	m6d:Destroy()
	anim:Stop()
end)

This should be the more optimal way

All you really need to do here is initialize truck outside of the tool.Equipped function. That way you can reference it in tool.Unequipped and call :Stop() on it like you did

Edit: Initialize just means create the variable and assign a value to it