Hey there! I’m trying to convert this, normally local sided, script that I had found into a server sided script!
I feel like I got the information to properly convert over, but I’m just not having the server interpret the given variables correctly. Could someone take a look and see why it isn’t working?
As for the script, it’s meant to first calculate where the player’s mouse is on the client, then point the tool towards the mouse on the server. (Hence the movement of the right arm’s cframe)
Any and all help is appreciated! Even just a nudge in the right direction would be super helpful, I’m not sure which part of this to tackle.
Local:
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local tool = script.Parent
local focused = tool.Values.Focused
local event = script.Parent.Point
local InputService = game:GetService("UserInputService")
script.Parent.Equipped:Connect(function()
if InputService.TouchEnabled == true then
script.Enabled = false
end
end)
local YAW_MIN = math.rad(-70) -- left/right (up to +/-180)
local YAW_MAX = math.rad(70)
local PITCH_MIN = math.rad(-30) -- up/down (up to +/-90)
local PITCH_MAX = math.rad(30)
local hit = mouse.Hit.Position
local function relay(char)
event:FireServer(char,hit, YAW_MIN,YAW_MAX, PITCH_MIN,PITCH_MAX)
end
local steppedConn
tool.Equipped:Connect(function()
steppedConn = RunService.Stepped:Connect(function()
relay(char)
end)
end)
tool.Unequipped:Connect(function()
steppedConn:Disconnect()
end)
local function Focused()
if focused.Value == true then
steppedConn:Disconnect()
else
steppedConn = RunService.Stepped:Connect(function()
relay(char)
end)
end
end
focused.Changed:Connect(Focused)
Server:
local event = script.Parent.Point
local function rotateArm(player,char,hit, YAW_MIN,YAW_MAX, PITCH_MIN,PITCH_MAX)
local hrp = char:WaitForChild("HumanoidRootPart")
local rShoulder = char:WaitForChild("Torso"):WaitForChild("Right Shoulder")
-- jointWorld is the position of C0 with the rotation of C0*C1:
local jointWorld = rShoulder.Part0.CFrame * rShoulder.C0
-- joint->mouse vector in "joint space"
local dirLocal = jointWorld:PointToObjectSpace(hit)
-- yaw or pitch of 0 means "straight in front"
local yaw = math.atan2(-dirLocal.Z, dirLocal.X) -- left/right rotation
local pitch = math.asin(dirLocal.Unit.Y) -- up/down rotation (use above to make a triangle with the Y axis)
local yawClamped = math.clamp(yaw, YAW_MIN, YAW_MAX)
local pitchClamped = math.clamp(pitch, PITCH_MIN, PITCH_MAX)
-- note: CFrame.Angles applies rotations in Z, Y, X order
-- CFrame.fromOrientation applies in Y, X, Z order
rShoulder.Transform = CFrame.fromOrientation(0, yawClamped, pitchClamped + math.pi/2)
end
event.OnServerEvent:Connect(rotateArm)