Pointing a players arm towards a part

Hello reader, hope you’re having a great day.

How would I go about pointing a players arm towards a part? More specifically, I am unsure how to go about manipulating the players’ shoulders’ Motor6D.C0 property to point towards a part each RenderStepped while using the shoulders natural position.

I have this code so far, which works, but the shoulder remains at the midpoint of the arm (Part0):

local shoulder: Motor6D = character["LeftUpperArm"]["LeftShoulder"];
local jointPosition = shoulder.Part0.CFrame:toWorldSpace(CFrame.new(0,0,0))
local cframe = CFrame.new(jointPosition.p, workspace.PartToPointAt.Position) * CFrame.Angles(math.pi/2, 0, 0) * CFrame.new(0, -1, 0)
shoulder.C0 = shoulder.Part0.CFrame:toObjectSpace(cframe)

I cannot use pre-defined offsets, as the R15 rigs scale during gameplay.

Thanks a lot for your help.
~ P3tray

5 Likes

You could use the look at function to do that.

1 Like

Could you elaborate? I’ve been messing with this for about 6 hours. This is a cry for help.

2 Likes

Can you tell me the part name which is the highest in the hand? Like the shoulder name

1 Like

The shoulder is called shoulder. This is an R15 rig. All default Roblox R15 rigs are named the same.

No not the rig name, the part name. Let me see actually

1 Like

It’s game.Workspace.PartToPointAt as seen on Line 3 in the question.
image

local lookAtCFrame = CFrame.lookAt(Shoulder.Position,LookAtPart.Position) -- This will the CFrame you should set your shoulder to
1 Like

Try this code right here and change the names, tell me if it works

1 Like

What am I supposed to do with lookAtCFrame? Where does this go in my code?

lookAtCFrame is a CFrame value which you need to set as your shoulder CFrame

1 Like

This creates a CFrame that is angled to face the LookAtPart.
But, it aligns it with the front. If improper alignment occurs, you can use a CFrame.Angles():

lookAtCFrame = lookAtCFrame * CFrame.Angles(0, math.rad(90), 0)
--using math.rad(90) for an example
2 Likes

Mark my code as solved if it worked for you by the way :smile:

1 Like

So my final code would look like this, if it so happens to point in the right direction?

local shoulder: Motor6D = character["LeftUpperArm"]["LeftShoulder"];
local jointPosition = shoulder.Part0.CFrame:toWorldSpace(CFrame.new(0,0,0))
local cframe = CFrame.lookAt(Shoulder.Position,LookAtPart.Position)
shoulder.C0 = shoulder.Part0.CFrame:toObjectSpace(cframe)
1 Like
local RunService = game:GetService("RunService")
local TargetPart = game.Workspace.Part

RunService.RenderStepped:Connect(function()
Character.IDontKnow.Motor6D.C0.CFrame = CFrame.new(Character.IDontKnow.Motor6D.C0.CFrame.Position, TargetPart.Position)
end)

Hope this helps.

1 Like

Probably. I’m still new to a lot of things in Lua, so I don’t know what shoulder: Motor6D does.

Edit: I think it’s based on finding an instance with like values assigned.

1 Like

It’s typescript. It defines the local variable shoulder as a Motor6D. It will error if it is not a Motor6D.

It is useful as by default Roblox doesn’t always know what type of thing a variable is, which means it cannot assist you with tab completion.

Makes sense. I’m more of a Javascript person personally, so a good amount of this is still new.

1 Like

This does not work. The arm has moved to a fixed position, and is not pointing at the part. Player movement does not effect the arm. The result is exactly the same if I replace CFrame.new with CFrame.lookAt.

Thank you for your contribution.

2 Likes

Truly a 1,000 IQ thread. Never before seen such intelligent answers.

2 Likes