How can I tween an existing rainbow ParticleEffect script, that is made for a part and rescript it to be able to make a rainbow gradient ColorSequence? The sample script is below:
local tweenService = game:GetService("TweenService")
local part = script.Parent
local tweeningInformation = TweenInfo.new(
5, -- Length
Enum.EasingStyle.Linear,
Enum.EasingDirection.InOut,
0, -- Number of times the tween will repeat
false, -- Tween repeats?
0 -- Delay
)
local green = {
Color = Color3.new(0.666667, 1, 0.647059)
}
local red = {
Color = Color3.new(1, 0.486275, 0.580392)
}
local blue = {
Color = Color3.new(0.486275, 0.847059, 1)
}
local white = {
Color = Color3.new(1, 1, 0.866667)
}
local Tween = tweenService:Create(part,tweeningInformation,green)
local redT = tweenService:Create(part,tweeningInformation,red)
local blueT = tweenService:Create(part,tweeningInformation,blue)
local whiteT = tweenService:Create(part,tweeningInformation,white)
while true do
Tween:Play()
wait(5)
redT:Play()
wait(4.5)
blueT:Play()
wait(5)
whiteT:Play()
end
I used this from another DevForum post, can’t remember where from at the mo:
local RS = game:GetService("RunService")
local rainbow = script.Parent -- GUI object
local grad = rainbow.UIGradient
local counter = 0 -- phase shift {def = 0}
local w = math.pi / 50 -- frequency [the lower the number the tighter the rainbow] {def = 12}
local CS = {} -- colorsequence table
local num = 15 -- number of colorsequence points (maxed at 16) [provides a more granular rainbow] {def = 15}
local frames = 0 -- frame counter, used to buffer if you want lower framerate updates {def = 0}
local count = 0
local cskCache = {}
while true do
-- build a ColorSequenceKeyPoint
for i = 0, num do
local c = Color3.fromRGB(127 * math.sin(w*i + counter) + 128, 127 * math.sin(w*i + 2 * math.pi/3 + counter) + 128, 127*math.sin(w*i + 4*math.pi/3 + counter) + 128)
table.insert(CS, i+1, ColorSequenceKeypoint.new(i/num, c))
end
local newCS = ColorSequence.new(CS)
-- build the cache
if #cskCache > 0 then
if newCS == cskCache[1] then
CS = {}
break
end
end
table.insert(cskCache, newCS)
-- clear out the CS table
CS = {}
-- this counter effectively sets the total cache to 80 ColorSequences (countStart = 0, countIncrement = math.pi/40, countMax = 2*math.pi)
counter = counter + math.pi/40
if (counter >= math.pi * 2) then counter = 0 end
end
local finalCacheCt = #cskCache
local rotation = 1
RS.Heartbeat:Connect(function()
if math.fmod(frames, 2) == 0 then
-- set the new gradient.
grad.Color = cskCache[rotation]
-- reset rotation once we have iterated through all the frames
if rotation >= #cskCache then rotation = 0 end
-- move to the next frame
rotation = rotation + 1
end
-- controls when the frame fires based off the fmod() above. We reset to prevent the number from endlessly increasing
if frames >= 1000 then frames = 0 end
frames = frames + 1
end)
Localscript & UIGradient are both children of the item you want the rainbow effect to appear on.Works well