An alternative method would be to utilize the repeatCount: number
and reverses: boolean
parameters (the 4th and 5th, respectively) of the TweenInfo
constructor in a way to keep the tween repeating forever and reversing itself on each repeat. Passing -1
into the repeatCount: number
parameter causes the tween to repeat indefinitely. Passing true
into the reverses: boolean
parameter causes the tween to tween back to its original property value on each repeat, using the same TweenInfo
.
To get a strobe effect, create a reversing Tween
that repeats infinitely. Before playing it, set the part’s Color
to the first color you want (e.g., blue), then play the Tween
after the initial color is set. The Tween
will now alternative between the first color you set and the color that is in the property table of the Tween
. It will keep repeating forever until you cancel the Tween
.
Additionally, I recommend running these these tweens on the client to save server resources.
In this demonstration, there are two parts parented under workspace, one named “Part1” and the other, “Part2”
local TweenService = game:GetService("TweenService")
local PartsArray: {BasePart} = {workspace.Part1, workspace.Part2}
local StrobeDuration = 1
local ColorA, ColorB = Color3.fromRGB(0, 170, 255), Color3.fromRGB(170, 0, 0)
local StrobeTweens: {[BasePart]: Tween} = {}
local function CreateStrobeTweenForPart(Part: BasePart, Duration: number, ColorA: Color3, ColorB: Color3): Tween
local StrobeTweenInfo = TweenInfo.new(Duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, -1, true)
local Tween = TweenService:Create(Part, StrobeTweenInfo, {Color = ColorB})
Part.Color = ColorA
Tween:Play()
StrobeTweens[Part] = Tween
return Tween
end
local function CreateStrobeTweensForArray(PartsArray: {BasePart}, Duration: number, ColorA: Color3, ColorB: Color3): ()
for Index, Part in ipairs(PartsArray) do
CreateStrobeTweenForPart(Part, Duration, ColorA, ColorB)
end
end
local function StopAllStrobeTweens(): ()
for Part, Tween in StrobeTweens do
Tween:Cancel()
Tween:Destroy()
end
table.clear(StrobeTweens)
end
local function StopStrobeTweenForPart(Part: BasePart): ()
local Tween = StrobeTweens[Part]
if Tween == nil then return end
Tween:Cancel()
Tween:Destroy()
StrobeTweens[Part] = nil
end
task.wait(3)
CreateStrobeTweensForArray(PartsArray, StrobeDuration, ColorA, ColorB)
task.wait(15)
StopAllStrobeTweens()