Color looping script not looping

Hey, this script is not looping, it seems to only change the color only one time!

Here’s the script:

script.Parent.Touched:Connect(function(player)
for count = 1, 10 do
		local CurrectColor = Color3.fromRGB(162, 255, 56)
		local OtherColor = Color3.fromRGB(255, 0, 4)

	 local R, B, G = CurrectColor.R, CurrectColor.G, CurrectColor.B
	 local A, B, C = OtherColor.R, OtherColor.G, OtherColor.B

	CurrectColor = Color3.new(R+A, G+B, B+C)
	script.Parent.Color = CurrectColor
	task.wait(1)
end
	
	
end)
1 Like

The code you provided will loop 10 times, setting the colour to the same value each time…

Are you wanting to have it loop repeatedly after being touched once?

I want it to be like a rainbow effect but only change colors every 1 second and only for 10 seconds…

Rainbow effect, or just slowly move from CurrentColor to OtherColor?

One way I can think of achieving this is converting the colors to vectors and using the :Lerp function of vectors:

local current = Color3.fromRGB(162, 255, 56)
local other = Color3.fromRGB(255, 0, 4)
local start = Vector3.new(current.R, current.G, current.B)
local goal = Vector3.new(other.R, other.G, other.B)

local steps = 10
for i = 1, steps do
	local alpha = i/steps
	local lerp = start:Lerp(goal, alpha)
	local color = Color3.new(lerp.X, lerp.Y, lerp.Z)
	print(color)
end

rainbox effect, like colors looping…

Here’s a quick script to produce a rainbow effect. This is a basic script and you will need to modify it to fit your task.

local hue = 0
local part = --[[part here]]

while true do
	hue += 1
	local nextHue = (hue % 360) / 360
	
    part.Color = Color3.fromHSV(nextHue, 1, 1)
    task.wait(0.1)
end

Or, lerp it if you need to have more control over it:

--this code can be more easily modified to control the linear interpolation
local part = script.Parent

local function lerpHue(a: number, b: number, c: number): number
	return (a + (b - a) * c) / 360
end

for i = 1, 360, 1 do
	local alpha = i / 360
	local hue = lerpHue(1, 360, alpha)
	
	part.Color = Color3.fromHSV(hue, 1, 1)
	task.wait(0.1)
end



Color3 also has the :Lerp function.

1 Like

The first script works, but then after a few seconds it resets and goes through the loop again… Is there a way to make it smoother?

Odd, I’m not having that issue with the script. How did you implement the code? When it reaches hue 360, it resets to hue 0, which looks almost identical.

Used this script:

local hue = 0
local part = script.Parent

	while true do
	hue += 1
	local nextHue = (hue % 360) / 360

	part.Color = Color3.fromHSV(hue, 1, 1)
	task.wait(0.1)
end

The thing it reaches purple and resets to red. I want it to be like a smooth transition not an instant one…

That’s my bad, I used the wrong hue variable in my code snippet. I’ve updated it in the main post. Corrected code:

local hue = 0
local part = script.Parent

while true do
	hue += 1
	local nextHue = (hue % 360) / 360

	part.Color = Color3.fromHSV(nextHue, 1, 1)
	task.wait(0.1)
end
1 Like

It works now! Thanks for the help!

1 Like
local colors = {
	Color3.fromRGB(255, 0, 0),
	Color3.fromRGB(255, 127, 0),
	Color3.fromRGB(255, 255, 0),
	Color3.fromRGB(0, 255, 0),
	Color3.fromRGB(0, 0, 255),
	Color3.fromRGB(75, 0, 130),
	Color3.fromRGB(148, 0, 211)
}

local db = true
script.Parent.Touched:Connect(function()
	if db then db = false
		for i = 1, #colors do
			script.Parent.Color = colors[i]
			task.wait(1)
		end db = true
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.