Lag Compensation Help

So basically I require some help, the ping compensation im doing, sort of works, The server is the black line, and the knife is the client, as you can see the server starts the raycast extremely late and i’m unsure why, in a single player roblox studio test server it works fine, but i’m assuming thats because its offline, basically I need help making the server cast start earlier, because there is no way the event is making that big of a difference.

Below I posted the important scripts

local function castProjectile(playerWhoFired, fireTime, firePosition, fireDirection)
	print("Fired")
	CastService.rayParms.FilterDescendantsInstances = {playerWhoFired.Character}
	local currentTime = tick()
	
	print(fireTime - currentTime)
	
	local fireVelocity = fireDirection * CastService.fireVelocity
	local deltaTime = currentTime - fireTime
	local gravityAcceleration = CastService.castBehavior.Acceleration
	local newVelocity = fireVelocity + gravityAcceleration * deltaTime
	local newPosition = firePosition + fireVelocity * deltaTime + 0.5 * gravityAcceleration * (deltaTime ^ 2)
	
	caster:Fire(newPosition, fireDirection, newVelocity, CastService.castBehavior)
end
knifeThrow.OnServerEvent:Connect(castProjectile)
local function castRayThrow(tool)
	CastService.rayParms.FilterDescendantsInstances = {character}
	local mousePosition = mouse.Hit.Position
	local origin = tool.Handle.Position
	local direction = (mousePosition - origin).Unit

	local bulletTemplate = tool.Handle:Clone()
	bulletTemplate.Anchored = true
	bulletTemplate.CanCollide = false

	CastService.castBehavior.CosmeticBulletTemplate = bulletTemplate
	
	throwKnife:FireServer(tick(), origin, direction)
	caster:Fire(origin, direction, CastService.fireVelocity, CastService.castBehavior)
	
	task.wait(1.3)
	Knife.isThrowing = false
end

sometimes fastcast acts up with their projectile visualization, just create a part and position it in the newPosition and show the results. slow down the initial velocity a little bit to better diagnose the issue.

also you can simulate latency in studio by going to studio settings → incoming replication lag

Honestly I have no idea whats going on anymore I am completely lost, Video provided but, in game server is ahead, but in studio with replication delay the client is ahead

In the video studio client replication had around 120 ping, But I just tested it with lower (the same in game) and it was still completely different results from studio and roblox game

its likely because of different framerates across server and client, try putting cap of 60fps in the client (the roblox game server is locked at 60fps), and if im right, it will be better (although not perfect). there is no real solution to this unfortunately

No, I don’t think that’s what I mean.

If you look at the first half of the video, it’s in the studio—it casts directly from the character and follows the knife smoothly. However, in the second half, where it’s in an actual Roblox game, there’s a huge gap, and for some reason, the server is ahead. I checked, and this happens regardless of the framerate.

ok nvm I looked at your code again, use workspace:GetServerTimeNow() instead of tick().

Thanks that solved it, didn’t realize it would be that simple, though I have one more question if its alright; How do I go about ensuring that every time the server hits something, the client also hits it on their screen and vice versa, because it seems even with my ping compensation, it sometimes hits on the client but not server, or on the server and not client

What you’re doing is not true lag compensation; currently, you are just changing the start position of the projectile, which does sync the client’s projectile position with the server, but it doesnt sync the world state.

Because of ping (and the stupidly high physics replication buffer (thanks roblox)), clients see the world in the past by a large amount. this problem of course is not fixable, but you can mitigate it.

There are 2 solutions to this; either 1. use client hitboxes just like 99% of roblox games, which is unsecure (because of exploiters), or if you are really patient and willing, make custom replication, about which I’ve made a post that may help

Unfortunately, this will take ages to do, because it requires strong comprehension of networking.

I had big success doing it, but it took me a really long time to fully finish and secure the system. If you have a large game like mine, it will take even longer, because you’ll also need to rewrite most of your systems to fit the new replication. If you have any questions, DM me

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.