I am firing remote event to the server which will fire the position to all clients in order to update the player’s head cframe and rotation
Client:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpdateNeck = ReplicatedStorage:WaitForChild("UpdateNeck")
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
-- Function to handle neck tracking for a given character
local function trackNeck(character)
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local isR15 = humanoid.RigType == Enum.HumanoidRigType.R15
local neck
if isR15 then
neck = character:FindFirstChild("Neck", true)
else
local torso = character:WaitForChild("Torso")
neck = torso:FindFirstChild("Neck")
end
if not neck then return end
local initialNeckC0
if isR15 then
neck.C1 = CFrame.new(0, 0, 0) -- Set C1 to 0,0,0 for R15
initialNeckC0 = neck.C0 -- Keep the original C0
else
neck.C1 = CFrame.new(0, -0.5, 0)
initialNeckC0 = CFrame.new(0, 1, 0)
end
local smoothFactor = 0.1
local lastUpdateTime = tick() -- Initialize the last update time
game:GetService("RunService").RenderStepped:Connect(function()
if neck and humanoidRootPart then
local cameraDirection = humanoidRootPart.CFrame:ToObjectSpace(camera.CFrame).LookVector
local targetC0 = CFrame.new(0, initialNeckC0.Y, 0)
* CFrame.Angles(0, -cameraDirection.X, 0)
* CFrame.Angles(cameraDirection.Y, 0, 0)
neck.C0 = neck.C0:Lerp(targetC0, smoothFactor)
-- Send the updated neck positions to the server every second
if tick() - lastUpdateTime >= 1 then
UpdateNeck:FireServer(neck.C0, neck.C1)
lastUpdateTime = tick() -- Update the last update time
end
end
end)
end
if player.Character then
trackNeck(player.Character)
end
player.CharacterAdded:Connect(trackNeck)
local TweenService = game:GetService("TweenService")
-- Listen for neck updates from the server
UpdateNeck.OnClientEvent:Connect(function(neckC0, neckC1, sender)
if sender ~= player then -- Only update if the sender is not the local player
local character = sender.Character
if character then
local isR15 = character:FindFirstChild("Humanoid").RigType == Enum.HumanoidRigType.R15
local neck
if isR15 then
neck = character:FindFirstChild("Neck", true)
else
local torso = character:FindFirstChild("Torso")
neck = torso:FindFirstChild("Neck")
end
if neck then
local tweenInfo = TweenInfo.new(0.35, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local tweenC0 = TweenService:Create(neck, tweenInfo, { C0 = neckC0 })
local tweenC1 = TweenService:Create(neck, tweenInfo, { C1 = neckC1 })
tweenC0:Play()
tweenC1:Play()
-- Optional: Connect to tween completed event if you need to perform actions after tweening
tweenC0.Completed:Connect(function()
-- Do something after C0 tween completes if needed
end)
tweenC1.Completed:Connect(function()
-- Do something after C1 tween completes if needed
end)
end
end
end
end)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UpdateNeck = ReplicatedStorage:WaitForChild("UpdateNeck")
UpdateNeck.OnServerEvent:Connect(function(player, neckC0, neckC1)
-- Send the neck update to all clients, including the sender
UpdateNeck:FireAllClients(neckC0, neckC1, player)
end)
It updates every 1 second, but this probably isnt a correct way so any help or recommendations will help in regard of how to optimize this