How to optimize constantly broadcasting a remote event?

This post has more information about the situation:

Basically, I have to always broadcast each player’s mouse position to the server which lags the game a lot. This is necessary because the object the player is holding always has to point at the player’s mouse position and everybody else needs to be able to see it.

Current Script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local MousePos = game.StarterPlayer.MousePos -- MousePos is a Vector3Value in StarterPlayer

RunService.RenderStepped:Connect(function()
	MousePos.Value = mouse.Hit.Position
end)

MousePos.Changed:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit.Position)
end)

I am very new to this, so any way to optimize the script is appreciated

1 Like

You can use object:SetNetworkOwner(player) on the server and then just change position on the client side. This is also how roblox handles the characters of players. Note: I did not test if it works.

Instead of even broadcasting when mouse is still, do it only when mouse moves.

So replace RenderStep with Mouse.Move or UIS alternative

#1 That does not fix the problem much because it still sends remote events very frequently.
#2 I already have that in my code

Roblox Vector3 uses three 64-bit numbers which is 24 bytes
{833952C0-4925-4254-AE67-6AD7376F26E0}

(this method may result in a tiny imprecision)
we can use the buffer library to send 3 32-bit numbers which is 12 bytes (you can optimize it further like making the y axis 2 bytes instead of 4 if the map size y isnot that big etc…)

Example
local mousePos = Vector3.new(50.52, -14.234, 0.86) -- a random Vector for test

--OnClient
local bf = buffer.create(12) -- it stores three 4 bytes float which is 12 bytes
buffer.writef32(bf, 0, mousePos.X)
buffer.writef32(bf, 4, mousePos.Y)
buffer.writef32(bf, 8, mousePos.Z)

sendToServer(buf)

-- OnServer
reciveOnServer(buf)
if buffer.len(buf) ~=12 then return end -- check if the buffer size is correct
local mousePosTest = Vector3.new(
	buffer.readf32(bf, 0),
	buffer.readf32(bf, 4),
	buffer.readf32(bf, 8)
)


print(mousePosTest) -- for testing

(you can use Vector316Int if the map size is between those bounds)
{7E0B2DE3-601C-4901-953C-F2F469AB7871}

though am not sure if Roblox does optimize Vector3s when passed through the network since there are no sources

You could try to send it less often by recording a starting tick and measuring whether if the tick delta between the recorded tick and the current tick exceeds a set amount of time and reset the recorded tick into the current tick.

Is this going to remove, or at least improve the lag?

1 Like

it can reduce the amount of data sent/received and it can compress the data when sent through the network
proof: Introducing Luau buffer type [Beta] - Updates / Announcements - Developer Forum | Roblox

though if Roblox does automatically compress Vector3s when passed through the network then the difference wouldn’t be much

you can also send the remote less often like what FrostyWolfus has said

Maybe I’m doing something wrong, or maybe it just doesn’t work the way I need it to. Didn’t work.