Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I loop a tween over and over until the connected GUI is destroyed?

I have found a way to tween between two colours.

local tween = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out) 
local tweenOn = ts:Create(Box, tween, {ImageColor3 = onColor})
tweenOn :Play()

How do I loop this tween?

Problem 1:

The Box Gui can be destroyed as its part of an inventory and it refreshes. There is an error "Can only tween objects in the workspace" when tweens are destroyed mid tween with normal GUI tweens. How could I avoid this?

Problem 2:

Since tweens are created with :Create, is there a possibility that stacking this could cause lag? Similar to connecting events and when you don't :Disconnect them.

1 answer

✓ Accepted answer

A tween can be setup to loop. Set its repeat count to -1.

Another solution would be to use a while loop and check if the parent is not nil.

example only

local tween = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out) 
local tweenOn = ts:Create(Box, tween, {ImageColor3 = onColor})

while Box.Parent do -- while there is a parent
	tweenOn:Play()
	tweenOn.Completed:Wait()
end

You may also want to use both methods of looping the tween and when the box parent changes stop the tween.

I hope this helps.

3 comments

0

I see, my inventory adds new boxes with :Clone(), will I have to do something with boxes that were destroyed? Similar to :Disconnect() with events to avoid lag stacking?

0

Using :Destroy() will disconnect the events associated with the instance. In any case I would cleanup the box explicitly where possible.

0

cleanup? Is that a function?

This discussion is preserved as it was. New answers, comments, and votes are closed.