How can i sync client and server with Workspace:GetServerTimeNow()?

How can i sync client&server by using this new function, i watched this tutorial: https://www.youtube.com/watch?v=FfCs6ASfcuc

But someone said there is better and more optimized way to do things from it, anyone know how?

1 Like

Checkout this video

it uses GetServerTimeNow() to sync the parts

2 Likes

Uhhh, ik it’s kindof strage but how am i supposed to use this? i mean it’s great tutorial but idk how he shows servertime, could you explain this to me?

I’ll try to explain using RemoteEvents as an example:

When the server fires a RemoteEvent, there’s a delay before the client detects it (this actually happens in any situation where the server needs to replicate something to the client). The duration of the delay depends on the quality of the network connection between the client and the server, and since the network connection quality can be drastically different between one client and another, it’s entirely possible for a client to detect when a RemoteEvent is fired a significant amount of time after a different client

A workaround you could do is for the server to send its current time when firing the RemoteEvent:

-- Server Script in ServerScriptService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage.RemoteEvent

remoteEvent:FireClient(Players.PlayerAdded:Wait(), workspace:GetServerTimeNow())

which would make it possible for the client to calculate how long it took for them to detect when the RemoteEvent was fired:

-- LocalScript in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnClientEvent:Connect(function(serverTime: number)
	local serverTimeNow = workspace:GetServerTimeNow()

	print("Server time when RemoteEvent was fired:", serverTime)
	print("Server time when client detects when the RemoteEvent was fired:", serverTimeNow)
	print("Time taken between firing RemoteEvent and client detects signal:", serverTimeNow - serverTime)
end)

It’s then up to the programmer to decide what to do depending on the value of the time delta. As an example, if a door is opened or closed each time the RemoteEvent is fired, and it does so using an animation that takes a shorter amount of time than it took for a client with a bad network connection to detect, you could choose to bypass the animation and open/close the door instantaneously in-order to keep each client’s experience as synchronized as possible


Note: The delay also occurs when the client needs to replicate something to the server, since the network connection quality affects client-server communication both ways


Another note: The delay is negligible when tesing in Studio, since your PC is acting as both the client and the server, unless you set the Incoming Replication Lag to a value greater-than 0

2 Likes

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