How to sync time of day with the server

hello…

I have this script on a local script to change the time of day

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

local timeShift = 50
local waitTime = 1

local amplitudeB = 1
local offsetB = 2

local amplitudeS = 0.2
local offsetS = 0.8

local pi = math.pi

while true do
	local mam = game.Lighting:GetMinutesAfterMidnight() + timeShift -- minutes after midnight
	local ham = mam/60 -- hours after midnight
	local wave = math.cos(ham * (pi / 12) + pi)
	local doubleWave =  math.cos(ham * (pi / 6))

	Lighting.Brightness = amplitudeB * wave + offsetB
	Lighting.ShadowSoftness = amplitudeS * doubleWave + offsetS

	local oaNum = 20 * wave + 80
	Lighting.OutdoorAmbient = Color3.fromRGB(oaNum, oaNum, oaNum)

	local ambientNum = 25 * (wave + doubleWave) + 50
	Lighting.Ambient = Color3.fromRGB(ambientNum, ambientNum, ambientNum)

	local tweenInfo = TweenInfo.new(waitTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local goal = {}
	goal.ClockTime = ham

	local tween = TweenService:Create(Lighting, tweenInfo, goal)
	tween:Play()

	wait(waitTime)
end

But this only appears on the client. I want to do some data store things when the day turns morning, but i cant do that on the client. So i was hoping there was a way to sync up the times to match.

I already thought about moving this script to a server script. But heard its not a very smart choice

Hope to see you in the replies!

You can use remote events to communicate between the client and server, just fire it whenever it is morning. Also will this time of day be the same for everyone? If so i suggest moving it to the server, if not use the method I provided

Well i was gonna use remote events, but they are so easily exploitable

so a safety net i was gonna use was to check if it was actually morning on the server. But i cant since its handled on the client

and i would put it on the server, but i read a lot of post about it. Its not recommended to

Anything on the client is exploitable, again if the time of day is the same for everyone you should set it up on the server. If you still want to implement it on the client I suggest adding checks on the server, what is this data you are trying to save, could I get a little bit more info?

1 Like

the info im trying to save is days, like the current day (Day1,day2,day3) i want to save that

i see no way of doing it, since putting it on the server can make it unstable.

but you said add checks on the server? what would that be. Cause the only check i can think of would be checking if the time of day is the same as the client, but at the moment im just trying to figure out how to make the server recieve the event, check if its actually morning. Then proceed

I see, I guess you can use options like GetServerTimeNow to utilize this, I also found this tutorial that can be helpfull:

At the end though updating it from the server is the easiest way to do this, yes it can cause latency but it is a minor change unless called very frequently which your script doesn’t.

2 Likes

welp oh well

R.I.P people that have trash ping

Thanks for the help!

am i right that you want every client to have the same ClockTime ?
we can create a NumberValue workspace.ClockTime
then have the server script to calculate and update its value (can use tween service like you did)

all clients can then hook up to workspace.ClockTime:GetPropertyChangedSignal(“Value”) and then update their own Lighting.ClockTime

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