Make player arms and head go down with mouse for gun tool?

So I made a few guns and I wanted to know how to make this:

I dont know how to make them for guns? How can I do it? And would I need to remove the animations in my guns?

1 Like

Something like this might work for the client using the gun. However, the transform needs to be updated on each client (so the camera vertical angle needs to be sent to other clients). It’s also necessary to use RunService.Stepped so that the transform changes won’t be overwritten by animations.

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

local plr = Players.LocalPlayer
local mouse = plr:GetMouse()

local char = -- the character (you can probably easily get a reference to it somehow)
local leftShoulder = char:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder")
local rightShoulder = char:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder")

local function rotateArms()
    local dir = mouse.Hit.Position - mouse.Origin.Position
    local angle = math.atan2(dir.Y, math.sqrt(dir.X^2+dir.Z^2))
    local rotationCf = CFrame.Angles(angle, 0, 0)
    local orgLeftTransform, orgRightTransform = leftShoulder.Transform, rightShoulder.Transform
    local orgLeftPos, orgRightPos = orgLeftTransform.Position, orgRightTransform.Position
    leftShoulder.Transform, rightShoulder.Transform = rotationCf*(orgLeftTransform-orgLeftPos)+orgLeftPos, rotationCf*(orgRightTransform-orgRightPos)+orgRightPos
end

RunService.Stepped:Connect(rotateArms)

Strange I tried the script and it doesn’t work. No error. It doesn’t move the gun and arms to mouse position?

Sorry, I forgot that the rotations were supposed to be based on mouse and not camera. I updated the code. Does it now work? Does it even do anything?

Well I tried the new code and it still doesnt work. Still no errors

wait is the code supposed to go on the server script or the client script? and if client how is it getting the tool?

The code needs to be on the client. To make it only update the transform when a gun is equipped, perhaps something like this would work.

local conn
tool.Equipped:Connect(function()
    conn = RunService.Stepped:Connect(rotateArms)
end
tool.UnEquipped:Connect(function()
    conn:Disconnect()
end

You can get a reference to the tool by getting it from the player’s backpack before than it’s equipped.

Where would I place the client script? not with the tool part? I tried starterCharacterScripts and it didnt work

Did it give any errors or do anything?

No it didn’t but I noticed that my server script mentions a tool. even if I don’t remember adding tool:

game.Players.PlayerAdded:Connect(
    function(player)
        player.CharacterAdded:Connect(
            function(Character)
                local Torso = Character:WaitForChild("HumanoidRootPart")
                local tool = script.Parent
                local Motor6D = Instance.new("Motor6D", Torso)
                Motor6D.Part0 = Torso
            end
        )
    end
)
game.ReplicatedStorage.MoveArms.OnServerEvent:Connect(
    function(plr, Motor, ToolHandle)
        Motor.Part1 = ToolHandle
    end
)