Complete a Tween When Starting A New Tween?

  1. What do you want to achieve? Keep it simple and clear!
    I want to start a new tween only after the first tween has finished.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that the first tween repeats, and I do not know how to let it complete when I start a new tween.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to use :cancel() to stop the first tweet and then right after playing the new tween, but it plays the new tween even if the first tween isn’t finished. I am new to using tweens still, and the only other thing that I think I can use is .Completed:Wait() but I do not know how that would work.

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
	local LightsV1 = TweenService:Create(Lights, TweenInfo[1], Color[10])

	for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
		local LightsV2 = TweenService:Create(Lights, TweenInfo[1], Color[5])	

		Button[1].MouseButton1Click:Connect(function()
			LightsV1:Play()

			Button[2].MouseButton1Click:Connect(function()		
				LightsV2:Play()

			end)
		end)		
	end
end

Here is a video. The first tween is to make it blue and the second tween is to make it green.

Any help and suggestions are appreciated!

5 Likes

Hi, you would want to create a queue system for your tweens then.

Here is a rough estimate, some of it might need tweaking.

local tweens = {}

local tweens_running = false
function run_tween()
		tweens_running = true

		tweens[1]:Play()
		tweens[1].Completed:Wait()

		table.remove(tweens,1)
		if #tweens > 0 then
			run_tween()
		else-- Only disable tweens running, when we in fact got no more in queue
			tweens_running = false
		end
	end

end


function add_tween(tween_to_add)
	table.insert(tweens,tween_to_add)
	if not tweens_running then
		run_tween()
	end
end

local created_tween = TweenService:Create(Lights, TweenInfo[1], Color[5])
add_tween(created_tween)

4 Likes

That method blocks (yields) which means it stops your code right there until it fires. It should work if you did it right!

tween1:Play()

tween1.Completed:Wait() -- waits for it to complete...

tween2:Play()
2 Likes

Hi! Thank you! So I added this and included my buttons and now I have 2 issues:

  1. The tween only applies to one part, not all of them.

Here I clicked button 1 to make the parts blue but only one part of all the parts turns blue.

  1. When I try to start a second tween after already starting one, the second one does not start.

Here I clicked button 2 to make the parts green and again only one part turns green. I also press button 1 after pressing button 2 but the next tween does not start.

Here is how the script looks now, with my buttons added.

local tweens  = {}
local tweens_running  = false
function run_tween()
	tweens_running  = true

	tweens[1]:Play()
	tweens[1].Completed:Wait()

	table.remove(tweens ,1)
	if #tweens  > 0 then
		run_tween()
	else-- Only disable tweens running, when we in fact got no more in queue
		tweens_running  = false
	end
end

function add_tween(tween_to_add)
	table.insert(tweens ,tween_to_add)
	if not tweens_running  then
		run_tween()
	end
end

Button[1].MouseButton1Click:Connect(function()
	for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
		local LightsV1 = TweenService:Create(Lights, TweenInfo[1], Color[10])
		add_tween(LightsV1)
	end
end)

Button[2].MouseButton1Click:Connect(function()
	for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
		local LightsV2 = TweenService:Create(Lights, TweenInfo[1], Color[5])
		add_tween(LightsV2)
	end
end)
1 Like

Hi! I tried this but it did not work. When it runs it lags for a bit and I get this:
“attempt to call a RBXScriptSignal value”"

1 Like

Did you run this: tween1.Completed:Wait()

Or this: tween1.Completed()

1 Like

I forgot the :Wait() ! But now that I did add it nothing happens. Not even an error in Outpout. :upside_down_face:

2 Likes

The thing is it blocks like I stated earlier. I’m assuming that’s the problem and it’s only running once. You’ll have to run it asynchronously all at the same thing like this, use this code for add_tween:

function add_tween(tween_to_add)
	table.insert(tweens ,tween_to_add)
	if not tweens_running  then
		task.spawn(run_tween)
	end
end

Tell me if it doesn’t work right. I’m unsure of how your code works.

2 Likes

this works, but i still have the same issues. The tween only applies to one part out of all the parts. And also once I have one tween playing the second tween does not play if I try to play it.

1 Like

If you do it in a for loop it’s going to do them one at a time no? So maybe try adding all of them into the table first and then run the tween and see if it works better

1 Like

Okay so I did that but it still only applies to one of all the parts. I switched the loop and button so the loop happens first, finds all the parts, and then I can use the button to add the tween to the table…

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
	local LightsV1 = TweenService:Create(Lights, TweenInfo[1], Color[10])
	Button[1].MouseButton1Click:Connect(function()
		add_tween(LightsV1)

	end)
end
1 Like

Update! I fixed the first issue! So I removed the “tweens[1]:Completed.Wait()” and now the tween applies to all of the parts! BUT, I still have the issue that when I click the second button to start a new tween, the first tween does not finish with the parts in their original color. Since the parts start off as white, I want the new tween to also begin with the parts in their original white color.

Here is the script now and video of it running.

local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local Module = require(script.ModuleScript)
local Button = Module.Buttons
local Tag = Module.Tags
local TweenInfo = Module.Tween
local Color = Module.Colors


local tweens  = {}
local tweens_running  = false
function run_tween()
	tweens_running  = true

	tweens[1]:Play()

	table.remove(tweens ,1)
	if #tweens > 0 then
		run_tween()
	else-- Only disable tweens running, when we in fact got no more in queue
		tweens_running  = false
	end
end

function add_tween(tween_to_add)
	table.insert(tweens ,tween_to_add)
	if not tweens_running  then
		task.spawn(run_tween)
	end
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
	local LightsV1 = TweenService:Create(Lights, TweenInfo[1], Color[10])
	Button[1].MouseButton1Click:Connect(function()
		add_tween(LightsV1)

	end)
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
	local LightsV2 = TweenService:Create(Lights, TweenInfo[1], Color[5])
	Button[2].MouseButton1Click:Connect(function()
		add_tween(LightsV2)
	end)
end

1 Like
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
local Module = require(script.ModuleScript)
local Button = Module.Buttons
local Tag = Module.Tags
local TweenInfo = Module.Tween
local Color = Module.Colors

local originalColors = {}

local tweens = {}
local tweens_running = false

function run_tween()
    tweens_running = true

    tweens[1]:Play()

    table.remove(tweens, 1)
    if #tweens > 0 then
        run_tween()
    else
        tweens_running = false
    end
end

function add_tween(tween_to_add)
    table.insert(tweens, tween_to_add)
    if not tweens_running then
        task.spawn(run_tween)
    end
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
    originalColors[Lights] = Lights.Color
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
    local LightsV1 = TweenService:Create(Lights, TweenInfo[1], {Color = originalColors[Lights]})
    Button[1].MouseButton1Click:Connect(function()
        add_tween(LightsV1)
    end)
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
    local LightsV2 = TweenService:Create(Lights, TweenInfo[1], {Color = originalColors[Lights]})
    Button[2].MouseButton1Click:Connect(function()
        add_tween(LightsV2)
    end)
end
1 Like

This did not end up working for me, however your original code did end up helping me! This is what I ended up with.

Thanks to you I also cleaned up some of my original code up. AND I figured out the solution! Idk if this can be dont better but I added a new tween specifically to turn the color back to White and then added tween:Play and tween.Completed:Wait() so that every time I click a button it tweens the color white THEN to the new color I want. Here is the code and a video showing it works!

local tweens  = {}
local tweens_running = false
function run_tween()
	tweens_running = true

	tweens[1]:Play()
	table.remove(tweens ,1)

	if #tweens > 0 then
		run_tween()
	else-- Only disable tweens running, when we in fact got no more in queue
		tweens_running = false
	end
end

function add_tween(tween_to_add)
	table.insert(tweens, tween_to_add)
	if not tweens_running then
		task.spawn(run_tween)
	end
end

for i, Lights in pairs(CollectionService:GetTagged(Tag[1])) do
	local OGLights = TweenService:Create(Lights, TweenInfo[1], Color[1])
	local LightsV1 = TweenService:Create(Lights, TweenInfo[2], Color[2])
	local LightsV2 = TweenService:Create(Lights, TweenInfo[2], Color[8])
	local LightsV3 = TweenService:Create(Lights, TweenInfo[2], Color[13])

	Button[1].MouseButton1Click:Connect(function()
		OGLights:Play()
		OGLights.Completed:Wait()
		add_tween(LightsV1)	

	end)
	Button[2].MouseButton1Click:Connect(function()
		OGLights:Play()
		OGLights.Completed:Wait() 
		add_tween(LightsV2)	
	end)
	Button[3].MouseButton1Click:Connect(function()
		OGLights:Play()
		OGLights.Completed:Wait() 
		add_tween(LightsV3)	
	end)
end

1 Like