TweenService not working

The script I’m currently using to attempt to change the color of a beam doesn’t seem to work.

I even used print statements, and they don’t show up in the output view.

It is meant to turn the beam fully white when I click, and it should fade to black when I release the mouse. (MouseButton1)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local spotlightModel = workspace.Spotlights
local spotlights = {
	spotlightModel.Spotlight1.Arm.Head.Lens.AttG.Beam,
	spotlightModel.Spotlight2.Arm2.Head2.Lens.AttG.Beam,
}

local startColor = ColorSequence.new(Color3.fromRGB(255,255,255),Color3.fromRGB(255,255,255))
local endColor = ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(0,0,0))

local fadeTime = 0.3

local lightAnimations = {}

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

function fade(Beam)
	local tween = lightAnimations[Beam]

	if lightAnimations[Beam] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then
			tween:Cancel()
		end

		tween:Play()
	end
end

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

	if lightAnimations[Beam] then
		if tween.PlaybackState == Enum.PlaybackState.Playing then
			tween:Cancel()
		end
	end

	Beam.Color = startColor
	print("on")
end)

game.ReplicatedStorage.SpotlightStop.OnServerEvent:Connect(function(player, Beam)
	fade(Beam)
	print("off")
end)

Are there any errors?
(For example, an “Argument 2 missing or nil” error for your TweenInfo.new)

No, it just doesn’t put a statement at all.

Ok, good to know!
I’ll look at your code and see if there’s any errors

1 Like

So, while these probably aren’t the causes of the problem, these should probably be changed because they may not work depending on what scripts you have.
First, use if tween then rather than if lightAnimations[Beam] then because it increases readability.
Also, if you have an anti-cheat script, it may change the names of the services. I’d recommend using game:GetService("ReplicatedStorage") instead of game.ReplicatedStorage

Changing them didn’t change much, as expected.

Try printing in your client code.
Maybe there’s an error there, not on the server.

Well, it’s a reused code from another script for making parts change their BrickColor, but these use Color3 values I believe.

Maybe there’s some things I have to remake for it to work.

The error I seem to get when I try out the code is, “TweenService:Create property named ‘Color’ on object ‘Beam’ is not a data type that can be tweened”.

Ohhh you can’t tween ColorSequences, you need to use loops and set the Color property

local beam = game.Workspace.Spotlights.Spotlight1.Arm.Head.Lens.AttG.Beam

game.ReplicatedStorage.SpotlightStart.OnServerEvent:Connect(function(player, beam)
	beam.Color = ColorSequence.new(Color3.fromRGB(255,255,255),Color3.fromRGB(255,255,255))
end)

game.ReplicatedStorage.SpotlightStop.OnServerEvent:Connect(function(player, beam)
	beam.Color = ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(0,0,0))
end)

I managed to fix the issue, and the beam now appears and disappears, but how do I gradually fade white to black after I release the mouse with color sequences?

Use for i = 1, 10 do, then add a task.wait(0.1)
In the loop you should set your color3 in the colorsequence to either 255 - (255 * (i/10))
or 255 * (i / 10)

I’m assuming it’s because I’m modifying the beam’s color wrong, but I’m not sure.
I’ve tried to look at the output view in studio to see if it throws an error, but even though it doesn’t, it still doesn’t work.
Also, my script is in the workspace of the game.
Like I said, I’m pretty new to scripting, so any and all help is appreciated.
Thank you!
Edit:
Here is my script for the Server Script:
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ServerStorage = game:GetService(“ServerStorage”)

local Players = game:GetService(“Players”)

local spotlightModel = workspace.Spotlights
local spotlights = {
spotlightModel.Spotlight1.Arm.Head.Lens.AttG.Beam,
spotlightModel.Spotlight2.Arm2.Head2.Lens.AttG.Beam,
}

local startColor = ColorSequence.new(Color3.fromRGB(255,255,255),Color3.fromRGB(255,255,255))
local endColor = ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(0,0,0))

local fadeTime = 0.3

local lightAnimations = {}

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

function fade(Beam)
local tween = lightAnimations[Beam]

if lightAnimations[Beam] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end

tween:Play()
end
end

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

if lightAnimations[Beam] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end

Beam.Color = startColor
end)

game.ReplicatedStorage.SpotlightStop.OnServerEvent:Connect(function(player, Beam)
fade(Beam)
end)

Edit 2:
I’ve tried using “Beam.Parent.Parent.Parent.Parent.Color”, but it still doesn’t work.
I’ve also tried “Beam.Parent.Parent.Parent.Parent.Parent.Color”, and it still doesn’t work.
Edit 3:
Here’s the script for the LocalScript.
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local spotlightModel = workspace.Spotlights
local spotlights = {
spotlightModel.Spotlight1.Arm.Head.Lens.AttG.Beam,
spotlightModel.Spotlight2.Arm2.Head2.Lens.AttG.Beam,
}

local startColor = ColorSequence.new(Color3.fromRGB(255,255,255),Color3.fromRGB(255,255,255))
local endColor = ColorSequence.new(Color3.fromRGB(0,0,0),Color3.fromRGB(0,0,0))

local fadeTime = 0.3

local lightAnimations = {}

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

function fade(Beam)
local tween = lightAnimations[Beam]

if lightAnimations[Beam] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end

tween:Play()
end
end

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

if lightAnimations[Beam] then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end

Beam.Color = startColor
end)

game.Replicated