Avoid physics lagging from SetNetworkOwner

I am making an air hockey game. For a few seconds the puck lags when it is hit by the striker. Thanks to others on the forum, I managed to narrow the problem down to SetNetworkOwner. I set the striker’s network owner to the player so that the player’s mouse movement can be replicated to the server.

Here is a clip showing the lag that the puck experiences for a few seconds.

Here is a clip showing how the puck is supposed to move.

Here is the script that I use to set the striker’s network owner. (Server script in ServerScriptService)

game.Players.PlayerAdded:Connect(function(plr)
	
	workspace.Striker:SetNetworkOwner(plr)
	
end)

Here is the script that I use to set the striker’s position to the player’s mouse position. (Local script in StarterPlayer.StarterPlayerScripts)

local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local striker = workspace.Striker

local function getMousePos(ignoreList)
	
	local mouseLocation = userInputService:GetMouseLocation()
	local viewportPointRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local extendedRay = Ray.new(viewportPointRay.Origin, viewportPointRay.Direction*1000)
	
	return workspace:FindPartOnRayWithIgnoreList(extendedRay, ignoreList)
	
end

runService.RenderStepped:Connect(function()
	
	local _, mousePos = getMousePos({striker})
	striker.BodyPosition.Position = Vector3.new(mousePos.X, striker.BodyPosition.Position.Y, mousePos.Z)
	
end)

Is there any way I could send the mouse’s position to the server or control the striker without SetNetworkOwner? I tried RemoteEvents but they are too slow to keep up with the mouse.

4 Likes

For sending the mouse’s position to the server, you can Invoke a RemoteFunction to return the position of the mouse to the server. For more information on RemoteFunctions and how to use and make one, Consider reading the Developer Wiki’s article on it

Do you want this game to be single player or multiplayer?

If you do it multiplayer then the replication lag will always exist whatever you do. It can be moved from one player to another or to both (by doing everything server side as you do now). But you will never be able to eliminate it.

If you do it single player then just move all your code to local scripts (and do the necessary refactoring). Then it will look smooth but just on this client.

It has to be multiplayer, which is what is causing my so many issues.

Then I am really sorry to say that you will always have replication lag. Even if you send the mouse position to the server, it will still be laggy.

1 Like

I see. Do you have any ideas of how I might move the striker without SetNetworkOwner? Every other method that I’ve tested has introduced latency (RemoteEvents/RemoteFunctions). Or is this just not possible in Roblox?

There is no way to not have latency because you check for collisions on the server probably and so both players see the delay to the server. If you check for collision on the client then one player will see it perfectly but for the other it will still be bad (actually worse).

Unfortunately roblox is not very good for games that require low latency.

I saw one scripter wrote code for custom puck physics. You can try to do this using math rather than roblox physics.
His video: