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.