Advice on Remote Event Rate limiting

Alright so basically, i have 2 scripts for a player head rotation system based on the camera, however i have made it so updates get sent by every client every 0.5 seconds it seemed to work fine until i tested it with 8 players and that’s when it went from printing that it fired it slowly now seems to report its been fired over 2000 times within a few seconds, heres the server side event to update the head positions:

ServerLookPositionEvent.OnServerEvent:Connect(function(player, angleX, angleY)
	--print(player, angleX, angleY)
for _, serverplayer in ipairs(game.Players:GetPlayers()) do
	if serverplayer ~= player then
			local sender = player.Character
			ClientLookPositionEvent:FireClient(serverplayer, angleX, angleY, sender)
			print("Fired To Other Clients")
		end
	end
end)

And heres the client one to manage it:

local function updateLookPosition()
	local character = localPlayer.Character
	if not character then return end
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local neck = character:FindFirstChild("Neck", true)
	if not neck then return end

	local CameraDirection = (humanoidRootPart.CFrame:ToObjectSpace(camera.CFrame)).LookVector
	local angleX = -math.asin(CameraDirection.X)
	local angleY = math.asin(CameraDirection.Y)

	-- Clamp the angles to the maximum values
	angleX = math.clamp(angleX, -maxAngleX, maxAngleX)
	angleY = math.clamp(angleY, -maxAngleY, maxAngleY)

	-- Check if there's a significant change in the rotation
	if math.abs(angleX - lastAngleX) > 0.001 or math.abs(angleY - lastAngleY) > 0.001 then
		-- Update neck rotation locally
		if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
			neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(0, angleX, 0) * CFrame.Angles(angleY, 0, 0)
		elseif character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
			neck.C0 = CFrame.new(0, neck.C0.Y, 0) * CFrame.Angles(3 * math.pi/2, 0, math.pi) * CFrame.Angles(0, 0, angleX) * CFrame.Angles(-angleY, 0, 0)
	end
		-- Update last angles
		lastAngleX, lastAngleY = angleX, angleY
	end

	local now = tick()
	if now - lastUpdate >= 0.5 then
		-- Send the rotation angles to the server every 0.5 seconds
		ServerLookPositionEvent:FireServer(angleX, angleY)
		lastUpdate = now
	end
end

i have tried:

  1. Changing The speed of updates which makes it look choppy
  2. Searching the dev fourm for advice on this

I would like to know if i can optimize this to handle 25+ players without remote event lag though i have no idea where to start on that

Thanks in advance.

You say 2,000 times over a few seconds, assuming you have 8 players firing the RemoteEvent every half a second, that is only about 16 fires per second, or 160 fires over 10 seconds, way below the 2,000 that is being reported, indicating you are doing something wrong. You’re probably not disconnecting your previous events.

You can implement some type of smoothing via lerping, tweens, etc.

UnreliableRemoteEvents are the perfect use case for this. RemoteEvents are reliable and guaranteed ordering, meaning it wont ignore dropped packets and will yield future packets to guarantee ordering. UnreliableRemoteEvents ignore dropped packets and wont clog up your network pipeline, improving network, since we don’t really care if a few packets gets dropped along the way.

1 Like

alright i have a few questions:

I think this is because of all clients sending the server an event to tell it to update the rotation every 0.5 seconds and since it loops through each player that would cause it to spike alot if i were to make it so the server is counting the 0.5 and then sending the event to the localscripts to tell it to send the angles and stuff back would that work better?

A question about this, would the logic swap be as simple as changing the remote event to a Unreliable remote event?

would an UnreliableRemoteEvent need to do this and when should i do it?

Regardless, it still shouldn’t amount to 2,000 calls per second. Double check and make sure your client code isn’t stacking connections together. If so, disconnect them.

Yes, the API would be the same, though there is a 900 byte limit when sending over the remotes: UnreliableRemoteEvent | Documentation - Roblox Creator Hub. Ensure you are only sending the angle numbers.

It works as any other Instances. Make sure you are disconnecting when you need to (read the first paragraph of this post).

1 Like

Alright ill try to make some adjustments to the code and i will let you know if it works or not

So i took your advice and now its down to about 1500x in a minute and a half and i added some extra debugging things and yes they do seems to sometimes being fired more than once, im gonna go ahead and fix that but thats the main issue solved so im gonna mark this as resolved

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