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

Why is This Tween/Coroutine Loop to Repeatedly Change Colours Not Working?

Asked by 1 year ago

I have quite a large sum of lightbulbs (spheres grouped together) that I would like to repeatedly Tween to a different colour and back simultaneously. I discovered that Tweening alone would not work, for the lightbulbs merely changed colour one at a time, as opposed to all at once. Therefore, I tried coroutining. With the script I’m currently using, however, none of the lightbulbs change whatsoever, and the output does not give any clue as to where I’ve gone wrong. If anyone could point out the error(s) in my script, it would be very much appreciated.

local Model = workspace.Colourful.Lightbulbs
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)
local Goal1 = {Color = Color3.fromRGB(100, 100, 100)}
local Goal2 = {Color = Color3.fromRGB(255, 195, 155)}

while true do
    wait()
    coroutine.resume(coroutine.create(function()
        for i, part in pairs(Model:GetChildren()) do
            if part:IsA("BasePart") then
                local tween1 = TweenService:Create(part, tweenInfo, Goal1)
                tween1:Play()
                tween1.Completed:Wait(1)
                local tween2 = TweenService:Create(part, tweenInfo, Goal2)
                tween2:Play()
                tween2.Completed:Wait(1)
            end
        end
    end))
    coroutine.yield()
end

1 answer

Log in to vote
0
Answered by 1 year ago

TweenInfo contains properties which allows you to repeat and reverse the tween. See if this works:

local TweenService = game:GetService('TweenService')
local Model = workspace.Colourful.Lightbulbs

for _, v in pairs(Model:GetChildren()) do
    if v:IsA('BasePart') then
        TweenService:Create(v, TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, true), {Color = Color3.fromRGB(100, 100, 100)}):Play()
    end
end
0
Excellent, it works perfectly and is much more streamlined code. destinyscampchild 2 — 1y
Ad

Answer this question