How to make changes to arms replicate to other clients

I have a local script in a tool which points makes the arms point to the mouse, it works only for the client but not for others.

RunService.RenderStepped:Connect(function(dt)
	if equipped then
		local UpdatedRecoilSpring = RecoilSpring:update(math.min(dt, 1)) -- recoil
	    workspace.Camera.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X), math.rad(UpdatedRecoilSpring.Y), math.rad(UpdatedRecoilSpring.Z))

           --i want this to replicate to other clients
	    local rightX, rightY, rightZ = char.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
	    char.Torso["Right Shoulder"].C0 = (char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))
          
	    local leftX, leftY, leftZ = char.Torso["Left Shoulder"].C0:ToEulerAnglesYXZ()
	    char.Torso["Left Shoulder"].C0 = (char.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-mouse.Hit.p - -mouse.Origin.p).unit.y))
	end
end)

This is where you can use UnreliableRemoteEvents. You can send it to the server to replicate to every other player.

  1. Add an UnreliableRemoteEvent in ReplicatedStorage named “UpdateShoulders” (or whatever name you want, just make sure to change the variables in the scripts):
    image

  2. Add a new script in ServerScriptService and paste this inside:

local updateShoulders = game:GetService("ReplicatedStorage").UpdateShoulders

updateShoulders.OnServerEvent:Connect(function(player, positionData)
	local torso = player.Character.Torso
	torso["Left Shoulder"].C0 = positionData[1]
	torso["Right Shoulder"].C0 = positionData[2]
end)
  1. Replace your client code with this:
local updateShoulders = game:GetService("ReplicatedStorage"):WaitForChild("UpdateShoulders")

RunService.RenderStepped:Connect(function(dt)
	if equipped then
		local UpdatedRecoilSpring = RecoilSpring:update(math.min(dt, 1)) -- recoil
		workspace.Camera.CFrame *= CFrame.Angles(math.rad(UpdatedRecoilSpring.X), math.rad(UpdatedRecoilSpring.Y), math.rad(UpdatedRecoilSpring.Z))
		
		local rightX, rightY, rightZ = char.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
		local leftX, leftY, leftZ = char.Torso["Left Shoulder"].C0:ToEulerAnglesYXZ()
		
		char.Torso["Left Shoulder"].C0 = (char.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-mouse.Hit.p - -mouse.Origin.p).unit.y))
		char.Torso["Right Shoulder"].C0 = (char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))

		updateShoulders:FireServer({
			(char.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-mouse.Hit.p - -mouse.Origin.p).unit.y)),
			(char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((mouse.Hit.p - mouse.Origin.p).unit.y))
		})
	end
end)

We’re using UnreliableRemoteEvents to save resources because it doesn’t matter if some packets are lost, since new packets will correct the position. You could also send events less frequently to save more performance, but that is for you to implement.

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