How to cap lag compensation rewinds in Roblox

Rewinding to a shot time the client sends lets players hit targets that already moved behind cover. How to rewind by the server's own ping figure, capped.

warningLuaudit check: client-timed-rewinds

What it looks like

To make hits fair for players with lag, the server keeps a short history of where each character was and checks a shot against where the target stood when it was fired. How far back to look comes from the client: a shot time it sends, often from Workspace:GetServerTimeNow(), or a ping or delay value. The server accepts any time inside its history.

Luau
remote.OnServerEvent:Connect(function(player, victim, shotTime)
	local position = history.at(victim, shotTime)
	if canHitFrom(player, position) then
		damage(victim)
	end
end)

What an exploiter could do

They can hit players where they were up to the whole length of the history ago. Someone who stepped behind cover or out of range still takes the shot, and dodging stops working against them.

How to fix it

  • Work the rewind out on the server: the shooter's GetNetworkPing() plus a small margin, capped at a few hundred milliseconds or whatever the game is tuned for.
  • If the client still sends a shot time, clamp it so the rewind is never longer than the server's own figure, rather than only inside the history.
  • Keep the history no longer than the cap. Snapshots older than any rewind the server would allow serve only exploiters.

Vulnerable example

Vulnerable
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local keep = 2
local range = 300
local damage = 45
local cooldown = 1.5

type Snapshot = { time: number, position: Vector3 }

local history: { [Model]: { Snapshot } } = {}
local lastShot: { [Player]: number } = {}

RunService.Heartbeat:Connect(function()
	local now = Workspace:GetServerTimeNow()
	for _, other in Players:GetPlayers() do
		local character = other.Character
		local root = character and character:FindFirstChild("HumanoidRootPart")
		if character and root and root:IsA("BasePart") and root.Position.Magnitude < math.huge then
			local list = history[character] or {}
			history[character] = list
			table.insert(list, { time = now, position = root.Position })
			while list[1] and now - list[1].time > keep do
				table.remove(list, 1)
			end
		end
	end
end)

local function positionAt(character: Model, time: number): Vector3?
	local list = history[character]
	if not list then
		return nil
	end
	for index = #list, 1, -1 do
		if list[index].time <= time then
			return list[index].position
		end
	end
	return nil
end

ReplicatedStorage.Remotes.FireRifle.OnServerEvent:Connect(function(player: Player, victim: unknown, latency: unknown)
	if typeof(victim) ~= "Instance" or not victim:IsA("Model") or typeof(latency) ~= "number" then
		return
	end
	if not (latency >= 0 and latency <= keep) then
		return
	end
	local character = player.Character
	local head = character and character:FindFirstChild("Head")
	local humanoid = victim:FindFirstChildOfClass("Humanoid")
	if not character or victim == character or not head or not head:IsA("BasePart") or not character:FindFirstChild("Rifle") then
		return
	end
	if not humanoid or humanoid.Health <= 0 then
		return
	end
	local position = positionAt(victim, Workspace:GetServerTimeNow() - latency)
	if not position then
		return
	end
	local now = os.clock()
	if now - (lastShot[player] or -math.huge) < cooldown then
		return
	end
	local offset = position - head.Position
	if not (offset.Magnitude <= range) then
		return
	end
	lastShot[player] = now
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = { character, victim }
	if not Workspace:Raycast(head.Position, offset, params) then
		humanoid:TakeDamage(damage)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		history[character] = nil
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	lastShot[player] = nil
end)

Fixed example

Fixed
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local maxRewind = 0.3
local rewindMargin = 0.05
local range = 300
local damage = 45
local cooldown = 1.5

type Snapshot = { time: number, position: Vector3 }

local history: { [Model]: { Snapshot } } = {}
local lastShot: { [Player]: number } = {}

RunService.Heartbeat:Connect(function()
	local now = Workspace:GetServerTimeNow()
	for _, other in Players:GetPlayers() do
		local character = other.Character
		local root = character and character:FindFirstChild("HumanoidRootPart")
		if character and root and root:IsA("BasePart") and root.Position.Magnitude < math.huge then
			local list = history[character] or {}
			history[character] = list
			table.insert(list, { time = now, position = root.Position })
			while list[1] and now - list[1].time > maxRewind do
				table.remove(list, 1)
			end
		end
	end
end)

local function positionAt(character: Model, time: number): Vector3?
	local list = history[character]
	if not list then
		return nil
	end
	for index = #list, 1, -1 do
		if list[index].time <= time then
			return list[index].position
		end
	end
	return if list[1] then list[1].position else nil
end

ReplicatedStorage.Remotes.FireRifle.OnServerEvent:Connect(function(player: Player, victim: unknown)
	if typeof(victim) ~= "Instance" or not victim:IsA("Model") then
		return
	end
	local character = player.Character
	local head = character and character:FindFirstChild("Head")
	local humanoid = victim:FindFirstChildOfClass("Humanoid")
	if not character or victim == character or not head or not head:IsA("BasePart") or not character:FindFirstChild("Rifle") then
		return
	end
	if not humanoid or humanoid.Health <= 0 then
		return
	end
	local rewind = math.min(player:GetNetworkPing() + rewindMargin, maxRewind)
	local position = positionAt(victim, Workspace:GetServerTimeNow() - rewind)
	if not position then
		return
	end
	local now = os.clock()
	if now - (lastShot[player] or -math.huge) < cooldown then
		return
	end
	local offset = position - head.Position
	if not (offset.Magnitude <= range) then
		return
	end
	lastShot[player] = now
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = { character, victim }
	if not Workspace:Raycast(head.Position, offset, params) then
		humanoid:TakeDamage(damage)
	end
end)

Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		history[character] = nil
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	lastShot[player] = nil
end)

How to spot this in your own scripts, and the cases that only look like it, are part of the Luaudit check your AI runs.