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?

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
    5,
    Enum.EasingStyle.Cubic,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)
local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})

while wait() do
    TweenAffect:Play()
    TweenAffect.Completed:wait()
    wait(0.1)
end
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.

while wait() do
    TweenAffect:Play()
    TweenAffect.Completed:wait()
    wait(0.1)
end

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

while true do
    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.

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

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

local 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.

local Info = TweenInfo.new(
    5,
    Enum.EasingStyle.Cubic,
    Enum.EasingDirection.Out,
    0,
    false,
    0
)

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

    TweenAffect:Play()
    TweenAffect.Completed:Wait()
    wait(0.1)
end
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.

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(
    5,
    Enum.EasingStyle.Cubic,
    Enum.EasingDirection.Out,
    -1, -- // -1 means that it will run forever until you cancel it. 
    false,
    0
)

local TweenAffect = TweenService:Create(game.Lighting, Info, {ClockTime = game.Lighting.ClockTime+12})
TweenAffect: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