How to weld a part to another?

Hello I have this gun and it has 2 grips. These grips are where I want the hands to be holding the gun. The first grip is the trigger and the second is the end of the gun. Is it possible to do some math to automatically position the gun to move in a orientation and position where the gun moves in the character at a position where both grips of the gun are being touched by the correct hand? I’m doing this so it’s easier to animate. Right now, my system is a little out of control

Here’s my code:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Wait()
	local gun = game.ServerStorage.Model:Clone()
	gun.Parent = plr.Character
	
	local HandRightWeld = Instance.new("WeldConstraint")
	local HandLeftWeld = Instance.new("WeldConstraint")
	
	local RightHand = nil
	local LeftHand = nil
	
	if plr.Character:FindFirstChild("Right Arm") then
		RightHand = plr.Character:FindFirstChild("Right Arm")
		LeftHand = plr.Character:FindFirstChild("Light Arm")
	elseif plr.Character:FindFirstChild("RightHand") then
		RightHand = plr.Character:FindFirstChild("RightHand")
		LeftHand = plr.Character:FindFirstChild("LeftHand")
	else
		error("something went wrong")
	end
	
	gun:PivotTo(CFrame.new(RightHand.Position))
	
	HandRightWeld.Parent = RightHand
	HandRightWeld.Part0 = RightHand
	HandRightWeld.Part1 = gun.Hand1
	
	HandRightWeld.Parent = LeftHand
	HandRightWeld.Part0 = LeftHand
	HandRightWeld.Part1 = gun.Hand2
	
end)

Is it possible to do some math to be able to position the gun grips to always be connected to the arms?
image
see how the arm is outstretched to this handle peice. Is it possible to make it so both gun grips are always touching the arm in the correct spot? For example
image
this way I will be able to animate the characters arms and the gun will follow the animation automaticacly

2 Likes

I advise changing somewhere along the Grip direction located in Properties of Appearance to which desired orientation of how you want your gear to face.

  • GripForward
  • GripPos
  • GripRight
  • GripUp

Another method is using the Tool Grip Editor plugin which is more easier but cost Robux.

2 Likes

It’s not a tool, it’s a model. I don’t want to use Roblox’s ugly tool system

2 Likes

You need to use gun:GetPivot().Position (assuming it’s a model), which will get its position. Then you would be able to use PivotTo.

If that does not work, try this instead

gun:PivotTo(CFrame.lookAt(RightHand.Position,LeftHand.Position))

Otherwise, you might have to resort into :SetPrimaryPartCFrame()

1 Like