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:
- Changing The speed of updates which makes it look choppy
- 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.