Currently I’m trying to work on a lag compensation system that grabs what the player would have seen when they tell the server. However it is absolutely vital that the ping is effectively removed in the calculations carried out on the server as it is very much a real time thing (its to do with hit detection).
I’m currently using Vector3s instead of CFrames just for output reasons.
At the moment, I’m trying to use the time between two known recorded points (could be 0.1s of each other) and using the time of the ping to calculate the alpha value between each two points, in order to calculate a Vector3 which would be what the character would see.
It requires saving lots of Vector3 bits of data (between 8 and 30) in a table and updating them regularly. This could optimally range between 8Hz and 30Hz.
This is my current script:
local start = Vector3.new(0, 0, 0)
local numx = 90
local finish = Vector3.new(0, numx, 0)
local current = Vector3.new(0, 0, 0)
local alpha = 0
local atarget = numx
local flip = false
local ping = 30/60
local refresh = 30
local seconds = 1
local tim = tick() - 1/refresh
local step
local stored = {}
game:GetService("RunService").Stepped:Connect(function(st)
step = st
if not flip then
alpha = alpha + 1
if alpha == atarget then
flip = true
end
else
alpha = alpha - 1
if alpha == 0 then
flip = false
end
end
current = start:lerp(finish, alpha/atarget)
if tick() - tim >= 1/refresh then
table.insert(stored, 1, {cf = current, t = tick()})
tim = tick()
if tick() - stored[#stored].t > seconds then
table.remove(stored, #stored)
end
end
end)
local function lerp(a,b,c) return a + (b - a) * c end
local function round(n, x)
if not x then x = 4 end
return math.floor(n*(10^x) + 0.5)/(10^x)
end
wait(2-ping)
local target = current
local targettime = tick()
local x = 0
local uf5 = tick()
repeat game:GetService("RunService").Stepped:Wait() until tick() - uf5 >= ping
print(tick() - uf5)
ping = tick() - uf5
local t2 = tick() - ping
local percent
local f1, f2
for i,v in pairs(stored) do
if v.t <= t2 then
f1 = stored[i-1] or {cf = current, t = tick()}
f2 = stored[i]
percent = (t2-f1.t)/(f2.t-f1.t)
print(f1.t, t2, f2.t, percent, targettime, t2, ping)
print(f1.cf, target, f2.cf, lerp(f1.t, f2.t, percent))
print(f1.cf:lerp(f2.cf, percent))
break
end
end
I am aware this code may use some bad practises and doesn’t actually involve any communication with the player at the moment, I’m just trying to simplify the problem so that it works out for me.
Half the program is to do with updating a Vector3 regularly at 60fps (what would happen in a server), then grabbing the Vector3 every other frame with the Vector3 and the timestamp.
Ignoring all bad practises, can anyone offer any insight to why this might not be working? I’ve been stuck on this for about 2 and a half weeks now and its really slowing down my program flow and it’s something I need to figure out ASAP.
Thank you to everyone who helps!