Tool looking towards mouse on server

Trying to make a “tool look towards mouse” script work on server but I’m having problems

There’s no errors in the console and it prints “equip” just fine but the arm does not move

Note that it works fine when the server code is in the localscript so the problem is not in the arm move script itself

LocalScript:

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

local remote2 = game:GetService("ReplicatedStorage"):WaitForChild("Injector2")

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

local tool = script.Parent

local steppedConn
tool.Equipped:Connect(function()
	steppedConn = RunService.Stepped:Connect(function()
		remote2:FireServer(char, mouse.Hit.Position)
	end)
end)

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

ServerScript:

local remote2 = game:GetService("ReplicatedStorage"):WaitForChild("Injector2")

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

remote2.OnServerEvent:Connect(function(plr, char, mousehit)
	local rShoulder = char:WaitForChild("Torso"):WaitForChild("Right Shoulder")
	local jointWorld = rShoulder.Part0.CFrame * rShoulder.C0
	local dirLocal = jointWorld:PointToObjectSpace(mousehit)
	
	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)

	print("equip")
end)

I tested it and right off the bat I don’t understand why you would be modifying the arm itself wouldn’t you just modify the handle of the tool to look towards the mouse? Maybe I’m missing something this is not a arm position type of modification so why would you modify the arm position if you’re trying to move the tool itself

Im trying to move the whole arm along with the tool, probably should have worded it a bit better

Please read documentation
image

2 Likes

I dont see how this helps me really, I said the arm moving does work on the local script just fine, its just that it doesnt work on the serverscript

image

1 Like

ohh I see… well then how should I go about making the change be visible to the server and everyone?

Replicate the mouse position to other clients and let them simulate movement on their side.

1 Like

Yes but you aren’t being specific enough. Ivan probably doesn’t know you’re referring to using :FireAllClients(). He has to switch from using :FireServer() to learning how to use :FireAllClients()

1 Like

I’d argue of using that
Character should simulate own movement for itself
You need to fire all clients but one.

1 Like

Yeah idk what I’m really doing to be honest

I tried adding something to both scripts but it don’t work

Server:

remote2.OnServerEvent:Connect(function(plr, char, mousehit)
	local rShoulder = char:WaitForChild("Torso"):WaitForChild("Right Shoulder")
	local jointWorld = rShoulder.Part0.CFrame * rShoulder.C0
	local dirLocal = jointWorld:PointToObjectSpace(mousehit)
	
	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)
	
	remote2:FireAllClients(plr, rShoulder, yawClamped, pitchClamped)
	
	print("equip")
end)

Local:

remote2.OnClientEvent:Connect(function(plr, rShoulder, yawClamped, pitchClamped)
	rShoulder.Transform = CFrame.fromOrientation(0, yawClamped, pitchClamped + math.pi/2)
end)

Ive been trying some stuff but I cant get it to work vro

Local Script:

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

remote2.OnClientEvent:Connect(function()
	print("ok")
	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)
end)

Server Script:

remote2.OnServerEvent:Connect(function(plr)
	remote2:FireAllClients()
	print("equip")
end)

where th do you expect mouse to pop up?
Out of thin air?
Look at your code again

its just modified segments of the code I posted in the original post, the mouse is a variable dw

Perhaps look into Inverse Kinematics and the IKControl class.

what does this have to do with turning the script from local to server

It allows for much better control over directly modifying Motor6Ds.

I would kinda like to keep my current script ngl

Then you’ll need to use remotes to tell every client to set the Transform on their client.

thats what I did in the above example tho and it didnt work