BrickColor fade with TweenService

It’s normal.
When I press, it prints Light started, if I release, it prints Light ended, yet it still doesn’t fade.

The LocalScripts are controlling the inputs for remote events.

The script I’m changing at the moment is a server script.

Strange! When I use the code below (Script is inside a part alongside a ClickDetector)…

local tween = game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(1), {Color = Color3.new(0, 0, 0)})

script.Parent.ClickDetector.MouseClick:Connect(function()
	tween:Play()
	tween.Completed:Wait()
	print("Completed")
end)

… it tweens completely fine. Maybe only try the tweening with a similar setup (ClickDetector). If it works then, you know your problem.

This should do:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local light = game.Workspace.Lights.LightBTM.light
local startColor = BrickColor.White()
local endColor = BrickColor.Black()

local fadeTime = 0.3

local t = game:GetService("TweenService"):Create(light, TweenInfo.new(fadeTime),{Color = Color3.new(0, 0, 0)})

function fade()
	if t.PlaybackState == Enum.PlaybackState.Playing then -- This eliminates the fading variable.
		return
	end
	
	t:Play()
end

game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player,light)
	if t.PlaybackState == Enum.PlaybackState.Playing then
		t:Cancel()
	end
	
	light.BrickColor = BrickColor.White()
end)

game.ReplicatedStorage.LightStop.OnServerEvent:Connect(function(player,light)
	fade()
end)

Modifications:

  • Removed the following, for it can be replaced by checking the Tween’s PlaybackState
local fading = false

...

if fading then return end

...

t.Completed:Connect(function(player,light)
	fading = false
end)
  • Moved the creation of a tween outside fade. Tweens are reusable.
  • Added a check in the LightStart OnServerEvent, which, when the Tween is still playing, cancels the Tween.

EDIT: ah whoops, wrong person replied to

Still not working somehow, it still immediately changes to black instead of fading…

It also doesn’t take the fadeTime into consideration, it just ends instantly.

I can’t seem to reproduce the issue, even with a somewhat accurate reproduction scene.

Try a longer fadeTime and see if it will work better. If it did, it might just be lag.

I cleaned up your code a bit more.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local light = game.Workspace.Lights.LightBTM.light
local startColor = Color3.new(1, 1, 1)
local endColor = Color3.new(0, 0, 0)

local fadeTime = 0.3

local t = game:GetService("TweenService"):Create(light, TweenInfo.new(fadeTime),{Color = endColor})

function fade()
	if t.PlaybackState == Enum.PlaybackState.Playing then -- This eliminates the fading variable.
		return
	end
	
	t:Play()
end

game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player,light)
	if t.PlaybackState == Enum.PlaybackState.Playing then
		t:Cancel()
	end
	
	light.Color = startColor
end)

game.ReplicatedStorage.LightStop.OnServerEvent:Connect(function(player,light)
	fade()
end)

It still ignores the fade time.

I made it 5 seconds, it ends immediately.

Do you have other scripts that interfere with your light script? Or any other scripts at all?

Well, I have 3 other scripts that should use the same remote events, since there are 3 lights that should do the same thing.

Here’s a video of them.

Also, each light is enabled using KeypadOne, KeypadTwo, and KeypadThree (bottom, center, top)
And each LocalScript tied to these 3 parts, only look into their corresponding models, so it shouldnt interfere.

It could possibly be that the scripts are canceling the animations too early whenever you press any of those three keys in quick succession. Scripts that share the same events should “filter” the parameters sent to them, or at least have a way to distinguish them.

To be honest, though, one script is enough. This code represents all the light scripts (ServerScripts) you have:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local lightModel = workspace.Lights
local lights = {
	lightModel.LightTOP.light, -- I guessed this.
	lightModel.LightMDL.light, -- Same with this one. Just change the paths if I am wrong.
	lightModel.LightBTM.light,
}

local startColor = Color3.new(1, 1, 1)
local endColor = Color3.new(0, 0, 0)

local fadeTime = 0.3

local lightAnimations = {}

for _, light in lights:GetChildren() do
	lightAnimations[light] = TweenService:Create(light, TweenInfo.new(fadeTime),{Color = endColor})
end

function fade(light)
	local tween = lightAnimations[light]
	
	if lightAnimations[light] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then -- This eliminates the fading variable.
			tween:Cancel()
		end

		tween:Play()
	end
end

game.ReplicatedStorage.LightStart.OnServerEvent:Connect(function(player, light)
	local tween = lightAnimations[light]

	if lightAnimations[light] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then -- This eliminates the fading variable.
			tween:Cancel()
		end
	end
	
	light.Color = startColor
end)

game.ReplicatedStorage.LightStop.OnServerEvent:Connect(function(player, light)
	fade(light)
end)
1 Like

Should I put this in the LocalScript or ServerScript?

This is the code for your server-side light scripts, except instead of three scripts representing each key, you have one.

Oh, I missed that one. Change this line:

for _, light in lights:GetChildren() do

to this:

for _, light in lights do
1 Like

Finally works! Thanks!! :slight_smile:

Just that little mistake

1 Like

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