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.
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!
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!
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?
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()
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.
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.
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 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!