You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I wanted to make my R6 arm point the direction the mouse is pointing
What is the issue? Include screenshots / videos if possible!
The mouse point in the wrong direction
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried searching for solutions on the Developer Hub but I cant find anything
This is the script that i use that causes the arm to point in wrong directions:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local torso = character:WaitForChild("Torso")
local rightArm = character:WaitForChild("Right Arm")
local mouse = player:GetMouse()
-- Create a Motor6D joint to attach the right arm to the torso
local motor6d = Instance.new("Motor6D")
motor6d.Part0 = torso
motor6d.Part1 = rightArm
motor6d.Name = "Right Arm Motor6D"
motor6d.C0 = CFrame.new(1.5, 0.5, 0)
motor6d.C1 = CFrame.new(0, 0.5, 0)
motor6d.Parent = rightArm
-- Get the initial look vector of the arm
local initialLookVector = rightArm.CFrame.lookVector
mouse.Move:Connect(function()
local lookVector = (mouse.Hit.p - rightArm.Position).Unit
-- Calculate the angle between the initial look vector and the current look vector
local angle = math.acos(initialLookVector:Dot(lookVector))
-- Rotate the Motor6D joint by the calculated angle around the axis perpendicular to both look vectors
local rotationAxis = initialLookVector:Cross(lookVector).Unit
motor6d.C0 = CFrame.new(1.5, 0.5, 0) * CFrame.Angles(angle, rotationAxis.x, rotationAxis.y, rotationAxis.z)
end)
Could you provide a video/screenshot of what’s happening so we have a better visualization of the problem. If i understand the context, the arm isn’t pointing towards the mouse correctly. Here is a script i made that should work.
Script:
wait()
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local CN,CA = CFrame.new,CFrame.Angles
local an = math.asin
repeat wait() until character:FindFirstChild("Torso")
local armOffset2 = character.Torso.CFrame:Inverse() * character["Right Arm"].CFrame
local armWeld2 = Instance.new("Weld")
armWeld2.Part0 = character.Torso
armWeld2.Part1 = character["Right Arm"]
armWeld2.Parent = character
mouse.Move:Connect(function()
local cframe = CN(character.Torso.Position, mouse.Hit.Position) * CA(math.pi/2, 0, 0)
armWeld2.C0 = armOffset2 * character.Torso.CFrame:toObjectSpace(cframe) * CN(0,0,-0.5)
end)
Instead of using Motor6d, I used welds. If you’re using a tool this also works. You just have to create a connection when equipped and whenever the tool is unequipped, use :Disconnect().
Ok, so I have found a script from someone else, and while the script works, I wonder how would I make the arm to only go up and down and not left and right. This is the script that I meant:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
--Weld for the cframing plus motor6ds are out of my league and animations will still work like nothing happen to it
local armWeld = Instance.new("Weld")
armWeld.Part0 = char.Torso
armWeld.Part1 = char:WaitForChild("Right Arm")
armWeld.Parent = char
--pi/2 is also known as 90 deg.(just wanted to point this out lol)
local piDiv2 = math.pi/2
RunService.Heartbeat:Connect(function()
local CFraming = CFrame.new((char.Torso.CFrame * CFrame.new(1.5, 0.5, 0)).Position, mouse.Hit.Position) * CFrame.new(0, 0, -0.5) * CFrame.Angles(piDiv2, 0, 0)
armWeld.C0 = char.Torso.CFrame:ToObjectSpace(CFraming)
end)
If you only want the arm to move from up to down, i’d rather do this:
wait()
local RS = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
RS.RenderStepped:Connect(function()
repeat wait() until character and character:FindFirstChild("Torso")
if character and character:FindFirstChild("Torso") then
local RightShoulder = character["Torso"]:WaitForChild("Right Shoulder")
local X = -(math.asin((mouse.Origin.p - mouse.Hit.p).unit.y))
local _, Y, Z = RightShoulder.C0:ToEulerAnglesXYZ()
RightShoulder.C0 = CFrame.new(RightShoulder.C0.Position) * CFrame.Angles(X+1.8, Y, Z) --You may want to change the number to whatever result you want to achieve, it's up to you
end
end)