How to tween Lighting.ClockTime that is a negative number?

Hi, thanks for taking your time to read my first post on the devforum :smiley: If you have any suggestion for me, don’t be afraid to tell me. I’m willing to learn from my mistakes.

As far as I know, you cannot directly set ClockTime to a negative number. But you can do that to the TimeOfDay property though. I’m wondering if there is any way to tween a negative ClockTime.

I’ve seen a way to convert TimeOfDay to ClockTime on this post but other than that, other solutions is to tween the ClockTime rather than TimeOfDay, which isn’t what I need.

In conclusion, I want to know a way to tween TimeOfDay (not tweening ClockTime) because TimeOfDay allows negative numbers.

Hmmm, I think you might be able to set the ClockTime in a loop. Not sure tho

I’ve tried it, no you cannot set it to negative even with a loop. It will just bring it back to 0. Thanks for trying to help though!

image
I’ve tried to set the TimeOfDay first, and ClockTime set to an - value, did you try doing that?

you can use % to make it go back to 0 when it got past 24

print(25%24) returns 1

I know you can do it that way, but I’m looking for a solution to tween the TimeOfDay without involving ClockTime, because you can only set the TimeOfDay to a negative value.

I don’t understand how that is going to help with this problem. Can you evaluate it? Thanks.

Oh, sorry I’ve miss read that. Now that I read it again I see you know to do that :sweat_smile:

Now that I think about it you should be able to do something like this (if you do print(typeof(game.Lightning.TimeOfDay) it says it’s an string)

TweenService:Create(game.Lightning, TweenInfo.new(), {TimeOfDay = "your:time:ofday"})

I checked the TweenService:Create() devhub page and it didn’t list string as tweenable.

Yep, now that I tried doing it it errored. You might need to make your own tween:

for i = 0, 24, 0.01 do
for x = 0, 60, 1 do
for y = 0, 60, 1 do
game.Lighting.TimeOfDay = tostring(i..":"..x..":"..y)
end
end
end

i put all the commentary in the script
i dont think having a clocktime that is negative is possible so thats why i used mod (%)
you can place this script in a textbox

local cl = math.clamp; -- define clamp

local lerpCT = game.Lighting.ClockTime; -- define the lerped value

local function lerp(a,b,c)

return (b-a)*c; -- my lerp formula

end;

game:GetService("RunService").Heartbeat:Connect(function(m)

local ct = tonumber(script.Parent.Text) or 0; -- if the clocktime on the textbox is correct then ct = textbox number else ct = 0

lerpCT = lerpCT + lerp(lerpCT,ct,m*4); -- lerp the clocktime (basically tween)

game.Lighting.ClockTime = lerpCT%24; -- i dont think you can really do it with a negative number so just use mod

end);

But that just defeats the purpose of tweening? Tweening can have different styles but this is just linear interpolation (lerp). If I have no choice left, I guess I would stick to lerping then. Thanks for the help though!

Thanks for the help but I’m looking towards the “tweening” part of tweening, that is to have multiple styles that I can choose from. As stated above, I would stick to lerping if there is no way to tween the TimeOfDay. And also, you can set TimeOfDay to negative, you just cannot set ClockTime to negative. Thanks for the help though!

Well so there definitely is a better way of doing this but in my example it converts given number into TimeOfDay using formatting. Since you can not tween TimeOfDay, instead you create number value, tween it’s value and add GetPropertyChangedSignal to it but problem with that is that if number has a lot of decimals like 5.128398532475 then you can’t set TimeOfDay to this value, it will break so i used some converting and poor math that i don’t event understand to convert the number value, as i said it’s not the best method but it works with tweening styles.

local Lighting = game:FindService("Lighting")
local TweenService = game:GetService("TweenService")

local Info = TweenInfo.new(
	5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In
)

local function TweenTimeOfDay(EndTime)
	local TimeValue = Instance.new("NumberValue")
	local GetConverted = string.gmatch(Lighting.TimeOfDay, "%d+")
	
	local Hours = tostring(GetConverted())
	local Minutes = tostring(GetConverted())
	local Seconds = tostring(GetConverted())
	
	local CurrentTime = ("%s:%s:%s"):format(Hours, Minutes, Seconds)
	
	TimeValue:GetPropertyChangedSignal("Value"):Connect(function()
		local NewPattern = ("%02i:%02i:%02i") -- Patern for converting
		
		local Dot = string.gmatch(tostring(TimeValue.Value), "%d+")
		
		local NumberBeforeDot = Dot()
		local Decimals = Dot() or "0000" -- or 0000 to add decimals if there are none to prevent errors
		
		NewPattern = string.format(NewPattern,
			NumberBeforeDot,
			string.sub(Decimals, 1, 2)/100 * 60, -- change range from 0-100 to 0-60
			string.sub(Decimals, 2, 3)/100 * 60
		)
		
		print(NewPattern)
		Lighting.TimeOfDay = NewPattern
	end)
	
	local Tween = TweenService:Create(TimeValue, Info, {Value = EndTime})
	Tween:Play()
	Tween.Completed:Wait()
	Tween:Destroy()
end

TweenTimeOfDay(12)
TweenTimeOfDay(-20)
TweenTimeOfDay(50)

In the end, I found 2 solutions to this problem.

Solution 1:

Just change the ClockTime and GeographicLatitude manually without using any plugins. It will take time, but it won’t cause problems with negative ClockTime. I used this solution because it’s easier and the second solution is unstable.

Solution 2 (kinda whack):

Create a proxy number value and then change the TimeOfDay according to the proxy value.

I’ve seen a way to convert TimeOfDay to ClockTime on this post but other than that, other solutions is to tween the ClockTime rather than TimeOfDay, which isn’t what I need.

I made a mistake about this conversion. It isn’t TimeOfDay to ClockTime, rather the opposite way around. I figured out the conversion myself though, and here is the function:

local function CTtoTOD(ct)
	local H = math.floor(ct)
	local M = (ct-H) * 60
	local S = (M-math.floor(M)) * 60
	return string.format("%02i:%02i:%02i",H,M,S)
end

This solution is a whacky one because somehow the sun will go crazy when you constantly change to a negative ClockTime. You’ll see why when you test it yourself. And it’ll sometimes overshoot the target value even though the tween style isn’t Elastic.
But anyways here is the code I wrote to tween negative ClockTime.

local function CTtoTOD(ct)
	local H = math.floor(ct)
	local M = (ct-H) * 60
	local S = (M-math.floor(M)) * 60
	return string.format("%02i:%02i:%02i",H,M,S)
end

local clockTimeProxy = Instance.new("NumberValue")

clockTimeProxy.Changed:Connect(function(val)
	game.Lighting.TimeOfDay = CTtoTOD(val)
end)

clockTimeProxy.Value = game.Lighting.ClockTime
game:GetService("TweenService"):Create(
	clockTimeProxy,
	TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),
	{Value = -9}
):Play()

This is my first post, hope this helps people in the future! :smiley:

2 Likes