How to stop specific tween

Hey,
so I am currently making a module where players can create magic rocks that grow and shrink. The player can create as many rocks as they wants, and because of this need to create multiple different tweens and parts that are tweened.

However, when a player clicks the rock again, it will stop tweening (growing and shrinking) and will just stay in the position. As in: the tween get’s cancelled.

I do not have any issues with this from a tween perspective. But I do have issues on how I can stop the animation of a certain parts. As in:
Player creates Part1, Part2, and Part3. He then wants to stop the Part2 from tweening. But, since I created all of these tweens and parts in a module I do not know how to stop that exact tween, since I have multiple tweens running at the same time.

local animations = {}

tweenService = game:GetService("TweenService")

--//Constants
local maxSize = 25
local magicRockStartSize = Vector3.new(3,1,1)

local tweenGoal = {}
tweenGoal.Size = Vector3.new(magicRockStartSize.X,maxSize,magicRockStartSize.Z)

local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,-1,true,0)


function animations.animate(player)
	local rootPartPosition = player.Character.HumanoidRootPart.Position
	local xRootPos = rootPartPosition.X
	local yRootPos = rootPartPosition.Y
	local zRootPos = rootPartPosition.Z

	magicRockPart = Instance.new("Part") 
	magicRockPart.Name = "magicRock" 
	magicRockPart.Size = Vector3.new(2,1,2)
	magicRockPart.Anchored = true
	magicRockPart.Parent = game.Workspace

	local magicRockOffset = Vector3.new(0,-2,-3)
	magicRockPart.CFrame = player.Character.HumanoidRootPart.CFrame*CFrame.new(magicRockOffset)

	local tweenGoal = {}
	tweenGoal.Size = Vector3.new(magicRockStartSize.X,maxSize,magicRockStartSize.Z)
	tweenGoal.Position = Vector3.new(xRootPos-magicRockOffset.X,(yRootPos+maxSize/2),zRootPos-magicRockOffset.Z)

	local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,-1,true,0)

	growTween = tweenService:Create(magicRockPart, tweenInfo, tweenGoal)
	growTween:Play()
end


function animations.stop()
	growTween:Cancel()
end


return animations

The below script is just for testing, but it does the same thing as I described above.

animationsModule = require(game.ReplicatedStorage.Animations)

character = game.Players.LocalPlayer.CharacterAdded:Wait()
wait(3) --i know this is bad coding, just for testing

animationsModule.animate(game.Players.LocalPlayer)
wait(0.2) --starting the second animation a little later for testing purposes
animationsModule.animate(game.Players.LocalPlayer)
wait(1)
animationsModule.stop() --stops just the first part that is animation. But I do not understand how this works... to be honest.

As you can see, when I try to stop the animation, I have only given the animations the same names aka. they run on the same tweens. And therefore, it acts weird.

I am not sure what the best solution is for solving this. I was thinking to maybe have different variable names for each animation, or store them in a table. But I am really not sure.

Thanks for any help! (Maybe modules aren’t the correct way to go about this?)

1 Like

Personally, the way I would go about this is, when the player stops the tween, create some kind of variable and save the current state of the properties of the part (rock). then stop the tween, and set those properties to the rock.