Server Sided Arm Pointing

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!

Why are you doing it on the server though? Changes on the client should automatically replicate

1 Like

I thought it was a local script, so it shouldn’t replicate to all clients?

Changes on client dont automatically replicate, its the other way around where anything that happens on the server replicates. @Falcon_Aviator Why do you need it to be on the server?

That way, all players can see the player’s arm rotating.

1 Like

So the print on the serverside, is it recieving and info? Is it the right info?

Yes, and yes.

I can send you the place file to test if you’d like.

Im good for now, thanks! What is rShoulder.Transform? rShoulder is an instance right? I didn’t think there was a property called transform.

I am not sure, to be honest, the script was cobbled together from many dev forum posts. Do you think I should switch to changing C0?

Im not very good with Joints and movement, so im not sure. I want to learn how to use it, is there a name for that kind of thing so i can look it up?

It should just be welds and Motor6Ds

1 Like

event.OnServerEvent:Connect(function(rShoulder, yawClamped, pitchClamped)
print(“Received, info:”, rShoulder.Name, yawClamped, pitchClamped)
rShoulder.Transform = CFrame.fromOrientation(0, yawClamped, pitchClamped + math.pi/2)

event.OnServerEvent:Connect(function(player, rShoulder, yawClamped, pitchClamped)
print(“Received info:”, rShoulder.Name, yawClamped, pitchClamped)
end)

You can only fire a remote event on the client, because the server does not have a mouse.
So you need to make a remote function, and call it on the client with the information from the mouse.
Then the server can apply the changes.