My soccer game's lag compensation isnt working!

I made this script, which is supposed to wait whenever a player wants to kick the ball. During that waiting period, if another player fires this event, it compares the timestamps of the players and sets the network owner to the player that fired the event first (had the lower timestamp)

ReplicatedStorage.Events.Remotes.React.OnServerEvent:Connect(function(
	player: Player, 
	ball: BasePart,
	touchedAt: CFrame,
	touchingPart: BasePart,
	reactDeclineCooldown: number, 
	timestamp: number
)
	print(`Received event from {player.Name}, fired at: {timestamp}s, received at {Workspace:GetServerTimeNow()}`)
	if not validateReact(player, ball, touchedAt, touchingPart, reactDeclineCooldown, timestamp) then
		warn(`{player.Name} react declined`)
		return
	end
	
	reactDeclineCooldown = math.clamp(reactDeclineCooldown, 0.1, 0.75)

	local playerToReact = player
	local finalCFrame = touchedAt
	
	if not reactRequests[player] then
		reactRequests[player] = {timestamp = timestamp, touchedAt = touchedAt}
	end
	
	task.wait(0.2)

	print(`normal: {getLengthOfDictionary(reactRequests)}`)

	if getLengthOfDictionary(reactRequests) > 1 then
		local earliestTime = math.huge
		local earliestPlayer = nil

		for player, reactData in reactRequests do
			print(`player: {player}, timestamp: {reactData.timestamp}, touchedAt: {reactData.touchedAt}`)

			if reactData.timestamp < earliestTime then
				earliestTime = reactData.timestamp
				finalCFrame = reactData.touchedAt
				earliestPlayer = player
			end
		end
		
		playerToReact = earliestPlayer
		
		print(`Final player to react: {player.Name}`)
	end

	if ball:GetAttribute("NetworkOwner") ~= playerToReact.UserId then
		if ball:GetAttribute("ReactDecline") then
			return
		end

		ball:SetAttribute("ReactDecline", true)
		task.delay(reactDeclineCooldown, function()
			ball:SetAttribute("ReactDecline", false)
		end)
		
		if ball.AssemblyLinearVelocity.Magnitude > 75 then
			if finalCFrame then
				ball.CFrame = CFrame.new(finalCFrame.Position)
			end
		end
	end

	table.clear(reactRequests)

	ball:SetNetworkOwner(playerToReact)
	ball:SetAttribute("NetworkOwner", playerToReact.UserId)
end)

However, despite this, the script doesn’t seem to be working, and players get in situations like these, where they touch the ball first, but the other player still gets it:


(Happens at 0:10)

You’re better off setting the network owner to nil for the collision object, making that object invisible, and making a separate visual model that smoothly animates to the server object on the client. Saves you the headache of whatever you are trying to do right now.

What do you mean, how would this fix the issue in the video?

Show us what it prints on the console as each players timestamps.

That way the server always has control over the ball. This is much more secure than giving clients direct network ownership over a critical game feature.

This will make the ball movement look laggy on clients. Hence why the suggestion is to have clients create their own visual ball object. Best of both worlds.

A potential further optimization is to fully remove the server ball object and just directly send relevant data to clients.

The wait you’re doing here seems like a bad practice. In general you shouldn’t wait like this. Just treat the first request you receive as being the actual first, and ignore future requests.

Exploiters could always send you timestamp 0 and consistenty steal the ball from everyone else.

But that still results in lag even if you visualize it, the server is always going to be laggy or have a small delay when applying forces, so if you’re just rendering that ball on the client there’s nothing that stops it from having that lag too unless you’re suggesting something else

On the client, you separately move the ball every frame. It interpolates to the target, not snaps to it.

Ok but my point still remains, the server is slow at applying velocities, with NetworkOwnership, theres no delay, with just a server-sided ball, there is a noticable delay of 0.1 seconds before the velocity actually MOVES the ball, unless you mean something else, otherwise “interpolating” won’t fix that issue either

I like the idea of having a server authorative ball, it’s just the responsiveness decreases significantly on the server

This will make the ball movement look laggy on clients. Hence why the suggestion is to have clients create their own visual ball object. Best of both worlds.

If we’re using a server ball (the server is slow to apply velocities), and if our client ball is essentially a visualization of the server ball (that interpolates to it or whatever) then how will it be smooth? I feel like im not understanding something here, won’t using the server as a reference point also make our client’s ball slow??? Won’t trying to get around this also just make the client ball visualizations off-sync with the server’s ball? which is what we wouldn’t want

Hi, I saw your problem. I’ve searched for possible causes to this and how to fix it.


Causes


Client timestamps:
Client timestamps can be spoofed or delayed.

Ball Network Ownership:
The ball network ownerhlship switching between clients causes desync and inconsistent physics

Clients
More clients may try to kick simultaneously, so the server processes late.


Possible solutions


Using Workspace:GetServerTimeNow() can help with the timing.

In case you don't know what GetServerTimeNow is

Workspace | Documentation - Roblox Creator Hub

Handle only the first valid kick event in the script, to fix the simultaneously (ATTENTION: I am not meaning to use the script once for a kick)

After a kick, broadcast velocity data to sync client visuals.