How to create a smooth strobe effect?

This is what I’m trying to achieve:

You see how the edges of the black part change from blue to red to blue and so on?
I’d like to do that, but with the color3 of all the parts in an array. I’ve tried putting a tween inside of a for loop that loops through the array, and put that inside of a while true loop. I don’t think it’s very performant though. Any alternative methods?

Thanks!

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()
2 Likes

StopStrobeTweenForPart never gets called…

Edit: Otherwise, your code works! Amazing! I’ve modified it into my updated case:

Code for copy paste
-- -- -- -- --

local StrobeDuration = 1
local ColorA, ColorB = Color3.fromRGB(0, 170, 255), Color3.fromRGB(170, 0, 0)

local HighlightTween:Tween = nil

local function CreateStrobeTweenForHighlight(Highlight:Highlight, Duration:number, ColorA:Color3, ColorB:Color3):()
	local StrobeTweenInfo = TweenInfo.new(Duration, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, -1, true)
	HighlightTween = TweenService:Create(Highlight, StrobeTweenInfo, {OutlineColor = ColorB})

	Highlight.OutlineColor = ColorA
	HighlightTween:Play()

end

local function StopStrobeTweenForHighlight():()
	HighlightTween:Cancel()
	HighlightTween:Destroy()
end

function module.selectSelectButton()
	
	CreateStrobeTweenForHighlight(partsHighlight, StrobeDuration, ColorA, ColorB)
	ContextActionService:BindAction("clickToSelect", SelectWhenClicked, false, Enum.UserInputType.MouseButton1)

end

function module.deselectSelectButton()
	
	StopStrobeTweenForHighlight()
	ContextActionService:UnbindAction("clickToSelect")

end

return module

Quick question though: I’d like to see how a rainbow effect would look. How could I do that?
I’m thinking to do something like this:

Highlight.OutlineColor = Color3.fromHSV(valueThatRollsOver, 1, 1)

Thanks!

Edit 3: I ended up using this: How to create a simple rainbow effect using TweenService

This has become useful once again, THANK YOU SO MUCH :pray: :pray: :pray: