Scripting Issue

So I’m using this script to tween a parts color, however, it keeps flashing the colors instead of tweening them nicely. Here’s the script:

local CurrentColor = Color3.new(1, 0, 0)
local TweenService = game:GetService("TweenService")
local TweenTime = 0.5
local TweenInfoX =  TweenInfo.new(TweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local Counter = 1
script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
	task.spawn(function()
		while task.wait() do
			task.wait()
			if Counter == 1 then
				CurrentColor = Color3.new(1, 0, 0)
			elseif Counter == 2 then
				CurrentColor = Color3.new(0.835294, 0.45098, 0.239216)
			elseif Counter == 3 then
				CurrentColor = Color3.new(1, 1, 0)
			elseif Counter == 4 then
				CurrentColor = Color3.new(0, 255, 0)
			elseif Counter == 5 then
				CurrentColor = Color3.new(18, 238, 212)
			elseif Counter == 6 then
				CurrentColor = Color3.new(255, 0, 191)
			end
			task.wait()
			local A = TweenService:Create(script.Parent, TweenInfoX, {Color = CurrentColor})
			local B = TweenService:Create(script.Parent:FindFirstChild("Beak"), TweenInfoX, {Color = CurrentColor})
			local C = TweenService:Create(script.Parent:FindFirstChild("Upper Torso"), TweenInfoX, {Color = CurrentColor})
			A:Play()
			B:Play()
			C:Play()
			task.wait(1)
			if Counter >= 6 then
				Counter = 0
			end
			if script.Parent.Color ~= CurrentColor then
				script.Parent.Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Beak").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Beak").Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Upper Torso").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Upper Torso").Color = CurrentColor
			end
			Counter += 1
		end
	end)
end)

Are you sure this section isnt the Issue?

Since the Color is changing, it may be detecting that it has changed, so it attempts to apply the color

1 Like

No, I added that after it not working. It was a test, but issue still happens without it.


Heres a video of it happening, FLASH WARNING, by the way.

I could be this:

Not sure.

Text

1 Like

Then what would be a work around? Because it needs to be in the loop.

What exactly is it used for? Whats what im wondering

(Scratch that, i looked at the script)

1 Like

Have you tried Tween.Completed?

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
	task.spawn(function()
		while task.wait() do
			task.wait()
			if Counter == 1 then
				CurrentColor = Color3.new(1, 0, 0)
			elseif Counter == 2 then
				CurrentColor = Color3.new(0.835294, 0.45098, 0.239216)
			elseif Counter == 3 then
				CurrentColor = Color3.new(1, 1, 0)
			elseif Counter == 4 then
				CurrentColor = Color3.new(0, 255, 0)
			elseif Counter == 5 then
				CurrentColor = Color3.new(18, 238, 212)
			elseif Counter == 6 then
				CurrentColor = Color3.new(255, 0, 191)
			end
			task.wait()
			local A = TweenService:Create(script.Parent, TweenInfoX, {Color = CurrentColor})
			local B = TweenService:Create(script.Parent:FindFirstChild("Beak"), TweenInfoX, {Color = CurrentColor})
			local C = TweenService:Create(script.Parent:FindFirstChild("Upper Torso"), TweenInfoX, {Color = CurrentColor})
			A:Play()
			B:Play()
			C:Play()
			A.Completed:Wait() -- yields code until the Tween is completed
			if Counter >= 6 then
				Counter = 0
			end
			if script.Parent.Color ~= CurrentColor then
				script.Parent.Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Beak").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Beak").Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Upper Torso").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Upper Torso").Color = CurrentColor
			end
			Counter += 1
		end
	end)
end)

Unfortunately still has the same issue.

Try this

local CurrentColor = Color3.new(1, 0, 0)
local TweenService = game:GetService("TweenService")
local TweenTime = 0.5
local TweenInfoX =  TweenInfo.new(TweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local Counter = 1

function CheckCounter()
if Counter == 1 then
return Color3.new(1, 0, 0)
elseif Counter == 2 then
return Color3.new(0.835294, 0.45098, 0.239216)
elseif Counter == 3 then
return Color3.new(1, 1, 0)
elseif Counter == 5 then
return Color3.new(18, 238, 212)
elseif Counter == 6 then
return CurrentColor = Color3.new(255, 0, 191)
end
end)

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
task.spawn(function()
CurrentColor = CheckCounter()

local A = TweenService:Create(script.Parent, TweenInfoX, {Color = CurrentColor})
local B = TweenService:Create(script.Parent:FindFirstChild("Beak"), TweenInfoX, {Color = CurrentColor})
local C = TweenService:Create(script.Parent:FindFirstChild("Upper Torso"), TweenInfoX, {Color = CurrentColor})
A.Completed:Wait()
if Counter >= 6 then
				Counter = 0
			end
			if script.Parent.Color ~= CurrentColor then
				script.Parent.Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Beak").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Beak").Color = CurrentColor
			end
			if script.Parent:FindFirstChild("Upper Torso").Color ~= CurrentColor then
				script.Parent:FindFirstChild("Upper Torso").Color = CurrentColor
			end
			Counter += 1
		end
end)
end)




It doesn’t run, like the colors don’t tween at all. (Yes I fixed the errors and tested)

I forgot to add the Play, LOL

1 Like

Hi, I wouldn’t suggest all those if statements, instead:

local Colors = {Color3.new(1, 0, 0),Color3.new(0.835294, 0.45098, 0.239216)--ect.}
-- To define the color:
{Color = Colors[Counter]}

I haven’t checked all the replies, but this just caught my attention.

1 Like

Yeah, I know, I just copy & pasted, im too lazy rn :frowning:

It stops at red. So it tweens once then stops, I’d assume its because the ‘:Wait’?

This is a server-sided script by the way.

Don’t be lazy when helping others out. This will let the bad practice proceed to exist for newer developers. :slight_smile:

rn im multi tasking, sorry if i do questionable things

Regardless it wouldn’t change anything but efficiency, I just need the script to work. I can fix it up later.

local CurrentColor = Color3.new(1, 0, 0)
local TweenService = game:GetService("TweenService")
local TweenTime = 0.5
local TweenInfoX =  TweenInfo.new(TweenTime, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local Counter = 1
local Colors = {Color3.new(1, 0, 0), Color3.new(0.835294, 0.45098, 0.239216), Color3.new(1, 1, 0), Color3.new(0, 255, 0), Color3.new(0.239216, 0.466667, 1), Color3.new(255, 0, 191)}
local D = 0

script.Parent:GetPropertyChangedSignal("Parent"):Connect(function()
	if D == 0 then
		D += 1
		task.wait()
		task.spawn(function()
			while task.wait() do
				CurrentColor = Colors[Counter]
				local A = TweenService:Create(script.Parent, TweenInfoX, {Color = CurrentColor})
				local B = TweenService:Create(script.Parent:FindFirstChild("Beak"), TweenInfoX, {Color = CurrentColor})
				local C = TweenService:Create(script.Parent:FindFirstChild("Upper Torso"), TweenInfoX, {Color = CurrentColor})
				A:Play()
				B:Play()
				C:Play()
				A.Completed:Wait()
				if Counter >= 6 then
					Counter = 0
				end
				if script.Parent.Color ~= CurrentColor then
					script.Parent.Color = CurrentColor
				end
				if script.Parent:FindFirstChild("Beak").Color ~= CurrentColor then
					script.Parent:FindFirstChild("Beak").Color = CurrentColor
				end
				if script.Parent:FindFirstChild("Upper Torso").Color ~= CurrentColor then
					script.Parent:FindFirstChild("Upper Torso").Color = CurrentColor
				end
				Counter += 1
			end
		end)
	end
end)

This is what I have at the moment, I thought maybe the parent was changing multiple times and maybe the loops runs more than once (So I added the ‘D’ constant), but that doesn’t seem to be it.

But I do believe that the loop is running more than once. I’m not sure why.