I don’t believe TimeOfDay
can be Tweened because it’s a string, so it might be more ideal to use the ClockTime
property which is a number.
Aside from that, the rest of the properties you mentioned are able to be Tweened – here’s explanations & examples of how it could be accomplished, utilizing some of the end goals mentioned in your post (however, I am not sure when you want one of the properties to Tween to another value, so I grouped them together in a way that made more sense)
Explanation
In order to utilize Tweens, we first need to access the TweenService
by creating a variable for it and any other important Instances that will be used:
local Lighting = game:GetService("Lighting") -- Instance that will be Tweened
local TweenService = game:GetService("TweenService") -- Service that allows us to use Tweens
local ClickDetector = script.Parent.ClickDetector -- ClickDetector that will activate the function
Afterward, we can continue by defining how the Tween
will act, including the amount of time it takes to reach the end goal, if it will repeat after completing, etc.
This is important to ensure that it will achieve the intended effect in an appropriate amount of time while also having a smooth transition.
local TweenProperties = TweenInfo.new( -- This contains properties that will determine how the Tween will be completed
10, -- Duration of time (in seconds) to complete the Tween
Enum.EasingStyle.Linear, -- EasingStyle (How it moves)
Enum.EasingDirection.In, -- EasingDirection (Direction it moves)
0, -- How many times it will repeat
false, -- If the Tween reverses back to its original state
0 -- Delay before starting the Tween
)
From here, we’ll create some tables that will establish what the end goal of each Tween should reach by the time it has concluded.
The name of the property that will be tweened goes on the left side, and the value it will reach by the end of the Tween will go on the right. A comma separates each entry as to distinguish it from one another.
local Goal1 = { -- Creates a table that determines the end goal of the first Tween
-- Once the Tween is over, the ClockTime property will be 0.5, and Brightness will be 0
ClockTime = 0.5,
Brightness = 0
}
local Goal2 = { -- Creates a table that determines the end goal of the second Tween
-- Once the Tween is over, the ClockTime property will be 24, and the Ambients will reach a Color3 value of 0,0,0
ClockTime = 24,
Ambient = Color3.new(0,0,0),
OutdoorAmbient = Color3.new(0,0,0)
}
Finally, the Tween can be created with the information that was defined in the codeblocks above. There are three arguments that will be referenced when creating a Tween:
- The
Instance
that will be changed
- The properties of the Tween/
TweenInfo
- The end goal of the Tween
--[[ This creates a Tween for the Lighting service. How the Tween acts will be
determined by the "TweenProperties" & where it ends up is defined by "Goal1".
Both of these are referencing what we created in the codeblocks above --]]
local firstTween = TweenService:Create(Lighting, TweenProperties, Goal1)
firstTween:Play() -- Plays the Tween with the given information above
From there, it can be put together and made cohesive in a full script:
Example Code
local Lighting = game:GetService("Lighting")
local TweenService = game:GetService("TweenService")
local ClickDetector = script.Parent.ClickDetector
local TweenProperties = TweenInfo.new( -- This contains properties that will determine how the Tween will be completed
10, -- Duration of time (in seconds) to complete the Tween
Enum.EasingStyle.Linear, -- EasingStyle (How it moves)
Enum.EasingDirection.In, -- EasingDirection (Direction it moves)
0, -- How many times it will repeat
false, -- If the Tween reverses back to its original state
0 -- Delay before starting the Tween
)
local Goal1 = { -- Creates a table that determines the end goal of the first Tween
-- This means that once the Tween is over, the ClockTime property will be 0.5, and Brightness will be 0
ClockTime = 0.5,
Brightness = 0
}
local Goal2 = { -- Creates a table that determines the end goal of the second Tween
-- I would advise adjusting the properties within the different goals so it makes more sense when swapping between the two (so the brightness is greater during the daytime, lower during the nighttime, etc.)
ClockTime = 24,
Ambient = Color3.new(0,0,0),
OutdoorAmbient = Color3.new(0,0,0)
}
local debounce = false -- Debounce variable to prevent the Tweens from playing again until they've been completed
local function onClicked()
if not debounce then -- If the Tweens are not currently active, then...
debounce = true -- The variable will be set to true so that this part of the function will not be run until the Tweens have concluded
local firstTween = TweenService:Create(Lighting, TweenProperties, Goal1)
--[[ Creates a Tween for the Lighting service, utilizing the properties outlined
in the "TweenProperties" with the goal of transitioning the properties of the
Lighting service to the values in the "Goal1" table --]]
local secondTween = TweenService:Create(Lighting, TweenProperties, Goal2)
firstTween:Play() -- This activates the first tween...
firstTween.Completed:Wait() -- And this will ensure that the script waits until the first Tween is completed before continuing
secondTween:Play() -- The second Tween will then be activated afterward
secondTween.Completed:Wait() -- And then this will wait until it has concluded
debounce = false -- Sets the debounce back to false so that this section of the function will be able to run again/the Tweens can be replayed
end
end
ClickDetector.MouseClick:Connect(onClicked)
For more information, please refer to the Developer Hub pages listed below:
Developer Hub Pages