I have the following code in a server script, attempting to aim the player’s arm and head at the mouse location.
-- Create arm motor6d
local shoulder = plr.Character.Torso:FindFirstChild("Right Shoulder")
local baseC0 = shoulder.C0
local aimMotor = shoulder:Clone()
aimMotor.Name = "Aim Motor"
aimMotor.Parent = plr.Character.Torso
-- Create head motor6d
local neck = plr.Character.Torso:FindFirstChild("Neck")
local neckBaseC0 = neck.C0
local neckAimMotor = neck:Clone()
neckAimMotor.Name = "Neck Aim Motor"
neckAimMotor.Parent = plr.Character.Torso
-- Set up aim connection
aimConnection = runServ.Stepped:Connect(function()
local mousePos = game.ReplicatedStorage.MiscRemoteEvents.getMouse:InvokeClient(plr)
mousePos = plr.Character.Torso.CFrame:PointToObjectSpace(mousePos)
-- Aim arm
local offset = CFrame.lookAt(Vector3.new(0, 0, 0), mousePos)
local aimCFrame = baseC0:ToObjectSpace(offset) * CFrame.Angles(0, math.rad(90), math.rad(90))
--aimMotor.C0 = baseC0 * aimCFrame.Rotation * CFrame.new(0, -.5, -.5) -- Can't use transform on the server because Heartbeat, so this its analog
game:GetService("TweenService"):Create(aimMotor, TweenInfo.new(.1), {C0 = baseC0 * aimCFrame.Rotation * CFrame.new(0, -.5, -.5)}):Play()
-- Aim head
local neckAimCFrame = neckBaseC0:ToObjectSpace(offset * CFrame.Angles(math.rad(90), math.rad(180), 0))
neckAimMotor.C0 = neckBaseC0 * neckAimCFrame.Rotation
end)
It works well:
Maybe a taaaaad bit choppy (if you know why that’s happening, do share, but I don’t think it’s too noticeable).
The problem comes when I do the other part, which is aiming the player’s body as well:
-- Start aiming
aimConnection = runServ.Stepped:Connect(function()
-- Aim player
local aim = game.Players.LocalPlayer:GetMouse().Hit.Position
local root = game.Players.LocalPlayer.Character.HumanoidRootPart
root.CFrame = CFrame.lookAt(root.Position, Vector3.new(aim.X, root.Position.Y, aim.Z))-- * CFrame.Angles(0, math.rad(90), 0)
end)
It works, but as you can see,
- The head and arm aim way past the target, and then fall back into place
- The head and arm are very choppy when you walk
If I had to guess, it’s something with how the head tries to position first, then the client does the torso and replicates to the server, and then the server adjusts accordingly, causing the overcorrection and the choppiness.
If my guess is correct, then
- How do I fix it?
- Can I instead do the body rotation on the server somehow? My current method doesn’t work; you can’t even move
- Is there a secret third thing I could do?