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