Getting the time since last server response on the client

I want to create a hit registration system, and for that I need to get the time elapsed since the last server response from the client. Here’s my criteria for how the method should be;
It should be a client function that gets the time elapsed since the last server response (This means that :GetNetworkPing() doesn’t apply here since it gets the average time between the last few server responses). It should also update whenever the function is called, so whenever I call the method, it should calculate the time and then return it. It should also be as precise as possible.
If anyone knows a function that fits the criteria well, you can tell me it and I would gladly appreciate it. If you also have an idea on how it could be done, you could tell me and I’ll see if I can make it myself.
Any help is appreciated.

1 Like

Based on what you’ve written, there’s no built-in function that will return the exact time since the server’s last response (such as the arrival of the last packet or network heartbeat), as far as I know. But you could implement a custom system with the following concepts:

  1. The server sends a periodic “ping” (RemoteEvent) to the client.
  2. The client records the local time each time it receives the ping.
  3. When the client wants to know the time since the last response, it subtracts the current time from the recorded time of the last ping.

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerHeartbeat = ReplicatedStorage:WaitForChild("ServerHeartbeat") -- RemoteEvent (must go inside ReplicatedStorage)

while true do 
ServerHeartbeat:FireAllClients(player) 
task.wait(1) -- You can adjust it according to your needs
end

localScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerHeartbeat = ReplicatedStorage:WaitForChild("ServerHeartbeat") -- RemoteEvent (must go inside ReplicatedStorage)

local lastServerResponse = DateTime.now()

ServerHeartbeat.OnClientEvent:Connect(function() 
lastServerResponse = DateTime.now()
end)

functions GetTimeSinceLastServerResponse()
return DateTime.now() - lastServerResponse
end

-- Example usage:
print("Time since last server response:", GetTimeSinceLastServerResponse()