Trying to fix color tweening

I have a script that changes a part’s color based on a keybind I press (currently the “F” key)

However, when I press the key, it immediately changes to the new color and then fades to black.
I want it to fade into the new color, whilst still fading out.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local lights = {
	workspace.Part
}

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

local fadeTime = 0.5

local lightAnimations = {}

for _, light in lights 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
			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
			tween:Cancel()
		end
	end

	light.Color = startColor
end)

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

What can be added or changed in this script to make it fade in as well?

1 Like