Better way to do that?

local TweenService = game:GetService("TweenService")
local TweenSpeed = 1
local btn = script.Parent:WaitForChild("MainOutline").OpenButton
local pwr = script.Parent:WaitForChild("MainOutline").Power
local Info = TweenInfo.new(TweenSpeed, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local btn_1 = TweenService:Create(btn, Info, {BackgroundColor3 = Color3.fromRGB(60,60,60)})
local btn_2 = TweenService:Create(btn, Info, {BackgroundColor3 = Color3.fromRGB(30,30,30)})
local pwr_1 = TweenService:Create(pwr, Info, {BackgroundColor3 = Color3.fromRGB(60,60,60)})
local pwr_2 = TweenService:Create(pwr, Info, {BackgroundColor3 = Color3.fromRGB(40,40,40)})

while true do
	btn_1:Play()
	pwr_1:Play()
	task.wait(1)
	btn_2:Play()
	pwr_2:Play()
	task.wait(1)
end

it should be a color changing button

1 Like

u can try replacing the while function with

local function tweenagain()
    btn_1:Play()
	pwr_1:Play()
	task.wait(1)
	btn_2:Play()
	pwr_2:Play()
	task.wait(1) 
    tweenagain() -- calls itself which makes the function loop
end

it should work

ALSO instead of using task.wait() you can use tween.Completed

2 Likes

That’s right, tween.Completed:Wait() is better.

Alternatively, only two tweens can be used. Further parameters in tween info include repeat count (-1 is infinite) and reverse. So you play once and let the tween transition in and out continuously absent from any loops.

If you don’t mind, I’d also like to mention that while-loops are more efficient than recursion in this case.

Loops are normally more efficient in terms of memory usage and performance for iterative tasks, and recursive functions can be more suitable for some types of problems - depends on the specific problem at hand and the trade-offs between efficiency and code readability.

Recursive functions create a new stack frame for each call, so they consume more memory depending on how deep recursion is. Which means loops have a more straightforward execution flow and constant memory usage.

Nevertheless, recursion can be great for tree and recursive structures with subproblems that need similar logic done in the function. Hence they can be more elegant, understandable and maintainable in such cases.

2 Likes

Hmm how could i change the color 2 times on a tween

So with two times you mean three colors? In that case, three tweens.

Example.

while true do
    tweenBlue:Play()
    tweenBlue.Completed:Wait()
    tweenGreen:Play()
    tweenGreen.Completed:Wait()
    tweenRed:Play()
    tweenRed.Completed:Wait()
end

Whereas with two colors you can have a single tween for each object, and play a single time.

local tween = TweenService:Create(
    guiObject,
    TweenInfo.new(
        1, Enum.EasyingStyle.Sine, Enum.EasyingDirection.InOut, -1, true
    ),
    {BackgroundColor3 = Color3.new(60, 60, 60)}
)
guiObject.BackgroundColor3 = Color3.new(30, 30, 30)
tween:Play()

In the above example, the tween is going to transition from original color to another one and back.

Don’t do this because every call will add one to the stack and eventually overflow it