How to show arm movement on the server?

At the moment I have a tool gun that allows your arm to follow your mouse when its equipped. What I’m trying to do is allow the movement of the arms when they follow the mouse to be shown on the server to other players.

https://gyazo.com/2930800ebf1355de180fa61579d4bef7

So far I have tried firing a RemoteEvent on a loop and run the arm movement via the server. While it does work, server lag makes it very impractical and slow to use compared to just running a client side loop.

This is my code.

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

local plr = game.Players.LocalPlayer

repeat wait() until plr.Character~= nil

local char = plr.Character

local Equipped = false


repeat wait() until plr.Character:FindFirstChild("UpperTorso")

local mouse = plr:GetMouse()



script.Parent.Equipped:Connect(function()
	Equipped = true
	
	local armOffset = char.UpperTorso.CFrame:Inverse() * char.RightUpperArm.CFrame
	
	local armWeld = Instance.new("Weld")
	armWeld.Name = "armWeld"
	armWeld.Part0 = char.UpperTorso
	armWeld.Part1 = char.RightUpperArm
	armWeld.Parent = char.RightUpperArm
	
	
	
	
	while Equipped == true do
		RunService.RenderStepped:Wait()
		local cframe = CFrame.new(char.UpperTorso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
		armWeld.C0 = armOffset * char.UpperTorso.CFrame:toObjectSpace(cframe)

	end
	armWeld:Destroy()
end)

script.Parent.Unequipped:Connect(function()
	
	Equipped = false
end)

script.Parent.Activated:Connect(function()
	if Equipped == true and FireCooldown == false then
		FireCooldown = true
		script.Parent.Remotes.Fire:FireServer(plr)
		wait(.1)
		FireCooldown = false
	end
end)

Make a remote event and send the arm rotation to the server and then make the server send your arm rotation to all other players… something like this:

Client:

-- whenever your arms move, send the arm angle to server
Remote:FireServer(ArmRotation)

-- when the server sends you someone else's arm angle request, update their arms on your client
Remote.OnClientEvent:Connect(function(Player, ArmRotation)
	-- adjust the Player's arms
end)

Server:

Remote.OnServerEvent:Connect(function(Caller, ArmRotation)
	for i, Player in pairs(game:GetService'Players':GetPlayers()) do
		if Player ~= Caller then
			Remote:FireClient(Player, Caller, ArmRotation)
		end
	end
end)
3 Likes