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

Failed attempt at looping tween? (not error)

Asked by 4 years ago

When I try to loop this tween the game just stop doing it completely. any help?

01local TweenService = game:GetService("TweenService")
02local Info = TweenInfo.new(
03    5,
04    Enum.EasingStyle.Cubic,
05    Enum.EasingDirection.Out,
06    0,
07    false,
08    0
09)
10local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})
11 
12while wait() do
13    TweenAffect:Play()
14    TweenAffect.Completed:wait()
15    wait(0.1)
16end
0
Does it tween atleast once? ACHRONlX 255 — 4y
0
so you want the tween to be infinite? mikey2019d 43 — 4y
0
Almost sure you can do that just with tween info Leamir 3138 — 4y
0
nope rookiecookie153 53 — 4y
0
Instead, make a function that creates and runs the tween, then make a loop that constantly loops the function. cookie_more 30 — 4y

3 answers

Log in to vote
1
Answered by
farrizbb 465 Moderation Voter
4 years ago
Edited 4 years ago

Well there are many things wrong with your code. Let's start off with the loop, which has a few problems.

1while wait() do
2    TweenAffect:Play()
3    TweenAffect.Completed:wait()
4    wait(0.1)
5end

There's no need to do while wait() do at all really it's bad practice you should always just do

1while true do
2    wait()

But it's even worse in this case as you're already using a wait in the code, so there's no need to use two. Also tween has a parameter that allows them to loop by themselves and also a delay time parameter.

1local Info = TweenInfo.new(
2    5,
3    Enum.EasingStyle.Cubic,
4    Enum.EasingDirection.Out,
5    0, -- This is how many times it loops change it to -1 and it'll loop forever
6    false,
7    0 -- delaytime change this to 0.1 since you want to wait that long before the next loop
8)

That's all good and all, but the real reason your code isn't working is because of this line

1local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})

It's not going to work how you think it will. Let me explain, this variable will not update (even if it did the tween doesn't update either), so your tween will constantly just be tweening to the same value. Say it was 14:00 at the time the code ran, it would keep tweening to 02:00. You need to create a new tween every loop. So moving your code into the while loop block so it updates would look something like this.

01local Info = TweenInfo.new(
02    5,
03    Enum.EasingStyle.Cubic,
04    Enum.EasingDirection.Out,
05    0,
06    false,
07    0
08)
09 
10while true do
11    local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})
12 
13    TweenAffect:Play()
14    TweenAffect.Completed:Wait()
15    wait(0.1)
16end
0
Forgot to say i fixed it D: rookiecookie153 53 — 4y
Ad
Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years ago

Using a while loop to tween infinitely is bad. Don't do that. Please. Instead take advantage of the 4th argument for TweenInfo. If it's -1, it will tween an infinite number of times.

01local TweenService = game:GetService("TweenService")
02local Info = TweenInfo.new(
03    5,
04    Enum.EasingStyle.Cubic,
05    Enum.EasingDirection.Out,
06    -1, -- // -1 means that it will run forever until you cancel it.
07    false,
08    0
09)
10 
11local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})
12TweenAffect:Play()

This should tween forever.

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Sorry about not accepting the answers because I kind of found a fix and forgot to tell you guys. I'm also just gonna say real quick that I wanted to post this for a friend. Thanks for your help anyways

Answer this question