Stop A Tween Started By One Button To Start A New Tween By A Second Button?

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to use gui buttons to start tweens on parts. Each button finds a group of parts that I find based off a shared tag, then I play a tween on the parts. What I want is to use other buttons on the same parts but run a new tween when I click it.

So what I ultimately want is to be able to:
1. Click button to play a tween.
2. Click a different button that stop any playing tween so it can start a new one.

  1. What is the issue? Include screenshots / videos if possible!
    The issue is I am not sure how I can achieve this. I have 3 buttons so far and Im a bit overwhelemed with how I can edit what I already have so that each button not only plays a new tween but checks if the parts are being tweened first to stop those tweens before starting new onesā€¦ itā€™s a lot!

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using if then, else for this but only managed to make it work so if the SAME button is pressed again then the tween will stop. I want to be able to do this so that if any of my buttons are pressed the currently playing tween stops so i can play a new one.

Here is my code:

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local Module = require(script.ModuleScript)
local Button = Module.Buttons
local Bulbs = Module.Bulbs
local TweenInfo = Module.Tween
----------------------------------------------------------------------------------------
------------------------------------- BUTTON 1 -----------------------------------------
------------------------- Alternating Light Bulbs Strobe -------------------------------
----------------------------------------------------------------------------------------
local Strobe1 = {}
local Strobe2 = {}

for _, Lights in pairs(CollectionService:GetTagged(Bulbs[1])) do
	table.insert(Strobe1, TweenService:Create(Lights, TweenInfo[2], {Transparency = 1}))
end

for _, Lights in pairs(CollectionService:GetTagged(Bulbs[2])) do
	table.insert(Strobe2, TweenService:Create(Lights, TweenInfo[2], {Transparency = 1}))
end

Button[1].MouseButton1Click:Connect(function()
	warn("Button 1 Pressed")
	for _, tween in ipairs(Strobe1) do
		tween:Play()
		print("Strobe1 Playing")
	end

	task.wait(1)
	for _, tween in ipairs(Strobe2) do
		tween:Play()
		print("Strobe2 Playing")
	end
end)

----------------------------------------------------------------------------------------
------------------------------------- BUTTON 2 -----------------------------------------
----------------------------- All Light Bulbs Strobe------------------------------------
----------------------------------------------------------------------------------------
local function createTween(Lights)
	local Strobe = TweenService:Create(Lights, TweenInfo[3], {Transparency = 1})
	Button[2].MouseButton1Click:Connect(function()
		warn("Button 2 Pressed")
		Strobe:Play()
		print("Strobe Playing")
	end)
end

for _, Lights in pairs(CollectionService:GetTagged(Bulbs[3])) do
	createTween(Lights)
end

----------------------------------------------------------------------------------------
------------------------------------- BUTTON 3 -----------------------------------------
------------------------- All Light Bulbs Fade Up To Down-------------------------------
----------------------------------------------------------------------------------------
local Rows = {}

for i = 4, 9 do
	local Fade = {}

	for _, Lights in pairs(CollectionService:GetTagged(Bulbs[i])) do
		table.insert(Fade, TweenService:Create(Lights, TweenInfo[3], {Transparency = 1}))
	end

	table.insert(Rows, Fade)
end
Button[3].MouseButton1Click:Connect(function()
	warn("Button 3 Pressed")

	for _, Fade in ipairs(Rows) do
		for _, tween in ipairs(Fade) do
			tween:Play()
			print("Fade Playing")
		end

		local maxDuration = 0
		for _, tween in ipairs(Fade) do
			maxDuration = math.max(maxDuration, .25)
		end
		task.wait(maxDuration)
	end
end)

If you have any ideas on how to rework this or what I can add to this, itā€™s all appreciated! Thank you!

2 Likes

Interesting, I am not a scripter, and am sorry I could not be of any help.

2 Likes

Iā€™m barely a scripter myself, still learning! :sweat_smile: But itā€™s fine I hope someone else can help me figure this out.

1 Like

Store all of your tweens somewhere, preferably a table ({}) and when you want to stop all tweens, run this code:

for _, tween in listOfTweens do
    tween:Cancel()
end

When you want to start a tween, run this code:

table.insert(listOfTweens, newTween)
2 Likes

I would add this right after the every Button[].MouseButton1Click:Connect(function() right? So that before it begins a new tween I am stopping any tween being played first?

1 Like

And when you start a new tween:

For this I get the error ā€˜Cancelā€™ is not a valid member of TweenInfo.

This is how I rewrote your code, right under each MouseButton1Click

	for _, playingtween in TweenInfo do
		playingtween:Cancel()
warn("TWEEN STOPPED")
	end

and TweenInfo is taken from a module script where I have my tweens already saved in a table. That looks like this

Module.Tween = {
	TweenInfo.new(	
		1, --number of seconds
		Enum.EasingStyle.Linear, --how it moves
		Enum.EasingDirection.InOut, --how it moves
		1, --how many times to repeat
		false, --go back to start after ending?
		0 --the wait before starting
	), 
	TweenInfo.new(
		1, --number of seconds
		Enum.EasingStyle.Linear, --how it moves
		Enum.EasingDirection.InOut, --how it moves
		-1, --how many times to repeat
		true, --go back to start after ending?
		0 --the wait before starting
	)

This is because you are inserting the TweenInfos, not a TweenBase (the actual tween). You need to insert whatā€™s created when you call TweenService:Create()

Ohhhhh! So would I use table.insert(playingtween, newTween) each time I press a button to save the tween created by that button and then use

for _, playingtween in TweenInfo do
		playingtween:Cancel()
warn("TWEEN STOPPED")
	end

to cancel it???

1 Like

Have you tried ā€¦

for _, tween in (whatever) do
	tween:Stop()
end
1 Like

This is already what Iā€™m having her do. Sheā€™s just trying to figure out how to get it into her code.

2 Likes

Anywhere where you create a tween using :Play(), you should also insert it with the code I showed earlier and then at the same time clear all the other tweens playing, using the respective code.

1 Like

Okay got it, I will do that now and see if i did it right and if it works! Thank you sm, I will get back to you! :grin:

Okay so I think it works, but it made me wonder, does stopping the tween revert the tweened partā€™s property back to itā€™s original value? For example, i am tweening the Transparency of the parts. For example, if a tween is tweening the transparency from 0 to 1 and I stop the tween midway, would the transparency stop at .5 or will it go back to 0 as it was before the tween???

This is the only reason I wanted to stop a tween to begin a new one since I thought stopping the tween also resets the tweened partā€™s propert.

:Cancel() resets the properties.

Cancel

void

Halts playback of a Tween and resets the tween variables.

Only resets the tween variables, not the properties being changed by the tween. If you cancel a tween halfway through its animation, the properties do not reset to their original values. Differs from TweenBase:Pause() in that once resumed, it takes the full duration of the tween to complete the animation.

So how could I stop the tween AND revert the properties back? In this case I want to revert Transparency back to 0.

1 Like

Use :Cancel(), not sure why he mentioned :Pause(), it doesnā€™t reset the variable.

1 Like

So I was just thinking and ended up putting Transparency = 0 after every MouseButton1Click of each button so all the parts ā€œresetā€ and itā€™s working how I want to, lol. Idk if itā€™s the most effective way of doing it but it worked!

1 Like

You should mark the solution post as solution so people know itā€™s solved, itā€™s helpful to others that see this post!

1 Like