Physics Lagging

I am making an air hockey game and I am using various body movers to strike the puck. However, for a brief moment at the beginning, the puck lags before moving. After a few seconds the problem goes away and the puck moves properly. This same problem occurs after the puck’s velocity is cancelled and its position is changed (When the player scores a goal, the puck resets).

Here is a clip showing the puck lagging (When the video stops, the puck looks like it is inside the table’s barrier. This is because of the way I am handling the hockey table’s boundaries and is intended behaviour.)

Here is a clip showing the intended, smooth behaviour

This can be caused because of unnecessary physics used in the script, so try optimizing your script.

I also can’t do anything without looking at the script, so this might be because it’s choppy

As for scripts, I have a server script in ServerScriptService that sets the striker’s Network Owner to the player that just joined.

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

I have a local script in StarterPlayer.StarterPlayerScripts that sets the player’s camera position to that of a part called “CamPart”.

workspace:WaitForChild(game.Players.LocalPlayer.Name)

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

workspace.CurrentCamera.CFrame = workspace.HockeyTable.CamPart.CFrame

Finally, I have a local script in StarterPlayer.StarterPlayerScripts that gets the mouse position using a ray and then sets the striker’s position to the mouse position.

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)

SetNetworkOwner is 100 percent causing this lag, if you know how it works, it takes all the connection from the game to one player or item, this causes it to be choppy and laggy

1 Like

I thought so too, but I need to replicate the player’s striker movement to the server. Do you have any suggestions of how I can do this whilst avoiding SetNetworkOwner or avoiding the lag?

Not sure what you can do, but you can optimize your script to use only necessary stuff, all you need to know is that SetNetworkOwner only works for singleplayer

By optimizing your script here’s a example

So I am running a render stepped when someones playing a game, let’s say the render stepped is unnessecary when someone is playing, I stop the render stepped and when they are done I turn it back on.

Another example is if someones playing a game and things are running in the background that the player can’t see, I can turn it off until they are done.

Think of it like render distance

You can also use SetNetworkOwner on the physics instead, which in that case, it can be multiplayer if you use it correctly

Oh ok. When I was first programming the striker’s movement I thought of constantly sending the mouse position to the server using a RemoteEvent. However, I think that there is a cooldown after which the RemoteEvent stops working for a bit. I also thought that a RemoteEvent might introduce more lag to the game. Both of these would be game breaking problems.

If you want to know more, RenderStepped and Heartbeat are the most costly to use, then it is SetNetworkOwner.

Remote Events and RemoteFunctions don’t do much though so don’t worry about those, you might want to worry about physics, they are the main part of huge lag

Alright, I’ll try using RemoteEvents. However I’d still need to use some sort of continuous loop to get the mouse position.

Be careful with loops, it can be costly as well, have a good day!

1 Like

Have a good day! Thanks for you help :slight_smile: