How do i cancel an Tween when the Tween gets played in a for loop

So i made my own Animation system for viewmodels and it works but!!

Whenever i want to cancel the Animation it does to the idle Animation but then will still continue the old animation

This is not the whole script btw
This part of the script is in a Function and fires when ever i do a MouseButtonClick or a KeyCode

for i,Key in pairs(AAFchildren) do-- goes through the animation folder
	-- this part is for finding welds
	for _,ViewmodelWeld in pairs(Viewmodel:GetDescendants()) do-- looks through the viewmodel to find motor6ds

		if ViewmodelWeld:IsA('Motor6D') then
			for _,KeyframeWeld in pairs(Key:GetDescendants()) do-- looks through the keyframe to find motor6ds

				if KeyframeWeld:IsA('Motor6D') then
					if ViewmodelWeld.Name == KeyframeWeld.Name then-- if the viewmodel weld and keyframe name are the same

						TweenService:Create(ViewmodelWeld, TweenInfo(TweenTime*Speed, EasingStyle[TweenEasingStyle], EasingDirection[TweenEasingDirection]), {C1 = KeyframeWeld.C1}):Play()
					end
				end
			end
		end
	end
end

yo bro use :Cancel() it works best for cancelling tweens and use “break” if you wanna break a loop

i know but i want to cancel it whenever the player releases the LeftMouseButton and the Animate is a function

image

i dont really know how to cancel it in a function

mind showing the full code? because my brain sucks at understanding words that are NOT in a coding language

also to get something like “left mouse button” released just do UserInputService.InputEnded then check for userinputtype “MouseButton2”

and if youre talking about trying to stop a tween that is made by a different part of a code and the function doesnt know of the code, just try to pass the tween to the function

image

ok vro i dont really get what youre aiming to do here really well i MIGHT have 4 years of coding experience but i do NOT understand anything here.

also to stop those tweens just put them in a table and go through every tween when you wanna cancel them.

Please avoid use of pairs/ipairs Performance - Luau

You do not capture referance for Tween and this is the cause why you can’t stop it.
Make sure to :Destroy() it after you done using it.

I also recomend caching tweens when possible to reuse them.

vro i use for i, v a lot and i use ipairs and pairs but mostly pairs, whats the big performance drop anyways? and what replacement should i use?

It utilized 2 different OPCODES and they behave much slower compared to generalized iteration.

this is literally your “pairs” without sugar.
Try looking at what your pairs function actually returns.
image
print(pairs({})) → function: 0xe87028781e9b508f {} nil

Even if i do an referance for the tween and cancel it or pause it another keyframe will play so the only way id think will work is by using break

What are you even trying to archive?
Just use animator.