Little trick here, one of the most underrated method in my opinion, TweenService:GetValue(a). With that function, is is possible to make custom tween systems, that still allowing you to use the Enum.EasingStyle of your choise (and EasingDirection as well)
In your case, using TweenService for doing a rainbow animation, doesn’t work very well, since TweenService is built for tweening a properties from an inital value, to a goal value. In your case, the initial value and goal value could be the same (if it does 1 cycle of the rainbow), so you would have to separate the tween into smaller chunks to avoid this issue, …
(@2112Jay’s solution does that perfectly, it’s simpler than my method)
Or you can use TweenService:GetValue(a) like this. This is for a local script btw
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Part = workspace:WaitForChild("AwesomePart")
local function TweenCube(Cube, TweenParams)
local StartTick = os.clock()
local InitialSize = Cube.Size
local InitialTransparency = Cube.Transparency
while true do
-- Alpha starts at 0, and goes to 1 gradually, after TweenParams.TweenTime amount of time
local alpha = (os.clock() - StartTick)/TweenParams.TweenTime
alpha = math.clamp(alpha, 0, 1) -- Prevent alpha from being bigger than 1
-- TweenService:GetValue() modifies alpha to make it follow the easing style and direction
-- mod_alpha still goes from 0 to 1
local mod_alpha = TweenService:GetValue(alpha, TweenParams.EasingStyle, TweenParams.EasingDirection)
-- Manually set the properties of the cube, using alpha to smoothly transition from the initial value to the goal value
Cube.Size = InitialSize:Lerp(Vector3.new(6,6,6), mod_alpha)
Cube.Transparency = math.lerp(InitialTransparency, 0, mod_alpha)
-- Magic part, we aren't restricted to initial value -> goal value, we can instead use a function that returns a color depending on alpha
-- alpha 0 -> red, alpha 0.5 -> Cyan, alpha 1 -> red
Cube.Color = Color3.fromHSV(mod_alpha, 1, 1)
-- You can do this to make it loop through the rainbow twice during the tween, similar to your original code
--Cube.Color = Color3.fromHSV(mod_alpha*2 % 1, 1, 1)
-- Reached the end of the tween, we can break out of the loop
if alpha == 1 then break end
RunService.RenderStepped:Wait()
end
end
--
task.wait(5)
local TweenParams = {
TweenTime = 2, -- Amount of seconds
EasingStyle = Enum.EasingStyle.Cubic,
EasingDirection = Enum.EasingDirection.Out,
}
TweenCube(Part, TweenParams)