Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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.

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? Marmalados 193 — 4y
0
Using :Destroy() will disconnect the events associated with the instance. In any case I would cleanup the box explicitly where possible. User#5423 17 — 4y
0
cleanup? Is that a function? Marmalados 193 — 4y
Ad

Answer this question