Will this timer be accurate between all players and servers?

My goal is to create a speedrun timer for a levels based game in which the player can time their run from start to finish. However, the timer needs to be accurate between all players and servers for example, if 2 players start and end a timer at the exact same time then their times should be the same, this should also apply between different servers, so if the players were on different servers as long as they start and end at the same time they should be the same. This is the code I’m using, im just not sure if its fully accurate:

local CurrentValue = 0
IsPlaying = true
local x
local start = script.Parent
start.TextScaled = true
start.MouseButton1Up:Connect(function()
	while IsPlaying == true do
		CurrentValue += task.wait()
		x = CurrentValue - CurrentValue % 0.01
		start.Text = x
		start.MouseButton1Up:Connect(function()
			IsPlaying = false
		end)
	end 
end)

The reason it has to be accurate is that the player can submit their times into a global leaderboard. Also this is currently in a local script for now but in the future i’ll be putting it in a server script im not sure if that changes anything.

The server can’t capture client-dependent input, i.e; mouse movement, mouse clicks, keyboard presses, controller button presses etc.

Yea I know, I’m just gonna have it fire a remote to the server when the player clicks then run this code in a server script.

the best way to time is like this

local times = {}

local function StartTimer(player)
	times[player] = time()
end

local function StopTimer(player)
	local deltaTime = time() - times[player]
	times[player] = nil
	print(player.Name, "finished in", deltaTime, "seconds")
	return deltaTime
end

also be careful as events take time to send from the client to the server so a player who is lagging more will take longer to send a event to the server so you might want to use GetNetworkPing

because GetNetworkPing gets the average time it takes messages to go from the client to the server and back to the client again you will want to divide this ping by 2

deltaTime -= player:GetNetworkPing() / 2

something like this

1 Like