Server Sided Arm Rotation

Hi everyone,

I want the player’s arm to follow their mouse, which I have written a script for. However, the movement is client-side, not server-side, like I’d prefer. I tried modifying the script to use a Remote Event to transfer information, but nothing happens on the server.

Local:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local event = game:GetService("ReplicatedStorage"):WaitForChild("UpdateArmWeld")

local YAW_MIN = math.rad(-70) 
local YAW_MAX = math.rad(70)
local PITCH_MIN = math.rad(-30)
local PITCH_MAX = math.rad(30)

local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()

local tool = script.Parent

wait(2)


local function rotateArm()
	local hrp = char:WaitForChild("HumanoidRootPart")
	local rShoulder = char:WaitForChild("Torso"):WaitForChild("Right Shoulder")

	local jointWorld = rShoulder.Part0.CFrame * rShoulder.C0

	local dirLocal = jointWorld:PointToObjectSpace(mouse.Hit.Position)

	local yaw = math.atan2(-dirLocal.Z, dirLocal.X) 
	local pitch = math.asin(dirLocal.Unit.Y) 

	local yawClamped = math.clamp(yaw, YAW_MIN, YAW_MAX)
	local pitchClamped = math.clamp(pitch, PITCH_MIN, PITCH_MAX)
	rShoulder.Transform = CFrame.fromOrientation(0, yawClamped, pitchClamped + math.pi/2)
	
	event:FireServer(rShoulder, yawClamped, pitchClamped)
	
	print("Fired, info:", rShoulder.Name, yawClamped, pitchClamped)
end

local steppedConn
tool.Equipped:Connect(function()
	steppedConn = RunService.Stepped:Connect(function()
		rotateArm(char)
	end)
end)

tool.Unequipped:Connect(function()
	steppedConn:Disconnect()
end)

Server:

local updateArmWeldEvent = game:GetService("ReplicatedStorage").UpdateArmWeld

wait(2)

updateArmWeldEvent.OnServerEvent:Connect(function(player, rShoulder, yawClamped, pitchClamped)
	rShoulder.Transform = CFrame.fromOrientation(0, yawClamped, pitchClamped + math.pi/2)
	print("Received, info:", player.Name, rShoulder.Name, yawClamped, pitchClamped)
end)

The print debugs work, the info is fired and received, but the server does nothing with it. Can someone help?

Anything is appreciated!

I would assume other players could see the arm movement when you manipulate the character on the client side, but I could be wrong. Potentially this may help Network Ownership | Roblox Creator Documentation.

The main problem here is that the client side code is not set up well to cooperate with the server side code.
The client code does not wait for the server code to be ready. If you want to send values to the server code, you should wait for the server to set up a replication and then send the values.
The client code does not identify which player is using the tool. The server code does not know which player is using the tool unless it is sent that data.
The client code does not know what the server values for the clamped angles are. The server code does not know what the client values for the clamped angles are.
The client code’s rShoulder is not useful to the server code. The server code has its own rShoulder that is not the same object as the client’s rShoulder.
The client code sends rShoulder as a parameter to the server code. The server code does not send rShoulder as a parameter to the client code, but the client code uses that parameter anyway, which will cause an error.
The client code does not wait for the server code to finish doing its work. The server code does not wait for the client code to finish doing its work.